Jump to content

Data Presentation Help


shogunJ

Recommended Posts

Hey my first post,

 

Firstly I'm still quite new to php/mysql and I've been doing as much homework as I can to pick things up smoothly, however I've hit a snag and would greatly appreciate some advice

 

I've created a table in my database which I plan to keep news posts in, for instance the table has the fields 'title' 'content' 'author' and 'dateposted'.

 

What I really wanted to do was to present each news post in a forum style...

 

Title 1

  content content content

 

posted: Jan 01

 

Title 2

  content content content

 

posted: Jan 03

 

 

etc etc, does php offer a 'for each' loop? I didnt want to use an auto number as when I delete posts, this may mess up the order etc.

 

Sorry if this has been quite a lot to read, just wanted to give as much detail as I could, thanks in advance all !!

Link to comment
https://forums.phpfreaks.com/topic/113489-data-presentation-help/
Share on other sites

Yes, there is a foreach loop in PHP. But, for this you would want to use a while loop for displaying records from a database query.

 

Example:

<?php

$query = "SELCT * FROM table ORDER BY dateposted";
$result = mysql_query($query) or die (mysql_error());

while ($post = mysql_fetch_assoc($result)) {

 echo $post['title'] . "<br>\n";
 echo $post['content'] . "<br>\n";
 echo "Posted: " . $post['dateposted'] . "<br>\n";

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.