phatgreenbuds Posted November 29, 2007 Share Posted November 29, 2007 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? Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 29, 2007 Share Posted November 29, 2007 When displaying each blog using a FOR or WHILE loop (and i assume you are), just do a simple check: if ($loop['id'] == $latest_id_number) { // do something different } else { // do the regular css stuff } Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted November 29, 2007 Author Share Posted November 29, 2007 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? Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 29, 2007 Share Posted November 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
grejon04 Posted November 29, 2007 Share Posted November 29, 2007 You could also make a date field in the database and key it from the most recent date. Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted November 29, 2007 Author Share Posted November 29, 2007 ok this makes logical sense now. I will give it a try and see how it goes. Thanks... Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 29, 2007 Share Posted November 29, 2007 No problem, let us know how it goes Quote Link to comment Share on other sites More sharing options...
phatgreenbuds Posted November 29, 2007 Author Share Posted November 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 29, 2007 Share Posted November 29, 2007 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. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 29, 2007 Share Posted November 29, 2007 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 Quote Link to comment Share on other sites More sharing options...
Wes1890 Posted November 29, 2007 Share Posted November 29, 2007 ^ Ahh yes, i appreciate the correction Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.