finding newest id

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

finding newest id

Post by ekosoftco »

ok, i have a code for a news system, and what im trying to do is echo New by the newest post, so the new news is marked new. ive tried using a query for max(id), but it will only show the newest news and thats it. i need it to still show all the others as well. this is the code im using right now.

Code: Select all

$query = "SELECT id, title, timestamp, type FROM news ORDER BY timestamp DESC LIMIT 0, 7";

$result = mysql_query($query) or die ("Error in query: $query . " . mysql_error());

// if records present
if (mysql_num_rows($result) > 0)
{
	//iterate through resultset
	// print article titles
	while($row = mysql_fetch_object($result))
	{
	?>
        </p>
		<?php
		$type = $row->type;
		$id = $row->id;
		$title = $row->title;
		$timestamp = formatDate($row->timestamp);
		if (($id+1) != $row->id){
		echo "NEW!&nbsp;";
		}
ive tried just about everything i can think of. the code in there right now puts NEW by every single news item. please help me :(
(the rest of the code simply prints the titles and dates of the news, and i didnt see a reason to post it, but that is why the code is still open)
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Code: Select all

$query = "SELECT id, title, timestamp, type FROM news ORDER BY timestamp DESC LIMIT 0, 7";

$result = mysql_query($query) or die ("Error in query: $query . " . mysql_error());

// if records present
if (mysql_num_rows($result) > 0)
{
   //iterate through resultset
   // print article titles
    $i = 1;
   while($row = mysql_fetch_object($result))
   {
   ?>
        </p>
      <?php
      $type = $row->type;
      $id = $row->id;
      $title = $row->title;
      $timestamp = formatDate($row->timestamp);
      if ($i <= 5)
        echo "NEW!&nbsp;";
      $i++;
   }
Notice the "$i" code-- the above should display "NEW" for the first 5 entries printed.
ekosoftco
Forum Contributor
Posts: 108
Joined: Fri Aug 04, 2006 8:21 pm

Post by ekosoftco »

didnt even think about that, since they are already in date order. thanks youre a lifesaver. now i can sleep :D
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

You're welcome. :)
Post Reply