Jump to content

Archived

This topic is now archived and is closed to further replies.

Woodburn2006

previous and next

Recommended Posts

i am displaying results from a database and i wanted to be able to put next and previous links at the bottom.

how would i do this?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I am interested in knowing how to do this.

Lets say if you limit each page to 10 results how do l make it so after those 10 results a new page link is added?

Share this post


Link to post
Share on other sites
so would i get just do a normal select id from table etc etc then use the array?

i have not heard of data_seek before and cannot seem to find anything about it?

Share this post


Link to post
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....

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Can you not just use the techniques discussed in your other thread that deals with almost the same issue?

http://www.phpfreaks.com/forums/index.php/topic,103739.0.html

???

Share this post


Link to post
Share on other sites
i had a look but i more wanted to find out about the count function that this seems to use and i cannot see anything in the other thread that is similar to this, i now understand the other thread but this is annoying me a bit.

sorry

Share this post


Link to post
Share on other sites

×

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.