Forum Moderators: coopster
<select name="cart_fk" id="cart_fk">
<?php
do {
?>
<option value="<?php echo $row_Recordset1['in_cart_id']?>"><?php echo $row_Recordset1['in_cart_name']?></option>
<?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
$rows = mysql_num_rows($Recordset1);
if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
?>
</select>
Alright. Now, the question is... I only want to have it echo the last 5 most recent results. How do I do that. I would imagine its somewhere in the the area of this area:
if($rows > 0) {
mysql_data_seek($Recordset1, 0);
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
}
So, if someone couldd throw me some pointers, I would greatly appreciate it! Thanks in advance!
1. Make sure your query that's populating the option box is sorted from most recent to least recent.
2. Insert a $count as a condition in the while loop (set $count = 1 to begin with, also set a $MAX_RECENT = 5). Then its just this:
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1) && ($count < $MAX_RECENT) );
This loop will exit even if you don't have 5 records to produce so no worries :P
That section you pointed out earlier is only going to give you the first record returned in the query. Not sure, but were you looking for something like this:
Provided the records are sorted from most to least recent.
$count = 0;
$MAX_RESULT = 5;
while ($row = mysql_fetch_assoc($result) && ($count < $MAX_RESULT) )
{
//do stuff
$count++
}
But, if your doing that you might as well just write a better query to select the top 5 most recent
--Nick