Jump to content


Photo

previous and next


  • Please log in to reply
10 replies to this topic

#1 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 07 August 2006 - 09:33 PM

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?

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 07 August 2006 - 09:43 PM

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.
follow me on twitter @PHPsycho

#3 dual_alliance

dual_alliance
  • Members
  • PipPipPip
  • Advanced Member
  • 140 posts
  • LocationNSW, Australia

Posted 07 August 2006 - 09:47 PM

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?

#4 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 07 August 2006 - 09:48 PM

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?

#5 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 07 August 2006 - 10:23 PM

dual_alliance: have a read of the pagination tutorial (http://www.phpfreaks...orials/43/0.php)

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
<?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>";

?>

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....
follow me on twitter @PHPsycho

#6 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 07 August 2006 - 10:50 PM

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

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.
$pagenum = 2;

$start = ($pagenum - 1) * ROWS_PER_PAGE;
$end = ROWS_PER_PAGE;

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.
$query = "select * from blah where blahblah=something limit $start, $end";
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
echo "<a href=\"this.php?page=" . $pagenum - 1 . "\">Previous</a> | <a href=\"this.php?page=". $pagenum + 1 . "\">Next</a>";

Well, something like that anyway.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#7 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 09 August 2006 - 11:34 AM

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?

#8 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 09 August 2006 - 12:05 PM

yep

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

to this.

<?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>";
?>

follow me on twitter @PHPsycho

#9 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 10 August 2006 - 07:28 PM

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

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

it will not load the page.

this is the section it does not like:

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



#10 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 10 August 2006 - 07:35 PM

Can you not just use the techniques discussed in your other thread that deals with almost the same issue?

http://www.phpfreaks...c,103739.0.html

???

#11 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 10 August 2006 - 07:41 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users