Jump to content

Recommended Posts

if you are passing data via the url then simply have..

<a href="thisscript.php?id=XXX">previous</a> <a href="thisscript.php?id=YYY">next</a>.

Now you must grab the correct values of xxx and yyy to make this work. To do so I would (not the most efficient I suspect but never mind) select all the possible result and use data_seek to traverse the array of results.
Link to comment
https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-70842
Share on other sites

dual_alliance: have a read of the pagination tutorial ([url=http://www.phpfreaks.com/tutorials/43/0.php]http://www.phpfreaks.com/tutorials/43/0.php[/url])

Woodburn: you may benefit from the same tutorial but your needs are a little more specific so here goes...

say we have 50 entries in a database and they all have a unique id. The page will grab the record that has an id of 18 and this is defined in the url like so script.php?id=18.

Now it may ot follow that previous would be 17 and next would be 19 but the code below won't be affected as we will use the order by clause in the query.

ok..

<?php
[code]
<?php
// Select all the records

$qry = "SELECT * FROM `ur_table` ORDER BY `your_orderd_field`";
$qry = mysql_query($qry);

// create an array of all the results.
// the keys of this array will be the same as the table fields...
$results = array();
while ($row = mysql_fetch_assoc($qry))
{
foreach($row as $key => $val)
{
  $results[$key][] = $val;
}
}

// grab the record you are looking for (id=18)
$curr = array_keys($results['id'], 18);
$curr = $curr[0]; // array_keys returns an array - even if only one result!

$prev = $curr - 1;
$next = $curr + 1;

echo $results['text_to_display']; // echo the result you want.

echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=" . $prev . "\">previous</a>";
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=" . $next . "\">next</a>";

?>
[/code]

Now there is a more efficient way of doing this which would involve two queries BUT there would be a similar computation to do to get the relevant id's.  Only go down that route if the amount of data in your table is enormous....
Link to comment
https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-70853
Share on other sites

First you need to define how many rows you are going to display per page.
[code]
define ('ROWS_PER_PAGE', 20);
[/code]

for example.  Then you need a page number. If we were on page two, for example, we would expect to display rows 20 - 39 (because we count from 0). So we set a start and end value.
[code]
$pagenum = 2;

$start = ($pagenum - 1) * ROWS_PER_PAGE;
$end = ROWS_PER_PAGE;
[/code]

Actually, $end here is just an offset. I have set the page num to two explicitly, you would need to be keeping track of it in some other way.

Then in your query, you fetch only the rows you want by using a limit clause with the $start and $end values. E.g.
[code]
$query = "select * from blah where blahblah=something limit $start, $end";
[/code]
Now you will get the rows returned that you want. Now you just display the next and previous links using the page num you have
[code]
echo "<a href=\"this.php?page=" . $pagenum - 1 . "\">Previous</a> | <a href=\"this.php?page=". $pagenum + 1 . "\">Next</a>";
[/code]

Well, something like that anyway.
Link to comment
https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-70866
Share on other sites

thanks toon. i can get it to work but the previous button does not work in the same way as the next button. if clicked the next button will keep going around the results so when it gets to the last result it will go back to the first and go throught them again, but the previous button gets to the first result (id=1) and then when previous is clicked it goes to (id=-1) which does not exist. obviously creating errors and an ugly page.

any ideas?
Link to comment
https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-71715
Share on other sites

yep

change this bit...
[code]<?php
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=" . $prev . "\">previous</a>";
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=" . $next . "\">next</a>";
?>
[/code]

to this.

[code]<?php
if ($curr > 0)
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=" . $prev . "\">previous</a>";
if ($curr < (count($results['id']) - 1)
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=" . $next . "\">next</a>";
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-71732
Share on other sites

i can get the previous if statement to work but this statement for the next button does not seem to work:

[code]if ($curr < (count($results['id']) - 1){echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?go=drawing&id=" . $next . "\">Next >></a>";}[/code]

it will not load the page.

this is the section it does not like:

[code](count($results['id']) - 1)[/code]

Link to comment
https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-72681
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.