1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

paging alternative

Discussion in 'PHP & Perl' started by imperial109, Dec 22, 2009.

Tags:
  1. imperial109

    imperial109 Regular Member

    Joined:
    Jan 19, 2009
    Messages:
    499
    Likes Received:
    361
    I don't like the 'standard' paging scripting that everyone suggests so I made my own.

    It's set to display only 10 results per page and is for an image gallery. However, you don't have to update it to match the number of rows in your database. Suggestions about how to make it more simple/dynamic are welcome.

    PHP:
    ///get primary values
    $gallery $_REQUEST['gallery']; //define what type of images
    $page $_REQUEST['page']; //the page that you are on
     
    //set parameters for results on one page
    $offsetx 0;
    $rowsy 10;
    //if on the first page, we want to display the very first result in the table
    //to get next 10, offsetx needs to be able to multiply
    if ($page != 1) {$offsetx 1
    $offsetx $offsetx 10;}
     
    //query for total number of rows
    $query "SELECT * FROM `images` WHERE `type`='$gallery'";
    $result mysql_query($query);
    $rows mysql_num_rows($result);
    //round up rows to multiple of 10 then divide by 10 to get number of pages
    $roundup ceil(intval($rows)/10)*10;
    $t $roundup/10;
    $p 1;
     
    //display pages in chronological order 1,2,3,... 
    while ($t >= 1) {
    echo 
    "<a href=\"gallery.php?gallery=$gallery&page=$p\">$p</a> ";
    $p++;
    $t--;
    }
     
    //query for images and show only 10 results per page
    $query "SELECT * FROM `images` WHERE `type`='$gallery' LIMIT $offsetx,$rowsy";
    $result mysql_query($query);
    //display gallery thumbnails
    while($row=mysql_fetch_array($resultMYSQL_ASSOC)) {
    $id $row['id'];
    $img $row['image'];
    echo 
    "<a href=\"gallery.php?view=$id\"><img src=\"$img\" border=0 height=150 width=150></a>";
    }
     
  2. XoC--

    XoC-- Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 5, 2009
    Messages:
    211
    Likes Received:
    113
    Hmm, make sure you check the variables going into the MySQL query.
    Example:
    Code:
    if(!preg_match("[0-9]{2}", $page)) $error = 1
    $page = mysql_real_escape_string($page)
    
    Otherwise you could face a possible headache :p