Problems with pagination

Discussion in 'PHP' started by PinkyM, Nov 8, 2008.

  1. #1
    Hello,

    I have a problem with my pagination code.

    Have table ITEMS (table wher all the adverts are kept)
    Related table IMAGES (Image paths + ref id the image belongs to )

    variable $id is a category.
    If the id is 0 all the adverts are displayed regardless the category. (view all)- pagination works fine here.

    When $id is something other than 0, could be 1- Accessories, 2- Antiques, etc., I have highlited in red,where the problem is it only needs to bring back the relevant rows and paginate those. I really don't know how to do this.

    Would anyone please be able to help???

    Many Thanks.

    Pinky M.


    <?php
    //variable for cat
    $id= $_GET['cat_id'];

    ?>

    <?php

    //database connection
    require('db_connect.php');


    //get the number of the page
    if (isset($_GET['pageno'])) {
    $pageno = $_GET['pageno'];
    } else {
    $pageno = 1;
    }


    // select details from table items
    if ($id==0) {
    $query = "SELECT *, items.Location as place FROM items left outer join images on items.RefID= images.Ref_ID order by timeStamp DESC";
    } else {
    $query = "SELECT *, items.Location as place FROM items left outer join images on items.RefID= images.Ref_ID where cat_id= $id order by timeStamp DESC";
    }
    $result = mysql_query($query);



    //return all rows
    $num_rows = mysql_num_rows($result);


    //calculate number of last page
    $rows_per_page = 2;
    $lastpage = ceil($num_rows/$rows_per_page);


    //check that value of pagenumber is between 1 and last page
    $pageno = (int)$pageno;
    if ($pageno > $lastpage) {
    $pageno = $lastpage;
    }
    if ($pageno < 1) {
    $pageno = 1;
    }


    // range to display in the query
    $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;




    if ($id==0) {
    $query = "SELECT *, items.Location as place FROM items left outer join images on items.RefID= images.Ref_ID order by timeStamp DESC $limit";
    } else {

    $query = "SELECT *, items.Location as place FROM items left outer join images on items.RefID= images.Ref_ID where cat_id= $id order by timeStamp DESC $limit";
    }

    $result = mysql_query($query);



    /*echo $result;*/

    /*

    //return all rows
    $num_rows = mysql_num_rows($result);

    */


    //print how many records are there

    echo "There are $num_rows record(s) under this category.<P>";


    //set up a table
    echo "<table width=200 border=1>\n";

    // set the row of data from database as variable and while row found
    while ($field = mysql_fetch_assoc($result)){
    echo "<tr>\n";

    //retreive row and print into the table
    //foreach ($get_info as $field)
    $title=$field['Title'];
    $desc=$field['Description'];
    $loc= $field['place'];
    $time= $field['timeStamp'];
    $image= $field['Location'];
    $defimage = "img/default.gif";



    echo "<td><font face=arial size=1/>$title</font></td>";
    echo "<td><font face=arial size=1/>$desc</font></td>";
    echo "<td><font face=arial size=1/>$loc</font></td>";
    echo "<td><font face=arial size=1/>$time</font></td>";
    If ($image==NULL) {
    echo '<td><font face=arial size=1/><img src="'. $defimage. '"width=100&amp;height=100&amp/></font></td>';
    }
    else {
    echo '<td><font face=arial size=1/><img src="'. $image. '" width=100&amp;height=100&amp/></font></td>';
    }
    echo "</tr>\n";

    }

    echo "</table>\n";

    if ($pageno == 1) {
    echo " FIRST PREV ";
    } else {
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
    $prevpage = $pageno-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
    }

    echo " ( Page $pageno of $lastpage ) ";


    if ($pageno == $lastpage) {
    echo " NEXT LAST ";
    } else {
    $nextpage = $pageno+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
    }



    ?>
     
    PinkyM, Nov 8, 2008 IP