Rodekode Posted November 13, 2012 Share Posted November 13, 2012 (edited) Hello world Hope somebody can help a noob in php I have a result.php page listing all posts from my database. For each post there is a link to a detail.php page for more info on each specific post. The link goes like this: <a href="detail.php?id=<?php echo $row['id']; ?>">Details</a> In detail.php I have some pagination that is working nicely as long as I dont filter by id. If I add WHERE id=$id to both $sql and $query the id is grabbed nicely but the pagination is lost. Please can somebody tell me how to get the filter by id and the pagination work together? My detail.php looks like this: ****** <?php $id = $_GET['id']; $server = "server"; $user = "user"; $pass = "pass"; $databasename = "databasename"; $db = mysql_connect($server, $user, $pass); mysql_select_db($databasename,$db); $sql = "SELECT * FROM `table`"; $query = mysql_query($sql,$db); $total_results = mysql_num_rows($query); $limit = "1"; $total_pages = ceil($total_results / $limit); if (empty($page)) { $page = "1"; } $offset = ($page - 1) * $limit; $query = "SELECT * FROM `table` LIMIT $offset, $limit"; $result = mysql_query($query); ?> <!DOCTYPE html> <head></head> <body> <?php while ($row = mysql_fetch_array($result)) { ?> <div> Data from base : <?php echo $row['field-01']; ?> and <?php echo $row['field-02']; ?> and so on... </div> <?php } ?> <?php // Previous if ($page != 1) { $prevpage = $page - 1; echo "<a href=$PHP_SELF?page=$prevpage>Previous</a>"; } ?> <?php // Next if ($page != $total_pages) { $nextpage = $page + 1; echo "<a href=$PHP_SELF?page=$nextpage>Next</a>"; } ?> <?php mysql_close(); ?> </body> </html> ****** Any help make me superhappy thanks Edited November 13, 2012 by PFMaBiSmAd removed existing formating to make readable Quote Link to comment https://forums.phpfreaks.com/topic/270628-a-problem-with-pagination/ Share on other sites More sharing options...
thara Posted November 13, 2012 Share Posted November 13, 2012 very difficult to read your post... increase font size and post again. when posting coding use code tag. Quote Link to comment https://forums.phpfreaks.com/topic/270628-a-problem-with-pagination/#findComment-1392007 Share on other sites More sharing options...
Rodekode Posted November 13, 2012 Author Share Posted November 13, 2012 First post ever, so learning there as well Hope this is better: Hello world Hope somebody can help a noob in php I have a result.php page listing all posts from my database. For each post there is a link to a detail.php page for more info on each specific post. The link goes like this: [/size][/font] <a href="detail.php?id=<?php echo $row['id']; ?>">Details</a> In detail.php I have some pagination that is working nicely as long as I dont filter by id. If I add [/size][/font] WHERE id=$id[font='Lucida Grande'][size=2] to both $sql and $query the id is passed nicely but the pagination is lost. Please can somebody tell me how to get the filter by id and the pagination work together? My detail.php looks like this: ****** <?php[/size] [size=4]$id = $_GET['id'];[/size] [size=4]$server = "server"; $user = "user"; $pass = "pass"; $databasename = "databasename"; $db = mysql_connect($server, $user, $pass); mysql_select_db($databasename,$db); $sql = "SELECT * FROM `table`"; $query = mysql_query($sql,$db); $total_results = mysql_num_rows($query); $limit = "1"; $total_pages = ceil($total_results / $limit); if (empty($page)) { $page = "1"; } $offset = ($page - 1) * $limit; $query = "SELECT * FROM `table` LIMIT $offset, $limit"; $result = mysql_query($query); ?>[/size] [size=4]<!DOCTYPE html>[/size] [size=4]<head></head>[/size] [size=4]<body>[/size] [size=4]<?php while ($row = mysql_fetch_array($result)) { ?>[/size] [size=4]<div>[/size] [size=4]Data from base : <?php echo $row['field-01']; ?> and <?php echo $row['field-02']; ?> and so on...[/size] [size=4]</div>[/size] [size=4]<?php } ?>[/size] [size=4]<?php // Previous if ($page != 1) { $prevpage = $page - 1; echo "<a href=$PHP_SELF?page=$prevpage>Previous</a>"; } ?>[/size] [size=4]<?php // Next if ($page != $total_pages) { $nextpage = $page + 1; echo "<a href=$PHP_SELF?page=$nextpage>Next</a>"; } ?>[/size] [size=4]<?php[/size] [size=4]mysql_close(); ?>[/size] [size=4]</body>[/size] [size=4]</html>[/size] [size=4] Quote Link to comment https://forums.phpfreaks.com/topic/270628-a-problem-with-pagination/#findComment-1392008 Share on other sites More sharing options...
thara Posted November 13, 2012 Share Posted November 13, 2012 you are passing post id through result.php page and it gets in details.php page. so how many result come to details.php page with post id? You have mentioned that so many posting in result page and there is a link to details page for more information. so do you need to add pagination to your result page or details page..? Quote Link to comment https://forums.phpfreaks.com/topic/270628-a-problem-with-pagination/#findComment-1392014 Share on other sites More sharing options...
Rodekode Posted November 13, 2012 Author Share Posted November 13, 2012 Hello Thara - thanks for respond Details.php show only one post at a time $limit = "1"; . I need pagination so that visitors do get the option to flip through each detailed post as well Quote Link to comment https://forums.phpfreaks.com/topic/270628-a-problem-with-pagination/#findComment-1392017 Share on other sites More sharing options...
cyberRobot Posted November 13, 2012 Share Posted November 13, 2012 The code doesn't account for the GET variable being passed, so $page is always "1". Instead of if (empty($page)) { $page = "1"; } Try the following: if(isset($_GET['page'])) { $page = $_GET['page']; } else { $page = "1"; } Side Note: I would recommend avoiding PHP_SELF for href attributes. For more information, see Why PHP_SELF Should Be Avoided When Creating Website Links Quote Link to comment https://forums.phpfreaks.com/topic/270628-a-problem-with-pagination/#findComment-1392051 Share on other sites More sharing options...
PFMaBiSmAd Posted November 13, 2012 Share Posted November 13, 2012 See the following post on pagination, using http_build_query, to make the links with any GET parameters that your code might already have along with the ones for pagination - http://forums.phpfreaks.com/topic/268497-pagination/page__hl__+http_build_query#entry1378864 Quote Link to comment https://forums.phpfreaks.com/topic/270628-a-problem-with-pagination/#findComment-1392059 Share on other sites More sharing options...
Rodekode Posted November 13, 2012 Author Share Posted November 13, 2012 Thanks a lot cyberRobot - that was the trick. To make it work I also changed the link-part on result.php. Instead of <a href="detail.php?id=<?php echo $row['id']; ?>">Details</a> I use this: <a href="detail.php?page=<?php echo $row['id']; ?>">Details</a> Also thanks for your kind advice on avoiding PHP_SELF - and so easy to avoid too Quote Link to comment https://forums.phpfreaks.com/topic/270628-a-problem-with-pagination/#findComment-1392060 Share on other sites More sharing options...
Rodekode Posted November 13, 2012 Author Share Posted November 13, 2012 Hello PFMaBiSmAd Thanks for the link. I will take a close look. Quote Link to comment https://forums.phpfreaks.com/topic/270628-a-problem-with-pagination/#findComment-1392063 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.