Jump to content

[SOLVED] Best way to check for end of a record and send it back to the first record


CBStrauss

Recommended Posts

I'm hoping someone can help me out and explain the best way to accomplish this.

 

What I'm doing I'm pulling data from a data base. I display the information 1 item at a time then have a next and previous link to navigate forward or backward through the records.

 

The issue is when it gets to the last record and I press the next link i get errors cause their is no more records. so what I'm wondering what a good way to send it back to the first record? would it make sense to store the total number of records in a variable and then load the load the id's pulled from the database into an array and check against the end of the array for the end of the records and send it back to the first element of the array?

 

Any explanations are examples would be helpful even a link to somewhere showing this would be good. I haven't found one yet.

Link to comment
Share on other sites

Run a simple query that selects the number of records in the table.  Assuming you have a variable named "$curIndex" or something, you can have a condition to determine the next index.

 

$result = mysql_query("
SELECT
	COUNT(id) AS numRows
FROM
	someTable
") or die(mysql_error());
$numRowsTotal = mysql_result($result, 0, 'numRows');

if ($curIndex + 1 == $numRowsTotal) {
    $nextIndex = 0;
}
else {
    $nextIndex = $curIndex + 1;
}

Link to comment
Share on other sites

Thanks this is a start for me to play around with, I'm trying to do this with a image gallery so when they are viewing the the larger image and not the thumbnail there be these next previous links to go on to the next large image rather then have to go back to the thumbnail page.

 

So basically I'm just trying to find a way to modify and improve my existing code to make it more end user convenient. Like I said I will play around with your example and report back my results or if I run into any other questions. :)

Link to comment
Share on other sites

Something like this.

 

<?php
// Get the current index from of the URL.
$curIndex = isset($_GET['index']) ? (int) $_GET['index'] : 0;

// Get the total number of rows in the table.
// We will use this information to determine the $nextIndex.
$result = mysql_query("
SELECT
	COUNT(id) AS numRows
FROM
	someTable
") or die(mysql_error());
$numRowsTotal = mysql_result($result, 0, 'numRows');
if ($curIndex + 1 >= $numRowsTotal) {
$nextIndex = 0;
}
else {
$nextIndex = $curIndex + 1;
}

// Get a single record out of the table from the $curIndex position.
$result = mysql_query("
SELECT
	id
	,title
	,whatever
	,else
FROM
	someTable
LIMIT
	$curIndex, 1
") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);

// Display its information for testing purposes.
echo "<pre>";
print_r($row);
echo "</pre>";
}
?>

<p> </p>
<hr size="1" noshade="noshade" />
<form method="post" action="?index=<?php echo $nextIndex; ?>">
<input type="submit" name="next" value="Next Record" />
</form>

Link to comment
Share on other sites

It took me a while to figure out how to make it work backwards with a back button and when on the first record to go to the last when pressed but I got it  :D Working backwards kind of sucks sometimes but I got it to work. But just stepping through the next code line by line figuring out what it did to move forward and back to the first record I figured out where it needed to be changed to make it go in reverse.

 

I used something Like this for back link:

 

if($curIndex == 0){
$prevIndex = $numRowsTotal - 1;

}else{
$prevIndex = $curIndex - 1;
}

 

Haven't ran into any problems with let me know if you see a flaw or something in my back code if not I will mark this as solved and hopefully someone can learn from it as well.

 

Thanks again for your help, the Idea I had involved taking the id out of the database and storing it an array then using that which would of been a lot more coding and lot more un needed headaches I could of avoided. Thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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