Jump to content

"Latest" box


Azzyh

Recommended Posts

Hello phpfreaks.com!

 

I want to learn how to make a script, that receives the latest column inserted in 3 tables, and then shows them with fields title and date. Checks for the last column by the latest id... order by id maybe?

 

I dont know how to do this, as i never had that in thought. Ive seen this happend in many sites, and in forums like "Latest posts" where its a box showing the poster's username and the title of the thread. So i know this is possible.

 

Thanks

 

Link to comment
Share on other sites

You mean the latest row(s) not columns. Yes, it is as easy as sorting by a field and getting the first n records. Here is an example which you would need to modify according to your particular database:

 

//Create query to get last 5 records based upon date field
$query = "SELECT field1, field2, field3 FROM table ORDER BY date DESC LIMIT 0, 5";
$result = mysql_query($query);
while($record = mysql_fetch_assoc($result))
{
    echo "{$record['field1']}, {$record['field2']}, {$record['field3']}<br />\n";
}

Link to comment
Share on other sites

Depends on what you mean. If you are referring to three tables that are related and you need data for a total of three recordsets of combined data from those three tables then you would need to use joins.

 

However, if you need three different sets of records each from each table then just run three queries.

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.