【问题标题】:How I can generate "read more" link dynamically for news site如何为新闻站点动态生成“阅读更多”链接
【发布时间】:2013-11-12 08:43:10
【问题描述】:

我正在做新闻网站并像这样从数据库中获取新闻

<div class="left-content nine-column" style="font-size:13px; line-height:25px; font-family:open sans;">
    <?php
        $qry = "Select * from  tbl_news";
        $result = null;
            if(!$result = mysqli_query($con,$qry)) 
                Die("Error in database");
            else
            {
                if(mysqli_num_rows($result) >= 1)
                {
                    while(($rec = mysqli_fetch_assoc($result)) != null) 
                    {
                        echo '<div class="post">
                        <h1>'. $rec['news_heading'] .'</h1>
                        <ul class="post-meta">
                        <li><a href="" title=""><i class="icon-calendar-empty"></i><span>'. $rec['news_date'] .'</span></a></li>
                        <li><a href="" title=""><i class="icon-user"></i>By'.$rec['postedby'].'/a></li>
                        <li><a href="" title=""><i class="icon-map-marker"></i>'.$rec['news_location'].'</a></li>
                        </ul>
                        <div class="post-desc">
                        <p>'.$rec['news_ldesc'].'                                     

                        <!-- here i want read more link -->
                        </p>                        
                        <br/>
                        </div>
                        </div> '; 

                    }
                }
            }           

            ?>
        </div>

现在我想要做的是我想阅读更多关于他们重定向到news_details.php的新闻项目的链接,并且将在那里显示一条新闻,我如何得到它,请帮助我

【问题讨论】:

标签: php html css


【解决方案1】:

首先,您需要构建news_detail.php,以处理单个帖子的查看。在那里,你需要决定要带来哪些新闻细节,通常人们用 id 来做。然后,您需要从您的链接中传递 id,例如 href='news_details.php?id=your_id_here'

【讨论】:

    【解决方案2】:

    您应该在数据库中有新闻 id。当您检索所有新闻并循环打印时,您应该使用以下代码。我不知道数据库中新闻 id 列名的名称是什么

    echo '&lt;a href="news_details.php?id='.$rec['news_id'].' "&gt;Read More&lt;/a&gt;'; // news_id should be matched with database column name

    现在您应该为新闻详情编写代码。您应该使用 get 方法从数据库中查询接收新闻 id 并打印到循环中。

    更新: 在 news_details.php 中你应该通过 GET 方法$_GET["id"] 接收新闻 id 的值,然后编写代码获取数据库表的行。即

    $newsId = $_GET["id"];
    $qry = "Select * from  tbl_news where id = $newsId ";
    
    $result = mysqli_query($con,$qry);
    
    while($row = mysqli_fetch_array($result))
    {
      echo $row['news_ldesc'] ;
    }
    

    试试这个。

    【讨论】:

    • 我在 url 中尝试了同样的事情,它显示 localhost/Theme/news_detail.php?id=1 但没有结果,只是 balnk page
    • 我已经更新了我的答案,你知道以后应该做什么
    【解决方案3】:

    您可以为特定新闻设置单独的链接,例如

    href='news_description.php?id='.base64_encode($id)
    

    【讨论】:

      【解决方案4】:

      使用以下步骤:

      1) 您可以使用php substr 将您的新闻内容截断为多个单词。然后在其下方添加阅读更多链接。

      您的代码将如下所示:

      <div class="left-content nine-column" style="font-size:13px; line-height:25px; font-family:open sans;">
               <?php
                  $qry = "Select * from  tbl_news";
                  $result = null;
                  if(!$result = mysqli_query($con,$qry)) 
                  Die("Error in database");
                  else
                  {
                  if(mysqli_num_rows($result) >= 1)
                  {
                  while(($rec = mysqli_fetch_assoc($result)) != null) 
                  {
                  echo '<div class="post">
                  <h1>'. $rec['news_heading'] .'</h1>
                  <ul class="post-meta">
                  <li><a href="" title=""><i class="icon-calendar-empty"></i><span>'. $rec['news_date'] .'</span></a></li>
                  <li><a href="" title=""><i class="icon-user"></i>By'.$rec['postedby'].'/a></li>
                                                  <li><a href="" title=""><i class="icon-map-marker"></i>'.$rec['news_location'].'</a></li>
               </ul>
              <div class="post-desc">
              <p>'.substr($rec['news_ldesc'], 0, 100).'                                     
      
                 <!-- here i want read more link -->
                  </p>                        
              <br/>
              </div>
              </div> '; 
      
                              }
      
                          }
                      }           
      
                  ?>
              </div>
      

      如果你看到了,我使用了substr($rec['news_ldesc'], 0 , 100),它将你的新闻内容截断为 100 个字。 0 开始,100 结束。

      2) 使用 Kuzgun 的答案

      另请阅读:

      How can I truncate a string to the first 20 words in PHP?

      【讨论】:

        猜你喜欢
        • 2016-11-12
        • 1970-01-01
        • 1970-01-01
        • 2013-02-10
        • 1970-01-01
        • 1970-01-01
        • 2014-12-11
        • 1970-01-01
        • 2013-07-25
        相关资源
        最近更新 更多