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 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误