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
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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.