0

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.

3
  • It's not clear what your trying to achieve sorry. Commented May 13, 2013 at 2:49
  • What do you mean use a list to print out updates? Are you trying to achieve some styling effect? Are you trying to use a list HTML tag? Commented May 13, 2013 at 2:52
  • Yes im trying to do this for styling reasons. I updated my query above Commented May 13, 2013 at 2:58

1 Answer 1

1

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>";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! That should get me going in the right direction!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.