Hello, I have got a pagination code which can display results according to a category. The problem is On default it is not set to display a category. when i run the script on index.php PHP: it does not display anything but when i run on index.php?cat=1 PHP: it is working. What can i do to display the results from all the categories on index.php Thanks <? //REMEMBER TO CONNECT TO DATABASE! $dbhost = "localhost"; // this will ususally be 'localhost', but can sometimes differ $dbname = "x"; // the name of the database that you are going to use for this project $dbuser = "root"; // the username that you created, or were given, to access your database $dbpass = "x"; // the password that you created, or were given, to access your database mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error()); mysql_select_db($dbname) or die( "Unable to select database"); //**EDIT TO YOUR TABLE NAME, ECT. $t = mysql_query("SELECT * FROM `web` WHERE `cat` = '".addslashes($_GET['cat'])."'"); if(!$t) die(mysql_error()); $a = mysql_fetch_object($t); $total_items = mysql_num_rows($t); $limit = $_GET['limit']; $cat = $_GET['cat']; $page = $_GET['page']; //set default if: $limit is empty, non numerical, less than 10, greater than 50 if((!$limit) || (is_numeric($limit) == false) || ($limit < 10) || ($limit > 50)) { $limit = 10; //default } //set default if: $page is empty, non numerical, less than zero, greater than total available if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) { $cat = 1; $page = 1; //default } //calcuate total pages $total_pages = ceil($total_items / $limit); $set_limit = $page * $limit - ($limit); //query: **EDIT TO YOUR TABLE NAME, ECT. $q = mysql_query("SELECT * FROM `web` WHERE `cat` = '".addslashes($_GET['cat'])."' LIMIT $set_limit, $limit"); if(!$q) die(mysql_error()); $err = mysql_num_rows($q); if($err == 0) die("No matches met your criteria."); //Results per page: **EDIT LINK PATH** echo(" <a href=www.yoursite.com/stuff/script.php?cat=$cat&limit=10&page=1>10</a> | <a href=www.yoursite.com/stuff/script.php?cat=$cat&limit=25&page=1>25</a> | <a href=www.yoursite.com/stuff/script.php?cat=$cat&limit=50&page=1>50</a>"); //show data matching query: while($code = mysql_fetch_object($q)) { echo("item: ".$code->title."<BR>"); } $cat = urlencode($cat); //makes browser friendly //prev. page: **EDIT LINK PATH** $prev_page = $page - 1; if($prev_page >= 1) { echo("<b><<</b> <a href=http://www.yoursite.com/stuff/script.php?cat=$cat&limit=$limit&page=$prev_page><b>Prev.</b></a>"); } //Display middle pages: **EDIT LINK PATH** for($a = 1; $a <= $total_pages; $a++) { if($a == $page) { echo("<b> $a</b> | "); //no link } else { echo(" <a href=http://www.yoursite.com/stuff/script.php?cat=$cat&limit=$limit&page=$a> $a </a> | "); } } //next page: **EDIT THIS LINK PATH** $next_page = $page + 1; if($next_page <= $total_pages) { echo("<a href=http://www.yoursite.com/stuff/script.php?cat=$cat&limit=$limit&page=$next_page><b>Next</b></a> > >"); } //all done ?> PHP:
If understand you correctly, you want to display ALL results irrespective of category, then: remove ---> WHERE `cat` = '".addslashes($_GET['cat']) from all your mysql_query statements
I want to keep WHERE `cat` = '".addslashes($_GET['cat']) because i want to show the results according to categories. But on the main index.php page i want to show the results regardless of which category it is. I want to do something like this whitout "WHERE" for index.php mysql_query("SELECT * FROM `table` LIMIT $set_limit, $limit"); PHP: