FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Display Previous and Next buttons not working





welshsteve
Hi everyone. I'm a complete beginner when it comes to PHP. I've found and implemented some page for displaying results of a MySQL query in tabular format, paged in groups of 15. The problem I am having is getting the "Previous Page" button to appear on pages in the middle of the query.

For example, if a query produces 35 records, and I set the paging to be 15 per page, the second page will display records between 16 and 30. But I can only get the "Next" button to appear on this page, not the "Previous Page" button as well. The "Previous Page" button only appears on the last page of the query.

Here's my code:

snookerbreaks_view.php
Code:

<?php

mysql_connect ("localhost","USERNAME","PASSWORD");
mysql_select_db ("DBNAME");

$number_of_links_per_page = 15;

if (!$_REQUEST[all_links]){
    $all_links = '0';
}else{
    $all_links = $_REQUEST[all_links];
}

$comma = ",";
$color = "#eeeeee";
if ($_POST["offset"]){
   $offset = $_POST[offset];
   $limitq = "Limit " . $offset . $comma . $number_of_links_per_page;
} else {
   $offset = '0';
    $limitq = "Limit " . $offset . $comma . $number_of_links_per_page;
}

//sql_links2 is for the Previous and next link generator
switch ($_POST['division']) {
    case "1":
        $division = "Breaks_Prem";
        break;
    case "2":
        $division = "Breaks_A";
        break;
    default:
        $division = "Breaks_Prem";
       
}

$sql_links = mysql_query("SELECT * FROM $division ORDER BY Break DESC , Name , Club $limitq") or die("Query Error:".mysql_error());
$sql_links2 = mysql_query("SELECT * FROM $division ORDER BY Break DESC , Name , Club") or die("Query Error:".mysql_error());
$num_links =  mysql_num_rows($sql_links);
       // if offset is 0 then use the num_links2, if offset
       //is more than 0 it just by passes it.lent
       if($offset == 0){
          $all_links =    mysql_num_rows($sql_links2);
          }
          // assign the outer result range number
               $resultmax = $offset + $number_of_links_per_page;
             if ( $resultmax > $all_links ){
                  $resultmax = $all_links;
                          }
             $firstrowonpage = $offset + 1;
             
echo "<p><b>Total: $all_links &nbsp;&nbsp;&nbsp;Showing Results $firstrowonpage to $resultmax</b></p>";
echo "<table style='padding:5px;border: 1px solid #000;border-collapse:collapse;'>";
echo "<tr>";
echo "<th style='background-color:#0a0;color:#fff;width:150px;border: 1px solid #006001;'>Name</th>";
echo "<th style='background-color:#0a0;color:#fff;width:200px;border: 1px solid #006001;'>Club</th>";
echo "<th style='background-color:#0a0;color:#fff;width:50px;border: 1px solid #006001;'>Break</th>";
echo "</tr>";

for ($i=0; $i<$num_links; $i++){
   $row = mysql_fetch_array($sql_links);
    $Name = $row["Name"];
    $Club = $row["Club"];
    $Break = $row["Break"];

    echo"<tr>";
    echo"<td style='text-align:left;border: 1px solid #006001;'>$Name</td>";
    echo"<td style='text-align:left;border: 1px solid #006001;'>$Club</td>";
    echo"<td style='text-align:left;border: 1px solid #006001;'>$Break</td>";
    echo"</tr>";
}
echo"</table>";
// Previos and next link generator
$pagestoshow = $all_links - $number_of_links_per_page;
// MULTIPAGE - if present offset is less than the number of the returned rows then do multipage
    if ( $offset < $pagestoshow ){
        $displayNextButton = "true";
        // here the offset is incremented to point to the next set of results desired
        $offset = ($offset + $number_of_links_per_page);
      include("nextpage.php");
      } else {
        if ($offset > 15){
            echo"<br /><input class=\"box\" type=\"button\" name=\"goBack\" value=\"Previous Page\" onclick=\"history.back(1)\">";
        }
    }
?>



nextpage.php
Code:

<?
echo"<form name=\"Nextpage\" method=\"post\" action=\"".$_SERVER['SCRIPT_NAME']."\">\n";

if ( $displayNextButton == "true" ){
 echo "<br /><input type=\"submit\" name=\"submit\" value=\"Next Page\"\n";
}
echo"<input type=\"hidden\" name=\"offset\" value=\"$offset\">\n";
echo"<input type=\"hidden\" name=\"all_links\" value=\"$all_links\">\n";
echo"</form>\n";
?>



Can anybody help at all? I don't understand enough of what the code is doing to troubleshoot it.
TheForgottenArts
If you don't actually understand most of the PHP, why not actually try and learn PHP and trouble shoot it yourself?
welshsteve
Yeah thanks for that. That really helps!

I AM learning PHP, but need to get this code working ASAP so thought I'd "ask the experts" so to speak.
TheForgottenArts
I think you'll have a problem on the "nextpage.php".

Code:

<?
echo"<form name=\"Nextpage\" method=\"post\" action=\"".$_SERVER['SCRIPT_NAME']."\">\n";

if ( $displayNextButton == "true" ){
 echo "<br /><input type=\"submit\" name=\"submit\" value=\"Next Page\"\n";
}
echo"<input type=\"hidden\" name=\"offset\" value=\"$offset\">\n";
echo"<input type=\"hidden\" name=\"all_links\" value=\"$all_links\">\n";
echo"</form>\n";
?>


Where does it define the variable "$displayNextButton"? It doesn't :X
Related topics
Hotmail accounts at risk
Pharming VOiP
My Laptop prblm
Which Firefox Extentions?
do you like the look of my site?
So I'm pretty new at this whole website thing
Did you use this script ? try it
Your new favourite site!
[Flash] Corporate Menu 200 frih$
Actionscript Problem: array failure?
Windows XP Media Centre edition problembs
Some funny javascript
Dell Inspiron 1420 - Vista multimedia buttons problem
What is eating my memory???
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.