hakmir Posted October 29, 2008 Share Posted October 29, 2008 I am exhausted. I've been looking for a dynamic pagination sample for decades(a little exaggerated). There are many pagination tutorials and none of them gives a code if it is dynamic. When I use this code everything works perfect. It gives all the results page by page: $query = "SELECT * FROM $tablename"; But when I use this code (which I want) It just shows first page and other pages are empty. It doesn't carry my request to other pages. $query = "SELECT * FROM $tablename WHERE country='$country' AND typeoffood='$typeoffood'"; A lot of people explains like this : The problem is that your posted vars do not carry over from page to page. They are only passed to the page from the initial form submission. To keep them persisting, you need to either make them session variables or else add them to the links to be passed through the url and retrieved via the GET method (like the other vars). If you are using a lot of posted info I would suggest using sessions, as there is a limit on how long a url string can be. Also, sessions are more secure. The problem is I don't know how to make it. All I am looking is a Sample code (for dynamic pagination) I checked the whole internet and not even one person made an example like this. Everybody is making pagination if you select everything from table and if you want dynamic they just give advice. I even saw a lot of people like me and they couldn't solve their problem either. Does anybody know a sample for dynamic pagination and with those session variables stuff in it? Thanks. By the way here is my full code to get results page by page: reciperesults.php <?php ////////////////////////////////////////// //// MySQL Database Connection /////////// ////////////////////////////////////////// $host = "localhost"; $user = "root"; $db_name= "recipetime"; $pass= ""; $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($db_name, $conn) or die(mysql_error()); ///////////////////////////////////////// ////// Prevent Injection Attack ///////// ///////////////////////////////////////// if(isset($_GET['pageno'])) { if(!is_numeric($_GET['pageno'])) { return 'Error: '.$_GET['pageno']; exit(); } $pageno = $_GET['pageno']; } else { $pageno=1; } ///////////////////////////////////////// /////////// PAGES FUNCTION ////////////// ////////////// by Leo /////////////////// /////// www.webdeveloperwannabe.com////// ///////////////////////////////////////// function pages($tablename, $pageno, $perPage, $query) { $output = ''; $queryCount = 'SELECT count(*) FROM '.$tablename; $resultCount = mysql_query($queryCount); $fetch_row = mysql_fetch_row($resultCount); $numrows = $fetch_row[0]; // if there is no results if($numrows == 0) { return 'Query returned 0 results.'; exit(); } $lastpage = ceil($numrows/$perPage); $pageno = (int)$pageno; if($pageno<1) { $pageno=1; } elseif($pageno>$lastpage) { $pageno=$lastpage; } // ----- PAGE LINKS ----- if($pageno==1) { $pages .= 'FIRST | PREVIOUS '; } else { $pages .= "<a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> | "; $prevpage=$pageno-1; $pages .= " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREVIOUS</a> "; } $pages .= ' ( Page '.$pageno.' of '.$lastpage.' ) '; if($pageno==$lastpage) { $pages .= ' NEXT | LAST '; } else { $nextpage = $pageno+1; $pages .= " <a href='".$_SERVER['PHP_SELF']."?pageno=$nextpage'>NEXT</a> | "; $pages .= " <a href='".$_SERVER['PHP_SELF']."?pageno=$lastpage'>LAST</a>"; } $limit=' LIMIT '.($pageno-1)*$perPage.', '.$perPage; $query = $query.$limit; $result = mysql_query($query); if(!$result) { return 'Query failed: '.mysql_error(); } while($row = mysql_fetch_array($result)) { $output .= $row['name']."<br>".$row['country']."<br>".$row['typeoffood']."<br>".$row['yourrecipe']."<br>"."<br>".'<br />'; } $output .= '<div style="width:100%; text-align:center; font-size:smaller; color:#999;">'.$pages.'</div>'; $output .= 'Total number of products: '.$numrows; return $output; exit(); } ///////////////////////////////////////// ////////// Set paramenters ////////////// ///////////////////////////////////////// $typeoffood = $_POST['typeoffood']; $country = $_POST['country']; $tablename = 'recipeform'; $perPage = 4; $query = "SELECT * FROM $tablename WHERE country='$country' AND typeoffood='$typeoffood'"; echo pages($tablename, $pageno, $perPage, $query); ?> my search form: searchrecipe.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $dbcnx = @mysql_connect('localhost', 'root', ''); if (!$dbcnx) { exit('<p>Unable to connect to the ' . 'database server at this time.</p>'); } if (!@mysql_select_db('recipetime')) { exit('<p>Unable to locate the recipe ' . 'database at this time.</p>'); } ?> <form id="form1" name="form1" method="post" action="reciperesults.php"> <label><strong>SEARCH RECIPE</strong><br /> <br /> </label> <p>Country<br /> <label> <select name="country" id="country"> <option>USA</option> <option>CANADA</option> <option>ENGLAND</option> </select> </label> </p> <p>Type of Food <br /> <label> <select name="typeoffood" id="typeoffood"> <option>DESERT</option> <option>ENTREE</option> <option>SPICY</option> </select> </label> </p> <p> <label> <input type="submit" name="Search" id="Search" value="Search" /> </label> </p> </form> <a href="submitrecipe.php">Submit Recipe</a> </body> </html> and sql -- phpMyAdmin SQL Dump -- version 2.11.6 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Oct 29, 2008 at 09:44 AM -- Server version: 5.0.51 -- PHP Version: 5.2.6 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- -- Database: `recipetime` -- -- -------------------------------------------------------- -- -- Table structure for table `recipeform` -- CREATE TABLE `recipeform` ( `name` varchar(255) default NULL, `country` varchar(255) default NULL, `typeoffood` varchar(255) default NULL, `yourrecipe` varchar(1000) default NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `recipeform` -- INSERT INTO `recipeform` (`name`, `country`, `typeoffood`, `yourrecipe`) VALUES ('Hakmir', 'USA', 'DESERT', 'Now I will tell you how to make apple pie'), ('Nick', 'CANADA', 'SPICY', 'Now time to make mexican food....'), ('MIra', 'ENGLAND', 'ENTREE', 'I will show you an entree from England'), ('tavik', 'USA', 'DESERT', 'tuvik lu desert usa'), ('tarik', 'USA', 'DESERT', 'tarikdan tatli usa'), ('now', 'USA', 'DESERT', 'iste simdi gidecek'), ('takita', 'USA', 'DESERT', 'check this out usa desert'), ('hokota', 'USA', 'DESERT', 'hokotadan desert'), ('hokota', 'USA', 'DESERT', 'hokotadan desert'), ('hokota', 'USA', 'DESERT', 'hokotadan desert'), ('hokota', 'USA', 'DESERT', 'hokotadan desert'), ('hokota', 'USA', 'DESERT', 'hokotadan desert'), ('hokota', 'USA', 'DESERT', 'hokotadan desert'), ('hokota', 'USA', 'DESERT', 'hokotadan desert'), ('hokota', 'USA', 'DESERT', 'hokotadan desert'), ('dallama', 'USA', 'DESERT', 'desert from dallama'), ('bude', 'USA', 'DESERT', 'bude'), ('rara', 'USA', 'DESERT', 'rarababa'), ('sat', 'USA', 'DESERT', 'sat'), ('sdfsdf', 'USA', 'DESERT', 'sdfsd'), ('asdssss', 'USA', 'DESERT', 'sssssss'), ('saki', 'USA', 'DESERT', 'saki'), ('sdfdf', 'USA', 'DESERT', 'sdfdf'), ('hey', 'USA', 'DESERT', 'hey'), ('hu', 'USA', 'DESERT', 'hu'), ('hakan2', 'USA', 'DESERT', 'hakan2 lets see if this will work'), ('', 'USA', 'DESERT', ''), ('', 'USA', 'DESERT', ''), ('taka', 'USA', 'DESERT', 'taka\r\n'), ('taka', 'USA', 'DESERT', 'taka\r\n'), ('haki', 'USA', 'DESERT', 'haki'), ('taka', 'USA', 'DESERT', 'taka\r\n'), ('haki', 'USA', 'DESERT', 'haki'), ('soko', 'USA', 'DESERT', 'soko'), ('', 'USA', 'DESERT', ''), ('soko', 'USA', 'DESERT', 'soko'), ('soko', 'USA', 'DESERT', 'soko'), ('soko', 'USA', 'DESERT', 'soko'), ('soko', 'USA', 'DESERT', 'soko'), ('takashi', 'USA', 'DESERT', 'tahashi'), ('saggg', 'USA', 'DESERT', 'saggg'), ('pasha', 'USA', 'DESERT', 'pasha'), ('pasha', 'USA', 'DESERT', 'pasha'), ('pasha', 'USA', 'DESERT', 'pasha'), ('pasha', 'USA', 'DESERT', 'pasha'), ('para', 'USA', 'DESERT', 'para'), ('money', 'USA', 'DESERT', 'money'), ('moneyttt', 'USA', 'DESERT', 'moneyttt'), ('yani', 'USA', 'DESERT', 'yani'), ('', 'USA', 'DESERT', ''), ('', 'USA', 'DESERT', ''), ('', 'USA', 'DESERT', ''), ('', 'USA', 'DESERT', ''), ('', 'USA', 'DESERT', ''), ('This is serious', 'USA', 'DESERT', 'this is serios'), ('hadiya', 'USA', 'DESERT', 'hadiya'), ('This is serious', 'USA', 'DESERT', 'this is serios'), ('hadiya', 'USA', 'DESERT', 'hadiya'), ('fff', 'USA', 'DESERT', 'fff'), ('fffeee', 'USA', 'DESERT', 'fffeee'), ('', 'USA', 'DESERT', ''), ('tttt', 'USA', 'DESERT', 'ttttt'), ('yyy', 'USA', 'DESERT', 'yyy'), ('test', 'USA', 'DESERT', 'test'), ('test', 'USA', 'DESERT', 'test'), ('test1', 'USA', 'DESERT', 'test1'), ('test2', 'USA', 'DESERT', 'test2'), ('test3', 'USA', 'DESERT', 'test3'), ('dfdfdf', 'USA', 'DESERT', 'dfdfdf'), ('gdfd', 'USA', 'DESERT', 'fdfdfd'), ('trtr', 'USA', 'DESERT', 'trtr'), ('yyyy', 'USA', 'DESERT', 'yyyy'), ('rrrr', 'USA', 'DESERT', 'rrrrr'), ('yyyy', 'USA', 'DESERT', 'yyyy'), ('ooooooo', 'USA', 'DESERT', 'oooooooo'), ('pppppppp', 'USA', 'DESERT', 'ppppppppp'), ('qqqqqqqqqq', 'USA', 'DESERT', 'qqqqqqqq'), ('p', 'USA', 'DESERT', 'p'), ('p', 'USA', 'DESERT', 'p'), ('qwqw', 'USA', 'DESERT', 'qwqw'), ('fuku', 'USA', 'DESERT', 'fuku'), ('fuku', 'USA', 'DESERT', 'fuku'), ('saki', 'USA', 'DESERT', 'saki'), ('saki', 'USA', 'DESERT', 'saki'), ('saki', 'USA', 'DESERT', 'saki'), ('saki', 'USA', 'DESERT', 'saki'), ('saki', 'USA', 'DESERT', 'saki'), ('re', 'USA', 'DESERT', 're'), ('yu', 'USA', 'DESERT', 'yu'), ('fdfd', 'USA', 'DESERT', 'dfdfdf'), ('fdfd', 'USA', 'DESERT', 'dfdfdf'), ('yumu', 'USA', 'DESERT', 'yumu'), ('yumu', 'USA', 'DESERT', 'yumu'), ('yumu', 'USA', 'DESERT', 'yumu'), ('fdfd', 'USA', 'DESERT', 'dfdfdf'), ('yumu', 'USA', 'DESERT', 'yumu'), ('89', 'USA', 'DESERT', '89'), ('8999', 'USA', 'DESERT', '89999'), ('8999', 'USA', 'DESERT', '89999'), ('56', 'USA', 'DESERT', '56'), ('erer', 'USA', 'DESERT', 'erer'), ('ty', 'USA', 'DESERT', 'ty'), ('fsdf', 'USA', 'DESERT', 'sdfsdf'), ('fsdf', 'USA', 'DESERT', 'sdfsdf'), ('te', 'USA', 'DESERT', 'te'), ('UI', 'USA', 'DESERT', 'UI'), ('OIOO', 'USA', 'DESERT', 'OIOO'), ('OIOO', 'USA', 'DESERT', 'OIOO'), ('WWW', 'USA', 'DESERT', 'WWW'), ('RERERE', 'USA', 'DESERT', 'RERERE'), ('25', 'USA', 'DESERT', '25'), ('EE', 'USA', 'DESERT', 'EE'), ('98', 'USA', 'DESERT', '565'), ('TEKRAR', 'USA', 'DESERT', 'TEKRAR'), ('fdfdfdfdfdf', 'USA', 'DESERT', 'fdsfsfddfsdfdf'), ('HAKAN', 'USA', 'DESERT', 'HAKAN'), ('den', 'USA', 'DESERT', 'den'), ('SAKINHA', 'USA', 'DESERT', 'SAKINBI KELEKLIK YAPMA BANA'), ('SAKINHA', 'USA', 'DESERT', 'SAKINBI KELEKLIK YAPMA BANA'), ('SAKINHA', 'USA', 'DESERT', 'SAKINBI KELEKLIK YAPMA BANA'), ('SAKINHA', 'USA', 'DESERT', 'SAKINBI KELEKLIK YAPMA BANA'), ('2222', 'USA', 'DESERT', '22222'), ('', 'USA', 'DESERT', '5656'), ('ddR', 'USA', 'DESERT', 'ddR'), ('ddRF', 'USA', 'DESERT', 'ddRF'), ('ddRF', 'ENGLAND', 'SPICY', 'ddRF'); Link to comment https://forums.phpfreaks.com/topic/130625-dynamic-pagination/ Share on other sites More sharing options...
R4nk3d Posted October 29, 2008 Share Posted October 29, 2008 http://www.phpfreaks.com/tutorial/basic-pagination heres a tutorial for you that has some examples. hope it helps. Link to comment https://forums.phpfreaks.com/topic/130625-dynamic-pagination/#findComment-677724 Share on other sites More sharing options...
genericnumber1 Posted October 29, 2008 Share Posted October 29, 2008 And they were right, it really is a simple change <?php ////////////////////////////////////////// //// MySQL Database Connection /////////// ////////////////////////////////////////// $host = "localhost"; $user = "root"; $db_name= "recipetime"; $pass= ""; $conn = mysql_connect($host, $user, $pass) or die(mysql_error()); mysql_select_db($db_name, $conn) or die(mysql_error()); ///////////////////////////////////////// ////// Prevent Injection Attack ///////// ///////////////////////////////////////// if(isset($_GET['pageno'])) { if(!is_numeric($_GET['pageno'])) { return 'Error: '.$_GET['pageno']; exit(); } $pageno = $_GET['pageno']; } else { $pageno=1; } ///////////////////////////////////////// /////////// PAGES FUNCTION ////////////// ////////////// by Leo /////////////////// /////// www.webdeveloperwannabe.com////// ///////////////////////////////////////// function pages($tablename, $pageno, $perPage, $query, $urlAppend) { $output = ''; $queryCount = 'SELECT count(*) FROM '.$tablename; $resultCount = mysql_query($queryCount); $fetch_row = mysql_fetch_row($resultCount); $numrows = $fetch_row[0]; // if there is no results if($numrows == 0) { return 'Query returned 0 results.'; exit(); } $lastpage = ceil($numrows/$perPage); $pageno = (int)$pageno; if($pageno<1) { $pageno=1; } elseif($pageno>$lastpage) { $pageno=$lastpage; } // ----- PAGE LINKS ----- if($pageno==1) { $pages .= 'FIRST | PREVIOUS '; } else { $pages .= "<a href='{$_SERVER['PHP_SELF']}?pageno=1$urlappend'>FIRST</a> | "; $prevpage=$pageno-1; $pages .= " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage$urlappend'>PREVIOUS</a> "; } $pages .= ' ( Page '.$pageno.' of '.$lastpage.' ) '; if($pageno==$lastpage) { $pages .= ' NEXT | LAST '; } else { $nextpage = $pageno+1; $pages .= " <a href='".$_SERVER['PHP_SELF']."?pageno=$nextpage$urlappend'>NEXT</a> | "; $pages .= " <a href='".$_SERVER['PHP_SELF']."?pageno=$lastpage$urlappend'>LAST</a>"; } $limit=' LIMIT '.($pageno-1)*$perPage.', '.$perPage; $query = $query.$limit; $result = mysql_query($query); if(!$result) { return 'Query failed: '.mysql_error(); } while($row = mysql_fetch_array($result)) { $output .= $row['name']."<br>".$row['country']."<br>".$row['typeoffood']."<br>".$row['yourrecipe']."<br>"."<br>".'<br />'; } $output .= '<div style="width:100%; text-align:center; font-size:smaller; color:#999;">'.$pages.'</div>'; $output .= 'Total number of products: '.$numrows; return $output; exit(); } ///////////////////////////////////////// ////////// Set paramenters ////////////// ///////////////////////////////////////// $typeoffood = $_GET['typeoffood']; $country = $_GET['country']; $urlAppend = "&typeoffood=$typeoffood&country=$country"; $tablename = 'recipeform'; $perPage = 4; $query = "SELECT * FROM $tablename WHERE country='$country' AND typeoffood='$typeoffood'"; echo pages($tablename, $pageno, $perPage, $query, $urlAppend); ?> I changed your POST to GET, and added the $urlAppend variable, search for it and follow it... see what I did Also, change your form to get <form id="form1" name="form1" method="get" action="reciperesults.php"> Link to comment https://forums.phpfreaks.com/topic/130625-dynamic-pagination/#findComment-677730 Share on other sites More sharing options...
genericnumber1 Posted October 29, 2008 Share Posted October 29, 2008 Also, I introduced a bug find it! (since I can't edit, and just telling you the fix wouldn't be any fun) Link to comment https://forums.phpfreaks.com/topic/130625-dynamic-pagination/#findComment-677747 Share on other sites More sharing options...
hakmir Posted October 29, 2008 Author Share Posted October 29, 2008 Thanks a lot guys. I really appreciate. genericnumber1, I made the changes you said but problem is still there. I forgot to tell about another problem. I don't know if it causes this problem. It always shows the number of pages of everything in the table not the number of pages that I selected from form. $queryCount = 'SELECT count(*) FROM '.$tablename; Link to comment https://forums.phpfreaks.com/topic/130625-dynamic-pagination/#findComment-677792 Share on other sites More sharing options...
hakmir Posted October 29, 2008 Author Share Posted October 29, 2008 I realized something. First page url is like this: http://localhost/Copy_of_src/reciperesults.php?country=USA&typeoffood=DESERT&Search=Search When I go to second page URL is like this and of course there is nothing on the page http://localhost/Copy%20of%20src/reciperesults.php?pageno=2 Then I started to play with URL and came up with this which is working: http://localhost/Copy%20of%20src/reciperesults.php?pageno=2&country=USA&typeoffood=DESERT&Search=Search Problem is the same, I don't know what or how to do to get this result everytime when I clicked next or try to go to another page. I feel I am so close to result. I can even smell it but Still I can not touch it. Link to comment https://forums.phpfreaks.com/topic/130625-dynamic-pagination/#findComment-677826 Share on other sites More sharing options...
ionik Posted October 29, 2008 Share Posted October 29, 2008 its not safe to include Get vars in your db query Link to comment https://forums.phpfreaks.com/topic/130625-dynamic-pagination/#findComment-677829 Share on other sites More sharing options...
genericnumber1 Posted October 29, 2008 Share Posted October 29, 2008 its not safe to include Get vars in your db query or post vars (though I'm sure you meant that as well). You should sanitize all input... but if I told that to everyone on here with sql injection vulnerabilities, I'd never do anything else. Link to comment https://forums.phpfreaks.com/topic/130625-dynamic-pagination/#findComment-677983 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.