Can someone please assist me with a problem of navigating through returned records? I have a mysql query:
if(isset($_GET['record'])) {
$record_current = $_GET['record'];
}else{
$record_current = 1;
}
$useremail = $_SESSION['useremail'];
$view_sql = "SELECT * FROM missions, calendar, bookings
WHERE missions.missions_id = calendar.missions_id
AND calendar.calendar_id = bookings.calendar_id
AND bookings.booking_email ='$useremail' LIMIT $record_current,1";
$view_result = mysql_query($view_sql) or die(mysql_error());
$view_count = mysql_num_rows($view_result);
$rsView = mysql_fetch_assoc($view_result);
The query produces the results desired. The values are used to fill a dynamic form. The problem I am having is navigating through the records properly. I am using this code for navigation (to allow a user to see all the bookings they have made). The code:
<span class="label"><?php if($record_current > 1) { ?><a href="view.php?record=<?php echo $record_current - 1; ?>" >Previous</a><?php } ?></span>
<span class="element"><?php if($record_current < $view_result) { ?><a href="view.php?record=<?php echo $record_current + 1; ?>" >Next</a><?php } ?></span>
The problem is that the first record to be displayed is actually the 2nd record from the query and the 'Next' link displays even when the last record is displayed. If the link is clicked it shows a blank form. I don't know what I am doing wrong and I've played with this for hours, any help would be greatly appreciated. Cheers
Edit
I have amended the code to this (thanks to Sean):
$view_sql = "SELECT SQL_CALC_FOUND_ROWS * FROM missions, calendar, bookings
WHERE missions.missions_id = calendar.missions_id
AND calendar.calendar_id = bookings.calendar_id
AND bookings.booking_email ='$useremail' LIMIT $record_current,1";
$view_result = mysql_query($view_sql) or die(mysql_error());
$view_count = mysql_query("SELECT FOUND_ROWS() AS cnt");
$rsView = mysql_fetch_assoc($view_result);
$adj_count = (mysql_result($view_count, 0, "cnt") - 1);
Apparently, "SELECT FOUND_ROWS" returns a set starting from zero. This is now working. Thanks to all who contributed. Cheers, Spud