spliting in pages (8 records per page)

Discussion in 'PHP' started by Kyriakos, Apr 17, 2008.

  1. #1
    hi,

    i have an online store with some products. i want to split in pages when the page has more than 8 records. how i can do this? this is my php code:
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("storedb", $con);
    
    $result = mysql_query("SELECT * FROM products WHERE cat='".$cat."' AND status=1 ORDER BY pricer");
    ?>
    PHP:
    any idea is acceptable

    thanks in advance.
     
    Kyriakos, Apr 17, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use LIMIT in your query.
    What you want is called pagination. If you do a google search you'll find lots of examples.
     
    CreativeClans, Apr 17, 2008 IP
  3. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    //Copyright Matthew Bell 2008. BSD License. http://www.matthewbell.eu
    function paginate($per_page, $current_page, $before, $after, $total_pages, $get_key) {
            $get = '';
            $output = '';
            if ($current_page <= 0)
                    $current_page = 1;
            if ($total_pages < 1)
                    $total_pages = 1;
            foreach ($_GET as $k=>$v) {
                    if ($k != $get_key) {
                            $get .= $k.'='.$v.'&';
                    }
            }
            $x = $current_page-$before;
            if ($x <= 0)
                    $x = 1;
            for($i=$x;$i<$current_page;$i++) {
                    $output .= '<a href="'.$_SERVER['PHP_SELF'].'?'.$get.$get_key.'='.$i.'">Page '.$i.'</a> ';
            }
            $output .= 'Page '.$current_page. ' of '.$total_pages.' ';
            for($i=$current_page+1;$i<=$current_page+$after && $i <= $total_pages;$i++) {
                    $output .=  '<a href="'.$_SERVER['PHP_SELF'].'?'.$get.$get_key.'='.$i.'">Page '.$i.'</a> ';
            }
            return $output;
    }
    
    PHP:
     
    matthewrobertbell, Apr 17, 2008 IP
  4. adwads.com

    adwads.com Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i really dont know wtf is this
    Kyriakos just use limit in sql query just like CreativeClans said
     
    adwads.com, Apr 17, 2008 IP