Jump to content

[SOLVED] How is it done?


heldenbrau

Recommended Posts

I found some info on the internet to help me and I copied their code into one of my own pages.  I am sure that I have copied it properly.  But what happens is, it only ever shows the first lot of results, no matter what page it is on. 

 

If I click next page, the pagenum goes to 2, but the "<--prev" link doesn't appear and it shows the same database results as it did for page 1.  This is the code:

 

<html> 
<head>
<style type="text/css">@import url(styles/cases.css);</style>
</head>
<body> 

<div id="masthead">
<!--#include file="cases/header.html"-->

</div>

<div id="sidebar">
<!--#include file="cases/sidebar.html"--><br>
</div>

<?php
date_default_timezone_set('Europe/London');

if (!(isset($pagenum)))
{
$pagenum = 1;
}



$time = mktime()-7776000;
$mysqli = new mysqli("localhost", "user", "password", "cases");
    if ($mysqli === false) {
      die("ERROR: Could not connect to database. " . mysqli_connect_error());
    }

$sql="SELECT * FROM cases WHERE lastpost>$time";
if ($result = $mysqli->query($sql)) {
      if ($result->num_rows > 0) {
          $rows = mysqli_num_rows($result);
     }else {echo "No records matching your query were found.";
        }
      }else {
        echo "ERROR: Could not execute $sql . " . $mysqli->error;
        }


$page_rows = 15;
$last = ceil($rows/$page_rows);

if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; 

$sql="SELECT casenum, heading,lastpost FROM cases WHERE lastpost>$time ORDER BY lastpost DESC $max";
    if ($result = $mysqli->query($sql)) {
      if ($result->num_rows > 0) {
        while($row = $result->fetch_array()){
          $date= date("d M Y H:i:s", $row[2]);
          echo"<div id=\"content\">\n
          <a href=\"cases/case$row[0].shtml\"<font size=\"4\">$row[1]</a>  |   Udated $date BST</font>\n
          </div>";
          }
          $result->close();
        }else {
          echo "No records matching your query were found.";
        }
      }else {
        echo "ERROR: Could not execute $sql . " . $mysqli->error;
        }

$mysqli->close();


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> ";
}

//just a spacer
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> ";
}
?>

<div id="footer">
<!--#include file="cases/footer.html" -->
</div>
</body>
</html>

Hi

 

That is only ever going to set it to 1, as it won't be set before the if statement (unless register_globals is on, which is generally strongly recommended against, even though it used to be the standard).

 

What you need is something like:-

 

$pagenum = intval($_REQUEST['pagenum']);
if ($pagenum < 1 )
{
$pagenum = 1;
}

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.