Woodburn2006 Posted August 7, 2006 Share Posted August 7, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/ Share on other sites More sharing options...
ToonMariner Posted August 7, 2006 Share Posted August 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-70842 Share on other sites More sharing options...
dual_alliance Posted August 7, 2006 Share Posted August 7, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-70844 Share on other sites More sharing options...
Woodburn2006 Posted August 7, 2006 Author Share Posted August 7, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-70846 Share on other sites More sharing options...
ToonMariner Posted August 7, 2006 Share Posted August 7, 2006 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.... Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-70853 Share on other sites More sharing options...
king arthur Posted August 7, 2006 Share Posted August 7, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-70866 Share on other sites More sharing options...
Woodburn2006 Posted August 9, 2006 Author Share Posted August 9, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-71715 Share on other sites More sharing options...
ToonMariner Posted August 9, 2006 Share Posted August 9, 2006 yepchange this bit...[code]<?phpecho "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=" . $prev . "\">previous</a>";echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?id=" . $next . "\">next</a>";?>[/code]to this.[code]<?phpif ($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] Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-71732 Share on other sites More sharing options...
Woodburn2006 Posted August 10, 2006 Author Share Posted August 10, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-72681 Share on other sites More sharing options...
HeyRay2 Posted August 10, 2006 Share Posted August 10, 2006 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 ??? Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-72685 Share on other sites More sharing options...
Woodburn2006 Posted August 10, 2006 Author Share Posted August 10, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16835-previous-and-next/#findComment-72688 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.