Jump to content

[SOLVED] Problem with Previous/Next result display script


patheticsam

Recommended Posts

Hi! I have a search script that will query a mySQL table and split the number of results into a number of pages (ex: 20 results per page).

 

The script is almost working fine since I get the results and the correct number of pages based on the number of results. . .

 

The only thing is can't go on the next page or any other pages except the first one....

 

Here's my code.... if anyone knows what I'm doing wrong :

 

<?php
$server = "localhost";
$user = "user";
$pass = "pass";
$databasename = "table";
$db = mysql_connect($server, $user, $pass);
mysql_select_db($databasename,$db);

  $sql = "SELECT * FROM annonccom WHERE (titre LIKE '%$search%')";
  $query = mysql_query($sql,$db);
  $total_results = mysql_num_rows($query);
  $limit = "5"; //limit of archived results per page.
  $total_pages = ceil($total_results / $limit); //total number of pages
if (empty($page))
{
  $page = "1"; //default page if none is selected
}
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB

$query = "SELECT * FROM annonccom WHERE (titre LIKE '%$search%') LIMIT $offset, $limit";
$result = mysql_query($query);
//This is the start of the normal results...

while ($row = mysql_fetch_array($result))
  {
  
echo "
<table style=\"border: 1px solid #000000\" cellpadding=4 cellspacing=0 width=600>
<tr>
<td bgcolor=D8D8D8 width=450><b><a href=\"annonce.php?cmd=view&id=$row[id]\">$row[titre]</a></b></td>
<td bgcolor=D8D8D8 width=150 align=right><div class=red>Prix : $row[prix] $</div></td>
</tr>
<tr>
<td width=450>$row[descrp]</td>
<td width=150 align=center><a href=\"annonce.php?cmd=view&id=$row[id]\"><IMG alt=\"\" src=\"thumbs.php?src=/uploads/$row[picture]&h=140&w=140&zc=1\" border=0> </a></td>
</tr>
</table><br>
</td>
<tr>
</table>
        ";


  }
  mysql_close();

// This is the Previous/Next Navigation
echo "<center>";
echo "Pages:($total_pages)  "; // total pages
if ($page != 1)
{
echo "<a href=$PHP_SELF?page=1><< First</a>   "; // First Page Link
$prevpage = $page - 1;
echo " <a href=$PHP_SELF?page=$prevpage><<</a> "; // Previous Page Link
}
     if ($page == $total_pages) 
   {
         $to = $total_pages;
      } 
  elseif ($page == $total_pages-1) 
   {
         $to = $page+1;
      } 
  elseif ($page == $total_pages-2) 
   {
         $to = $page+2;
      } 
  else 
   {
         $to = $page+3;
      }
     if ($page == 1 || $page == 2 || $page == 3) 
   {
         $from = 1;
      } 
  else 
   {
         $from = $page-3;
      }
   
for ($i = $from; $i <= $to; $i++)

{
if ($i == $total_results) $to=$total_results;
if ($i != $page)
  {
  echo "<a href=$PHP_SELF?showold=yes&page=$i>$i</a>";
  }
else
  {
  echo "<b><font face=Verdana size=2>[$i]</font></b>";
  }
if ($i != $total_pages)
  echo " ";
}
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo " <a href=$PHP_SELF?page=$nextpage>>></a> "; // Next Page Link

}
echo "</center>";

?>

 

 

Any help would be greatly appreciated since i'm really new with php!!

 

THANKS!!

that script looks to be using register_globals wich has been depreciated for security reasons. You need to define $page... add this to the top.

try:

<?php
$server = "localhost";
$user = "user";
$pass = "pass";
$databasename = "table";
$db = mysql_connect($server, $user, $pass);
mysql_select_db($databasename,$db);

  $sql = "SELECT * FROM annonccom WHERE (titre LIKE '%$search%')";
  $query = mysql_query($sql,$db);
  $total_results = mysql_num_rows($query);
  $limit = "5"; //limit of archived results per page.
  $total_pages = ceil($total_results / $limit); //total number of pages
if (empty($_GET['page']))
{
  $page = "1"; //default page if none is selected
} else {
  $page = $_GET['page'];//$_GET is the array of URL variables which is how the page number is passed. 
}
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB

$query = "SELECT * FROM annonccom WHERE (titre LIKE '%$search%') LIMIT $offset, $limit";
$result = mysql_query($query);
//This is the start of the normal results...

while ($row = mysql_fetch_array($result))
  {
  
echo "
<table style=\"border: 1px solid #000000\" cellpadding=4 cellspacing=0 width=600>
<tr>
<td bgcolor=D8D8D8 width=450><b><a href=\"annonce.php?cmd=view&id=$row[id]\">$row[titre]</a></b></td>
<td bgcolor=D8D8D8 width=150 align=right><div class=red>Prix : $row[prix] $</div></td>
</tr>
<tr>
<td width=450>$row[descrp]</td>
<td width=150 align=center><a href=\"annonce.php?cmd=view&id=$row[id]\"><IMG alt=\"\" src=\"thumbs.php?src=/uploads/$row[picture]&h=140&w=140&zc=1\" border=0> </a></td>
</tr>
</table><br>
</td>
<tr>
</table>
        ";


  }
  mysql_close();

// This is the Previous/Next Navigation
echo "<center>";
echo "Pages:($total_pages)  "; // total pages
if ($page != 1)
{
echo "<a href=$PHP_SELF?page=1><< First</a>   "; // First Page Link
$prevpage = $page - 1;
echo " <a href=$PHP_SELF?page=$prevpage><<</a> "; // Previous Page Link
}
     if ($page == $total_pages) 
   {
         $to = $total_pages;
      } 
  elseif ($page == $total_pages-1) 
   {
         $to = $page+1;
      } 
  elseif ($page == $total_pages-2) 
   {
         $to = $page+2;
      } 
  else 
   {
         $to = $page+3;
      }
     if ($page == 1 || $page == 2 || $page == 3) 
   {
         $from = 1;
      } 
  else 
   {
         $from = $page-3;
      }
   
for ($i = $from; $i <= $to; $i++)

{
if ($i == $total_results) $to=$total_results;
if ($i != $page)
  {
  echo "<a href=$PHP_SELF?showold=yes&page=$i>$i</a>";
  }
else
  {
  echo "<b><font face=Verdana size=2>[$i]</font></b>";
  }
if ($i != $total_pages)
  echo " ";
}
if ($page != $total_pages)
{
$nextpage = $page + 1;
echo " <a href=$PHP_SELF?page=$nextpage>>></a> "; // Next Page Link

}
echo "</center>";

?>

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.