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
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";

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.