SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with pagination.

    Hello I have the following code for pagination. buts when i click on the next or last button its showing no record found. can any1 help me pls??

    <?php
    include "dbconnect.php";
    $link=dbconnect();
    $pc=$_POST['pc'];
    if (!(isset($pagenum)))
    {
    $pagenum = 1;
    }

    $query=("select *from doctors WHERE pc='$pc';");
    $resultt=mysql_query($query);
    $rows = mysql_num_rows($resultt);
    if(!$rows)
    {
    echo"<script>alert (\"Sorry No Record Found!!!\");history.go(-1)</script>";
    break;
    }
    else
    {
    $page_rows = 3;
    $last = ceil($rows/$page_rows);
    if ($pagenum < 1)
    {
    $pagenum = 1;
    }
    elseif ($pagenum > $last)
    {
    $pagenum = $last;
    }
    $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
    $result=mysql_query("select *from doctors WHERE pc='$pc' $max;");



    while ($row=mysql_fetch_array($result))
    {
    echo $row["title"]."&nbsp;"."&nbsp;". $row["name"];
    }
    echo " --Page $pagenum of $last-- <p>";
    if ($pagenum == 1)
    {
    }
    else
    {
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
    echo " ";
    $previous = $pagenum-1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
    }
    echo " ---- ";
    if ($pagenum == $last)
    {
    }
    else {
    $next = $pagenum+1;
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
    echo " ";
    echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
    }
    ?>

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    1) when you click on the next or last button, no 'pc' value is passed, so $_POST['pc'] will not be set, and your first query won't find any rows.
    2) you pass 'pagenum' through the url, but never use $_GET['pagenum'] anywhere. $pagenum does not automatically contain the passed value (at least it won't if register_globals is OFF, as it should be).

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    1) when you click on the next or last button, no 'pc' value is passed, so $_POST['pc'] will not be set, and your first query won't find any rows.
    2) you pass 'pagenum' through the url, but never use $_GET['pagenum'] anywhere. $pagenum does not automatically contain the passed value (at least it won't if register_globals is OFF, as it should be).
    Can u pls help me how can i fix it?

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code PHP:
    <?php
    include "dbconnect.php";
    $link=dbconnect();
    $pc = '';
    if (isset($_POST['pc'])) {
      $pc = $_POST['pc'];
    } else {
      if (isset($_GET['pc'])) {
        $pc = $_GET['pc'];
      }
    }
    if (isset($_GET['pagenum'] && ((int)$_GET['pagenum'] > 0)) {
      $pagenum = (int)$_GET['pagenum'];
    } else {
      $pagenum = 1;
    } 
     
    $query=("select *from doctors WHERE pc='$pc'") or die(mysql_error());
    $resultt=mysql_query($query);
    $rows = mysql_num_rows($resultt);
    if(!$rows) {
      echo"<script>alert (\"Sorry No Record Found!!!\");history.go(-1)</script>";
      break;
    } else {
      $page_rows = 3;
      $last = ceil($rows/$page_rows);
      if ($pagenum < 1) {
        $pagenum = 1;
      } elseif ($pagenum > $last) {
        $pagenum = $last;
      }
      $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
      $result=mysql_query("select *from doctors WHERE pc='$pc' $max;") or die(mysql_error());
     
      while ($row=mysql_fetch_array($result)) {
        echo $row["title"]."&nbsp;"."&nbsp;". $row["name"];
      }
      echo " --Page $pagenum of $last-- <p>";
      if ($pagenum != 1) {
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
        echo " ";
        $previous = $pagenum-1;
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
      }
      echo " ---- ";
      if ($pagenum != $last) {
        $next = $pagenum+1;
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
        echo " ";
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
      }
    }
    ?>

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Code PHP:
    <?php
    include "dbconnect.php";
    $link=dbconnect();
    $pc = '';
    if (isset($_POST['pc'])) {
      $pc = $_POST['pc'];
    } else {
      if (isset($_GET['pc'])) {
        $pc = $_GET['pc'];
      }
    }
    [COLOR="Red"]if (isset($_GET['pagenum'] && ((int)$_GET['pagenum'] > 0))[/COLOR] {
      $pagenum = (int)$_GET['pagenum'];
    } else {
      $pagenum = 1;
    } 
     
    $query=("select *from doctors WHERE pc='$pc'") or die(mysql_error());
    $resultt=mysql_query($query);
    $rows = mysql_num_rows($resultt);
    if(!$rows) {
      echo"<script>alert (\"Sorry No Record Found!!!\");history.go(-1)</script>";
      break;
    } else {
      $page_rows = 3;
      $last = ceil($rows/$page_rows);
      if ($pagenum < 1) {
        $pagenum = 1;
      } elseif ($pagenum > $last) {
        $pagenum = $last;
      }
      $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
      $result=mysql_query("select *from doctors WHERE pc='$pc' $max;") or die(mysql_error());
     
      while ($row=mysql_fetch_array($result)) {
        echo $row["title"]."&nbsp;"."&nbsp;". $row["name"];
      }
      echo " --Page $pagenum of $last-- <p>";
      if ($pagenum != 1) {
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
        echo " ";
        $previous = $pagenum-1;
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
      }
      echo " ---- ";
      if ($pagenum != $last) {
        $next = $pagenum+1;
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
        echo " ";
        echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
      }
    }
    ?>
    if (isset($_GET['pagenum'] && ((int)$_GET['pagenum'] > 0)) line creating the following error:

    Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')'

    I m not sure where is the problem. can u pls help


Bookmarks

Posting Permissions

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