Jump to content

[SOLVED] Another simple blog type question


phatgreenbuds

Recommended Posts

So I have this database that stores individual entries and they can be retrieved with no issue for display.  I would like to take the most recent entry though and display it with a different CSS style. Thing is, I know how to get them all out of the database but how would I write the query to single out the most recent based on ID which is also the primary key?

Link to comment
https://forums.phpfreaks.com/topic/79474-solved-another-simple-blog-type-question/
Share on other sites

Actually here is part of what I have so far.

 

<?php while ($blog = mysql_fetch_array($bloggins)) { ?>

<table width="75%" border="1" cellpadding="1" cellspacing="0">
  <tr><td width="12%" valign="top"><?php echo $blog["date"]; ?></td>
  <td width="76%"><?php echo $blog['blogtext']; ?></td>
  <td width="12%" valign="top"><?php echo "<a href=\"blogeditor.php?id={$blog['id']}\">Modify/Delete</a>"; ?></tr>
</table>

<?php } ?>

 

So using your method I still need a method to define $latest_id_number? or is this a built in function?

Something like this.. im not sure of your DB structure so you will have to modify it

 

<?php

$i = 0; // this is a simple way of doing it, so set the $i to 0

while ($blog = mysql_fetch_array($bloggins))
{
  if ($i == 0) // only if $i = 0.. otherwise do the normal.. when this while loop is finished, it will increment the $i
  {
    // the first blog html goes here
  }
  else
  { ?>

  <table width="75%" border="1" cellpadding="1" cellspacing="0">
    <tr><td width="12%" valign="top"><?php echo $blog["date"]; ?></td>
    <td width="76%"><?php echo $blog['blogtext']; ?></td>
    <td width="12%" valign="top"><?php echo "<a href=\"blogeditor.php?id={$blog['id']}\">Modify/Delete</a>"; ?></tr>
  </table>

<?php
  }
  
  $i++; // increment the $i
}
?>

 

 

---------

 

 

Another way to do it would be to use an SQL statement to get the latest blog's ID, then you could just check it like this:

 

if ($blog['blog_id'] == $the_lastest_blog_id_we_just_got_from_the_sql_statement)
{
  // do the first blog stuff
}
else
{
  //regular
}

 

doing it that way would eliminate the use of the $i

now see there ya go confusing me again...:)

 

Another way to do it would be to use an SQL statement to get the latest blog's ID, then you could just check it like this:

 

Another way to do it would be to use an SQL statement to get the latest blog's ID, then you could just check it like this:


if ($blog['blog_id'] == $the_lastest_blog_id_we_just_got_from_the_sql_statement)
{
 // do the first blog stuff
}
else
{
 //regular
}

doing it that way would eliminate the use of the $i

 

How do I write a sql query to identify the lastest or in this case the highest number ID? Considering it changes with every entry I can see the logic in using the $i but doing this with a query alone is a mystery to me.

This is how i would:

 

<?php
$sql = mysql_query("SELECT blog_id FROM blog_table ORDER BY blog_id ASC LIMIT 1") or die(mysql_error());
while ($row = mysql_fetch_array($sql))
{  $latest_id = $row['blog_id'];  }
?>

 

I'm sure there is a simpler way to get a single variable from the DB, but I've forgotten lol. But that works just the same.

Here is how you would do it

 

<?php

$bloggins = mysql_query("SELECT `date`, blogtext, id, MAX(id) as highest FROM blogs")or die(mysql_error());

$count = 0;
while ($blog = mysql_fetch_array($bloggins)) {

   if($count == $row['highest']) $css = "class_name";
   else $css = "aDifferentClassName";
   
   ?>
   
   <table class="<?php echo $css; ?>" width="75%" border="1" cellpadding="1" cellspacing="0">
     <tr><td width="12%" valign="top"><?php echo $blog["date"]; ?></td>
     <td width="76%"><?php echo $blog['blogtext']; ?></td>
     <td width="12%" valign="top"><?php echo "<a href=\"blogeditor.php?id={$blog['id']}\">Modify/Delete</a>"; ?></tr>
   </table>
   
   <?php 
   $count++;
} 
?>

 

Wes1890 - You don't need two separate queries to get the highest ID

Archived

This topic is now archived and is closed to further replies.

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