Jump to content

Archived

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

eRott

[SOLVED] MySQL: Find Last Row

Recommended Posts

Hey,

 

Okay so I have a database with the primary key set as the ID field which is set to auto_increment. What I am looking to do is a simple if statement to change the value of a variable depending if it is the last row (the most recently added) in the database as you can see from the example below. (I have clearly marked the sections of interest).

 

<?php

include 'global/db.php';

$query = "SELECT * FROM bosses ORDER BY id DESC";
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
echo '<div class="boss">';
echo '<div class="bar">';	
echo '<div class="title">'.$row['raid_date'].' :: '.$row['raid_name'].' - '.$row['boss_name'].'</div>';

?>

<div class="toggle"><a href="javascript:;" onmousedown="toggleSlide('boss<?php echo $row['id']; ?>');" style="text-decoration: none;"><img src="images/btn-toggle.png" style="border: 0px;" /></a></div>

<?php

/* ---------------------------- */
/* ---START IMPORTANT SECTION-- */
/* ---------------------------- */

if (LAST_ROW_IN_DATABASE) { // as you may have noticed, there is where I am stuck
	$display = "display: block";
else {
	$display = "display: none";
}

echo '</div>';
echo '<div class="content" id="boss'.$row['id'].'" style="'.$display.'; overflow: hidden; height: 600px;">';

/* ---------------------------- */
/* ----END IMPORTANT SECTION--- */
/* ---------------------------- */

$fields = explode(', ', $row['boss_loot']);
foreach($fields as $val){
	list($item_id, $item_name) = explode('|', $val);
	echo '<a class="q4" href="http://wowhead.com/?item='.$item_id.'">['.$item_name.']</a> | ';
}

echo '<a href="'.$row['ss_url'].'" alt="'.$row['raid_name'].' - '.$row['boss_name'].'"><img src="'.$row['ss_url'].'" style="padding-top: 5px; border: 0px; height: 580px; width: 880px;" /></a>';
echo '</div';
echo '</div>';
}
?>

 

Essentially there are a bunch of div content containers and I only want the latest one to be shown while all of the rest are hidden.

 

Any ideas?

 

Thanks.

Share this post


Link to post
Share on other sites

you could get the max value from that database like this

$query = "SELECT MAX(`ID`) AS `max` FROM `bosses`";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$max = $row['max'];

then change this

if ($row['id'] == $max) { // as you may have noticed, there is where I am stuck
	$display = "display: block";
else {
	$display = "display: none";
}

 

Scott.

 

Share this post


Link to post
Share on other sites

Rat's solution is much more readable, but less efficient...

 

Ah the eternal battle of readability vs efficiency.

 

You could also do something like:

 

<?php
mysql_data_seek($result, mysql_num_rows($result) - 1);
$row = mysql_fetch_array($result);
$max = $row['id'];
mysql_data_seek($result, 0);
?>

 

It's a bit more efficient than rat's and almost as readable... but it only works if you're selecting in order of IDs ascending. Compare the $max var against the current row id, just as in rat's.

 

PS. Your query is already in order of ID descending, that would make the first result always the one with the highest ID... why not just do it on the first iteration of the while() loop?

Share this post


Link to post
Share on other sites

$sql = "SELECT * FROM bosses ORDER BY id DESC limit 0,1" ;

will return the last record(most recently added record).

 

P.S Relational database theory states that row order in a database is not guaranteed.

 

Some database engines may add rows that are physically where you would expect them to be, but others may not, and deletions or updates may affect this order, too.

 

The point is this: you should always select records based on the data that is contained in the records, never on what you assume is the order of the records on the storage device.

 

 

 

Share this post


Link to post
Share on other sites

@ratcateme

Excellent. Thank you kindly :). That is exactly what I was looking for. Works wonderfully (minus the single quotes). Cheers!

 

@genericnumber1

Hey, thanks for your input as well. However, ordering them in an ascending manner will not work as that would result in the newest ones being on the bottom. As per your "PS.", if I knew how to apply conditions to the first repetition of a while loop, then I wouldn't be here asking now would I? ;) Either way, thanks again. I appreciate taking your time to help.

 

@coder500

... What? I do not see your point. Who said anything about assuming the order of records in a database? But yes, your line of code would work too if I were to use it like:

 

<?php
$query = "SELECT id FROM bosses ORDER BY id DESC LIMIT 0,1";
$result = mysql_query($query) or die(mysql_error());
$grab = mysql_fetch_assoc($result);
$max = $grab['id'];
?>

 

Anyway, thank you to all of you. Now that I look at it, this was really a rather simple solution to a simple question I had somehow managed to overlook :P. As always, I tend to over think a problem expecting the answer to be much more difficult then it really is or has to be.

 

Take care,

eRott

Share this post


Link to post
Share on other sites

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