简体   繁体   中英

Next record doesnt exist error handling

I have this bit of code that goes to the next record in the database based on the record id, when it gets to the last record the next time you click it the code breaks due to the record not existing in the table and the url not being valid. Is there some error handling i could put into this to show an alert when the last record has been reached? Thanks

<a id="next" data-ajax="true"  href="eventviewtester.php?eventid= <?php echo $_GET['eventid'] +1;?>" style="display:none;"></a>

It seems as if you are dealing with a degenerate form of pagination - the case where you are paginating a single record at a time. As such, you should use typically pagination methodology which requires that you understand the size of the total possible result set and where within that result set you are.

I would recommend going away from blindly relying on row id + 1 for paginated viewing as this mechanism may not be reliable (for example what happens if you delete a row from the table leaving a gap in the number sequence?). You should perform a LIMIT X,Y type of query on your table to understand specifically the rows you are dealing with. Perhaps in your case that means getting a group of two records at a time so you can display the current record as well as display a link to the next record (or not show a next link if you have reached the upper record limit and your query only returned a single record).

Here are some query suggestions.

This would get the currently selected record plus the next highest one. If only one row is returned, this is the last record

SELECT [fields]
FROM [table]
WHERE eventid >= [value from GET]
ORDER BY eventid ASC
LIMIT 2

Or if you you want to deal in terms of "pages" ie not rely on event id itself, but rather event in X position

SELECT [fields]
FROM [table]
ORDER BY eventid ASC
LIMIT X,1

Here X would be a zero-based value such that if you wanted 10th event in ascending order, you would pass a value of 9. Here you don't need to know the next event id number (you can just call for page number 11 in link from page 10), so we only return 1 record. Of course, to understand the total number of pages you would need to perform an additional simple query to determine the number of rows in the table.

Since you have mentioned in your comments that you want to try the eventid based approach, then I think the first query is what you would need to go with. So when you read the result set, obviously the first record will contain the data that you need for displaying on the current page. You then need to get the eventid information from the second record and use this in building your query string.

// you would grab next event id in your second iteration through the DB result set
$next_event_id = $row['eventid'];

// then use it when building your URL reference
echo '<a id="next" data-ajax="true"  href="eventviewtester.php?eventid=' . $next_event_id . '" style="display:none;"></a>';

Find the total number of records and then display the link if the count is less than or equal to that number.

// FIND TOTAL RECORD COUNT
$number_of_records = 20;

// CHECK TO SEE IF THE COUNT IS LESS THAN OR EQUAL TO THE TOTAL 
if ($_GET['eventid'] + 1 <= $number_of_records) {
    echo '<a id="next" data-ajax="true"  href="eventviewtester.php?eventid='.($_GET['eventid'] + 1).'" style="display:none;">Link Text</a>';
}
else {
    echo 'Non-Link Text';
}

I am not sure about this, just try this. Use count value for getting the number of records.

if($count>$_GET['eventid']){
<a id="next" data-ajax="true"  href="eventviewtester.php?eventid= <?php echo $_GET['eventid'] +1;?>" style="display:none;"></a>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM