shogunJ Posted July 6, 2008 Share Posted July 6, 2008 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 !! Quote Link to comment Share on other sites More sharing options...
DarkWater Posted July 6, 2008 Share Posted July 6, 2008 Every article should have a unique ID. Who cares if it's "out of order" every once in a while when you delete something? And yes, PHP has a foreach loop....you could have googled that. Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 6, 2008 Share Posted July 6, 2008 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"; } ?> Quote Link to comment Share on other sites More sharing options...
shogunJ Posted July 6, 2008 Author Share Posted July 6, 2008 oh ok i think i get it, thanks mjdamato Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.