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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度