Jump to content

Please help with my Pagination code


yandoo

Recommended Posts

Hi there,

 

I was hoping for some expert help & guidance on my current php problem please!!

 

Basically i have gona through the pagination tutorial (very helpful) and am now integrating the pagination with a recordset i have already created.

 

I get the records to display results accordingly and also at the bottom of the page is the "NEXT 6" and "PREVIOUS 6" link (I limited the records to display 6 only per page). The "NEXT 6" and "PREVIOUS 6" are only diaplyed as text though NOT as a hyperlink!

 

I think im very close to getting this to work correctly but am currently racking my brain and pulling out my hair here. If anybody can help id be soooo gr8ful.

 

Please see attachments for a screen print of my page (so you can see how it looks) and below is the code uysed to create it:

 



<?php

    $limit          = 6;               
    $query_count    = "SELECT count(*) FROM teachersname";   
    $result_count   = mysql_query($query_count);   
    $totalrows      = mysql_num_rows($result_count);

    if(empty($page)){
        $page = 1;
    }
       

    $limitvalue = $page * $limit - ($limit);
    $query  = "SELECT * FROM teachersname WHERE " . $_POST['field'] . " LIKE '%$find%'                        LIMIT $limitvalue, $limit";       
    $result = mysql_query($query) or die("Error: " . mysql_error());

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

?>

body,td,th {
   font-family: Arial, Helvetica, sans-serif;
   font-size: 12px;
}
-->
</style></head>

<body>

<span class="border_bottom">

<?php

         //check if the user entered data in the form

if (isset($_POST['find'])) {
     
       //data has been entered so lets search the mofo
   
   echo "<h2>Results</h2><p>";
     
      //sanitise the data

      $find = strtoupper($_POST['find']);
      $find = strip_tags($_POST['find']);
      $find = trim ($_POST['find']);

      //connect to the db
      //mysql_connect("localhost", "root", "winn3rs") or die(mysql_error());
      //mysql_select_db("laptop_loan_database") or die(mysql_error());
      //Now we search for our search term, in the field the user specified
      //$query = "SELECT * FROM teachersname WHERE " . $_POST['field'] . " LIKE '%$find%'"  ;
     
$data = mysql_query($query);
     
     
     
     
          //And we display the results

      while($result = mysql_fetch_array( $data ))
      {   
     
   echo"<table width=\"200\" border=\"0\" class=\"border_bottom\"> \n";
   echo "<tr> \n";

   echo "<td> </td> \n";
   echo "</td> \n";
   echo "</tr> \n";
   echo "<tr> \n";
   echo "<td>";   echo "<strong>Client Name</strong>"; echo "</td>";

    echo "<td width=\"75\" style =\"text-align: left\""; ?> <a href="search_client_details.php?recordID=<?php echo $result['Client']; ?>"><?php echo $result['Client']; ?> [/url] <?php echo " </td> \n";

   echo "</tr> \n";
   echo "<tr> \n";

   echo "<td>";   echo "<strong>Department Code</strong>"; echo "</td>";
   echo "<td>";   echo "";    echo $result['DepartmentCode']; echo ""; echo "</td>";
   
   echo "</tr> \n";
   echo "<tr> \n";
   echo "<td> </td> \n";
   echo "</td> \n";
   echo "</tr> \n";
   echo "</table> \n";
   echo" ";

    }

    if($page != 1){
        $pageprev = $page--;
         
     
        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."[/url] ");
     
    }else{
        echo("PREV".$limit." ");
    }

    $numofpages = $totalrows / $limit;
   
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i[/url] ");
      }
    }

    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i[/url] ");
         echo " ";
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."[/url]");
    }else{
        echo("NEXT".$limit);
      echo "\n";
    }

      ?>
</span>

<?php
      }
     
//This counts the number or results - and if there wasn't any it gives them a little message explaining that

      $anymatches=mysql_num_rows($data);
      if ($anymatches == 0)
      {
      echo "Sorry, but we can not find an entry to match your query";
      }
?>

<?php
mysql_free_result($user_conditional);
?>

 

[attachment deleted by admin]

Link to comment
Share on other sites

Hope this helps, I'm pretty new at this.

 

Try changing these:

 

echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."[/url] ");

echo("<a href=\"$PHP_SELF?page=$i\">$i[/url] ");

echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."[/url]");

 

To these:

 

echo '<a href="'.$PHP_SELF.'&page= '. $pageprev.'">PREV '.$limit.'"</a> ';

echo '<a href="'.$PHP_SELF.'?page='. $i.'"> '.$i.'</a> ';

echo '<a href="'.$PHP_SELF.'?page='. $pagenext.'">NEXT '.$limit.'"</a> ';

 

And make sure to keep the space between the last > and ' or your page numbers will clump together. I'm not sure if this would help either but you might want to try replacing $PHP_SELF with $_SERVER['PHP_SELF']

Link to comment
Share on other sites

Hi & thanks for the reply!

 

The code that you suggested i change to:

 

echo '<a href="'.$PHP_SELF.'&page= '. $pageprev.'">PREV '.$limit.'"</a> ';

 

....is actually the code i am using already, it must have changed slightly when i added to code to post!

 

As for the $_SERVER['PHP_SELF'] bit i did try it and it has had no effect on the page...

 

Any more ideas...?

 

Thank You :)

 

Link to comment
Share on other sites

i found the tut on this site pretty hard to comprehend... found this other one that helped me immensely:

 

 

http://www.webpronews.com/expertarticles/2006/06/20/php-pagination-with-mysql

 

what i ended up with was an include that would let me determine the query string and rows per page, and any URL variables i needed to spit out on my page i want to break up, and then include the pagination script after that. i tried doing it as a function but couldn't get the nav links to print out. anyway, here's what i ended up with:

 

on my page i wanted to break up, i would put this:

 

at the top of the paginated script:

 

  $rowsPerPage = 30; // how many rows 
  $query = "SELECT COUNT(*field*) AS numrows FROM *table* WHERE *parameter* = '".$variable."'"; // query
  $pageLink = "?variable=$parameter&othervariable=$otherparameter&"; // URL variables to pass through to nav links

 

then include the pagination script that uses those variables...

 

inc.paginate:

 

  $numQuery = mysql_query($query) or die(mysql_error()); // mysql query
  $row = mysql_fetch_row($numQuery); // get numrows
  $total_entries = $row[0]; // turn numrows into variable

  if(isset($_GET['page'])) { // if there's a page number
    $page_number = $_GET['page']; // make it a variable
  }
  else { // otherwise 
    $page_number = 1; // page number variable is page 1
  }

  $total_pages = ceil($total_entries/$rowsPerPage); // total pages is total entries divided by rowsperpage

  $offset = ($page_number - 1) * $rowsPerPage; 

  for($page = 1; $page <= $total_pages; $page++) { // thing that turns pages into array
    if($page == $page_number) { // if current page is page number
      $nav .= " $page "; // no need to print out link
    }
    else { // otherwise
      $nav .= " <a href=\"$pageLink"."page=$page\">$page</a> "; // print out links for other pages
    }
  }
  if($total_pages <= 1) { // if total is less than or equal to one
    $navlinks = ""; // $navlinks is nothing
  }
  else { // otherwise
    $navlinks = "<div class=\"center\">Go to page: $nav</div>\n"; // $navlinks is this string
  }

 

then later down the page i would echo my navlinks:

 

    echo $navlinks; // print out the $navlinks created in inc.paginate

 

 

hope that helps any! i know deconstructing mine and really going over it helped me...

Link to comment
Share on other sites

Try printing out your queries and see what they are sending. You place this under the query code

 

print $yourqueryname;

 

and run the page.

 

I had an empty string in a count(*) query that I was able to fix by moving another line of code that was above the only other query I had on the page. That code was working because it was underneath the code I needed to move and the count code was not. Because the count code wasn't working properly my links where not showing up.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.