Pagenation help

Discussion in 'Programming' started by izlik, Feb 22, 2014.

  1. #1
    With the code bellow i want to be able to remove the "ID LIMIT 50" and have the code with pagenation instead but i have problems, i have tried some solutions and failed, is there someone that can help me with this please? :/

    
    <div id="block" border="1" width="200" style="float:center">';
    $i = 0;
    $getquery=mysql_query("SELECT * FROM dogs ORDER by ID LIMIT 50");
    while($rows=mysql_fetch_assoc($getquery)){
    
        $id=$rows['id'];
        echo '<a href="/index.php?dogs='. $id .'">
            <img src="/thumbs/'. $id .'.jpg" width="100" height="100"  alt="" />
        </a>';
    
        $i++;
        if($i == 10) {
            echo '<br />';
            echo '<br />';
            $i = 0;
        }
    }
    echo '</div>';
    PHP:
     
    izlik, Feb 22, 2014 IP
  2. Bec0de

    Bec0de Well-Known Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    #2
    You could use something as the following:
    $perpage = 10;
    $currentpage = 0;
    $sql = "SELECT * FROM dogs";
    $numRows = mysql_num_rows(mysql_query($sql));
    $lastpage = ceil($numRows / $perpage);
    $getquery = mysql_query("$sql ORDER by ID LIMIT $currentpage, $perpage");
    ...
    PHP:
    Also, the MySQL extension is deprecated as of PHP 5.5.0 so I strongly suggest you use PDO or MySQLi instead.
     
    Bec0de, Feb 22, 2014 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Hi, i got it to work but, i have a problem with how to switch pages, could you give an example of how i can switch from page 1 to 2 and so one and so on?
    Thank you very much for the help! :)
     
    izlik, Feb 23, 2014 IP
  4. Bec0de

    Bec0de Well-Known Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    #4
    Let's say you have the page number in a GET parameter "page":
    // You can put this at the top
    $page = (isset($_GET['page'])) ? intval($_GET['page']) : 1;
    
    // and replace the $currentpage variable with this one
    $currentpage = ($page - 1) * $perpage;
    
    PHP:
     
    Bec0de, Feb 23, 2014 IP