Page 1 of 1

finding newest id

Posted: Sun Dec 16, 2007 8:03 pm
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)

Posted: Sun Dec 16, 2007 9:34 pm
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.

Posted: Sun Dec 16, 2007 9:41 pm
by ekosoftco
didnt even think about that, since they are already in date order. thanks youre a lifesaver. now i can sleep :D

Posted: Sun Dec 16, 2007 10:04 pm
by Josh1billion
You're welcome. :)