dougua4836 2013-05-13 02:46
浏览 34
已采纳

在php中使用echo与列表?

I have the following PHP code:

$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
  $id = $row['id'];
  $title = $row['title'];
  $body = $row['body'];
  $date = $row['date'];
  echo "<div class=\"title\">$title</div><br>";
  echo nl2br($body);
  echo "<br><div class=\"date_time\">".time_ago($date)."</div>";
  echo "<hr>";
}

This is used to create a news feed and I use echo to print out what is within the updates. Is there a way in which I could maybe use a list to print out each update instead of the way im currently doing it?

Or is it possible to create a div around each update that the while loop creates?

I'm sorry if the question is not clear but thanks for all the help!

My newsfeed creates updates in a news feed like twitter. Each update is printed out using echo and surrounded by


. Im trying to find a way in which I can create a list or div for the entire layout of each update. Im finding very difficult to arrange whats going on in each update.
  • 写回答

1条回答 默认 最新

  • douchi5822 2013-05-13 02:55
    关注

    The following code should get you there. Just echo out the html as you would like it to appear.

    $getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
    while ($row = mysql_fetch_assoc($getnews)) {
        $id = $row['id'];
        $title = $row['title'];
        $body = $row['body'];
        $date = $row['date'];
        echo "<div class='news-article'>";
        echo "<div class=\"title\">$title</div><br>";
        echo nl2br($body);
        echo "<br><div class=\"date_time\">".time_ago($date)."</div>";
        echo "</div>";
        echo "<hr>";
    }
    

    A lot of the time a lot of echo statements like that just confuse what you are trying to do.

    $getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
    while ($row = mysql_fetch_assoc($getnews)) {
        $id = $row['id'];
        $title = $row['title'];
        $body = $row['body'];
        $date = $row['date'];
        ?>
        <div class='news-article'>
        <div class="title"><?php echo $title ?></div><br>
        <?php echo nl2br($body); ?>
        <br><div class="date_time"><?php echo(time_ago($date)) ?></div>
        </div>
        <hr>
        <?php
    }
    

    If you wanted to accomplish the same thing as an unordered list you would do this.

    $getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
    echo "<ul class='article-list'>";
    while ($row = mysql_fetch_assoc($getnews)) {
        $id = $row['id'];
        $title = $row['title'];
        $body = $row['body'];
        $date = $row['date'];
        ?>
        <li class='news-article'>
        <div class="title">$title</div><br>
        <?php echo nl2br($body); ?>
        <br><div class="date_time"><?php echo(time_ago($date)) ?></div>
        </li>
    
        <?php
    }
    echo "</ul>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里