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
Your if()
in your NEXT
line is wrong, as you are checking $record_current
against the query - $view_result
instead of the number of rows - $view_count
. Try changing to -
if($record_current < $view_count)
EDIT
Take a look at SQL_CALC_FOUND_ROWS
/ FOUND_ROWS()
at http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows.
$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()");
$rsView = mysql_fetch_assoc($view_result);
Note : in getting the LIMIT $record_current,1
be reminded of computers logic that every counts start at zero so if your trying to get the first data you must set your initial value of $record_current
as 0
if(isset($_GET['record'])) {
$record_current = $_GET['record'];
}else{
$record_current = 1;
}
$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";
The problem is that the first record to be displayed is actually the 2nd record from the query
note that when you are using LIMIT the offset of the first row will always be 0 not 1...