Pagination according to a category

Discussion in 'PHP' started by baris22, Sep 9, 2009.

  1. #1
    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&amp;limit=10&amp;page=1>10</a> | 
    <a href=www.yoursite.com/stuff/script.php?cat=$cat&amp;limit=25&amp;page=1>25</a> | 
    <a href=www.yoursite.com/stuff/script.php?cat=$cat&amp;limit=50&amp;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>&lt;&lt;</b> <a href=http://www.yoursite.com/stuff/script.php?cat=$cat&amp;limit=$limit&amp;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&amp;limit=$limit&amp;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&amp;limit=$limit&amp;page=$next_page><b>Next</b></a> &gt; &gt;"); 
    } 
    
    //all done 
    ?>
    
    PHP:
     
    baris22, Sep 9, 2009 IP
  2. anwaraa

    anwaraa Active Member

    Messages:
    167
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    Digital Goods:
    2
    #2
    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
     
    anwaraa, Sep 9, 2009 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    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:
     
    baris22, Sep 9, 2009 IP
  4. innovatewebs

    innovatewebs Well-Known Member

    Messages:
    194
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    I can do this kind of paging for your page please contact me for price.

    [​IMG]
     
    innovatewebs, Sep 9, 2009 IP