www.webdeveloper.com
Results 1 to 3 of 3

Thread: Pagination problem for where cluase

  1. #1
    Join Date
    Aug 2012
    Posts
    2

    Pagination problem for where cluase

    Hello,
    I am facing one problem of pagination.
    Actually I did pagination successfully,but problem is getting by where clause
    I used SELECT query with WHERE clause.Using input field.
    Now the problem is that "while choosing the page of that pagination I have to again choose that input fields which used in the where clause.
    Here is my piece of code :
    Collapse | Copy Code

    <?php
    $i = 0;

    if(!empty($_POST['select2']))
    {
    foreach ($_POST['select2'] as $selectedOption)
    {
    $options[$i++] = $selectedOption;
    }
    }

    if (isset($_GET['pageno'])) {
    $pageno = $_GET['pageno'];
    } else {
    $pageno = 1;
    }
    $sql ="select DISTINCT date,mobno,city,state,type,telecaller,time FROM import";
    $query = mysql_query($sql);
    $query_data = mysql_num_rows($query);

    $numrows = $query_data;
    $rows_per_page = 10;
    $lastpage = ceil($numrows/$rows_per_page);
    $pageno = (int)$pageno;
    if ($pageno > $lastpage) {
    $pageno = $lastpage;
    }
    if ($pageno < 1) {
    $pageno = 1;
    }
    $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
    $sql = "select date,mobno,city,state,type,telecaller FROM import WHERE time IN(";
    $num = count($options);
    for ($i=0; $i<$num-1; $i++)
    {
    $sql .= "'".$options[$i]."', ";
    }
    $sql .= "'".$options[$i]."')";
    $sql .= "GROUP BY mobno,telecaller ORDER BY date DESC $limit";
    // OR date1='$_POST[date]'
    //echo $sql . "<br>";
    $query = mysql_query($sql);
    echo"<div id='pagination'>";
    if ($pageno == 1) {
    echo " FIRST PREV &nbsp &nbsp &nbsp ";
    } else {
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> &nbsp &nbsp &nbsp ";
    $prevpage = $pageno-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> &nbsp &nbsp &nbsp ";
    }
    echo"</div>";
    echo"<div id='pagination1'>";
    echo " ( Page <b>$pageno</b> of $lastpage ) &nbsp &nbsp &nbsp";
    echo"</div>";
    echo"<div id='pagination2'>";
    if ($pageno == $lastpage) {
    echo " NEXT LAST &nbsp &nbsp &nbsp";
    } else {
    $nextpage = $pageno+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> &nbsp &nbsp &nbsp";
    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
    }
    echo"</div>";

  2. #2
    Join Date
    Nov 2006
    Posts
    526
    I'm seeing some things that just don't look right in that code. For example the query to get the total records is not using the WHERE parameter to get the correct record total based upon the filter criteria. But the solution to your problem is simple. If the user passes data to be used to filter the records then you need to same those parameters and use them on subsequent page loads. Here is some sample code you can try:

    PHP Code:
    <?php

    session_start
    ();

    $records_per_page 10;

    function 
    parseOptions($opt)
    {
        
    $opt mysql_real_escape_string($opt);
        return 
    "'{$opt}'";
    }

    if(!empty(
    $_POST['select2']))
    {
        
    $options array_map('parseOptions'$_POST['select2']);
        
    $_SESSION['options'] = $options;
    }
    elseif(isset(
    $_SESSION['options']))
    {
        
    $options $_SESSION['options'];
    }

    //Create WHERE clause
    $WHERE = (isset($options)) ? "WHERE time IN (" implode(', '$options) . ")" '';

    //Get total record count
    $query ="SELECT COUNT(*)
             FROM import
             
    {$WHERE}";
    $result mysql_query($query);
    $total_records mysql_result($result0);

    //Calculate total pages
    $total_pages ceil($total_records $records_per_page);

    //Set page number
    $pageno = (isset($_GET['pageno'])) ? intval($_GET['pageno']) : 1;
    $pageno min(max($pageno1), $total_pages);

    //Run query to get records for selected page
    $LIMITSTART = ($pageno 1) * $records_per_page;
    $query "SELECT date, mobno, city, state, type, telecaller
              FROM import
              
    {$WHERE}
              GROUP BY mobno,telecaller
              ORDER BY date DESC
              LIMIT  
    $LIMITSTART$records_per_page";
    $result mysql_query($query);

    //Create page navigation
    echo"<div id='pagination'>";
    if (
    $pageno == 1)
    {
        echo 
    " FIRST PREV &nbsp &nbsp &nbsp ";

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

    echo
    "<div id='pagination1'>";
    echo 
    " ( Page <b>$pageno</b> of $total_pages ) &nbsp &nbsp &nbsp";
    echo
    "</div>";

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

  3. #3
    Join Date
    Aug 2012
    Posts
    2
    huh,it works thanks.. but can you please explain me the above coding??

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles