Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nex / Previous script help

Status
Not open for further replies.

JamesCliff

Programmer
Feb 16, 2005
106
GB
Hi all,

Right i have finished my site, however im doing some upgrades as requested by my client. He wants to have a certain number of records per page and then next and previous buttons. So i found a script and implemented it into my script which ive posted below:-

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>
<?
session_start(); 
$host = "127.0.0.1";
$user = "jim11";
$pass = "";
$db = "gbplantandmachinery";

$connection = mysql_connect($host, $user, $pass) or die ("Unable to
connect!");

mysql_select_db($db) or die ("Unable to select database!");

if (!session_is_registered("salescounted")){ 
    mysql_query("UPDATE salescounter SET count=(count + 1) WHERE count_id=1"); 
session_register("salescounted"); 
}
?>

<div align="center">
  <table width="724">
    <!--DWLayoutTable-->
    <tr> 
      <td width="100%" height="261" valign="top"><div align="center">
          <br>
          <p><font size="2"><strong>sales</strong></font></p>
          <p>Please contact us for any further information or pictures regarding<br>
            a sales item in the list below.<br>
            <br>
            <a href="index.php?page=contact">Click here for contact information</a></p>
          <p>&nbsp;</p>
          <p> 
<?

$server = "127.0.0.1"; 
$user = "jim11"; 
$pass = ""; 
$databasename = "gbplantandmachinery"; 
$db = mysql_connect($server, $user, $pass); 
      mysql_select_db($databasename,$db); 	
	
	
echo "<table>";
echo "<tr>

      <td width='120'><strong>Photo</strong></td>
	  <td width='130'><strong>Make / Model</strong></td>
	  <td width='260'><strong>Description</strong></td>
	  <td width='120'><strong>Date Posted</strong></td>
	  <td width='110'><strong>Price</strong></td>
	  
	  </tr>";
	  
echo "<tr>";
	 
echo '<td colspan="5"><hr /></td>
		   
	  </tr>';
	  
echo "<tr>";
	 
echo '<td colspan="5">&nbsp;</td>
		   
	  </tr>';
	  
echo "</table>";

	

$sql = "select `Image_Ref`, `Title`, `Description`, `Date`, `Price` from sales"; 
        $query = mysql_query($sql,$db); 
        $total_results = mysql_num_rows($query); 
        $limit = "3"; //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 `Image_Ref`, `Title`, `Description`, `Date`, `Price` from sales LIMIT $offset, $limit"; 
    $rslt = mysql_query($query); 
//This is the start of the normal results... 

    while ($result = mysql_fetch_array($rslt)) 
        { 
		
	 echo "<table>";

     echo "<tr>";
	 
     echo '<td><img src="photos/' . $result['Image_Ref'] . '" width="100" height="75"></td> ';
     echo '<td width="175">' . $result['Title'] . '</td>
	       <td width="250">' . $result['Description'] . '</td>
		   <td width="175">' . $result['Date'] . '</td>
		   <td width="100">' . $result['Price'] . '</td>
		   
		   </tr>';
		   
	 echo "<tr>";
	 
	 echo "<td><a href=\"photos/" . $result['Image_Ref'] . "\" target=\"_blank\">Full Size Picture</a></td>"; 
		   
	 echo "</tr>";
	 		   
	 echo "<tr>";
	 
     echo '<td colspan="5"><hr /></td>
		   
		   </tr>';



echo "</table>";

 
        } 
        mysql_close(); 


// This is the Previous/Next Navigation 
echo "<font face=Verdana size=1>"; 
echo "Pages:($total_pages)&nbsp;&nbsp;"; // total pages 
if ($page != 1) 
{ 
echo "<a href=$PHP_SELF?page=1><< First</a>&nbsp;&nbsp;&nbsp;"; // First Page Link 
$prevpage = $page - 1; 
echo "&nbsp;<a href=$PHP_SELF?page=$prevpage><<</a>&nbsp;"; // 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 "&nbsp;"; 
    } 
if ($page != $total_pages) 
{ 
$nextpage = $page + 1; 
echo "&nbsp;<a href=$PHP_SELF?page=$nextpage>>></a>&nbsp;"; // Next Page Link 
echo "&nbsp;&nbsp;&nbsp;<a href=$PHP_SELF?page=$total_pages>Last >></a>"; // Last Page Link 
} 
echo "</font>"; 

// This is the end of the Previous/Next Navigation 


?>
            <br>
          </p>
        </div></td>
    </tr>
  </table>
</div>
</body>
</html>

Now everything displays fine and looks good. The script is able to work out how many pages are needed and shows the links at the bottom of the page. I currently have 4 records in my database and have the limit on the script above set to 3. So therefore it displays page 1 and page 2 link. However when i click on a link to goto page 2 nothing happens, no records change or update or anything like that. It simply stays on the same page it started with displaying the first 3 records. However after the links have been clicked, the address bar does alter and page=2 is displayed, still dosnt work though.

Why is this?

You can see the problem for yourself at:


Click on the page links and see what happens.
Using the code i posted above, can anyone tell me whats wrong and or how it can be fixed?

Thanks alot

Jim
 
Jim

i'll take a look at this. is this an adaptation of my earlier post to you on this topic? it looks familiar stylistically.

does your client definitely only want next/previous or to be able to browse to any page (i.e. have a page listing). it would be easy to do a hybrid with the pages in a drop down between the next and previous buttons too.

Justin
 
hmm, no - not one of mine. look back through your old posts and you can see what i posted. works out of the box.

the flaw in your code is in your use of the $page var.
Code:
if (empty($page))
    {
        $page = "1"; //default page if none is selected
    }

would suggest instead;


Code:
if (empty($_GET['page']))
    {
        $page = "1"; //default page if none is selected
    } 
else
    {
        $page = addslashes($_GET['page']);
    }
 
on reflect you might want to expand on my post above as follows:

Code:
if (!isset($_GET['page']) || empty($_GET['page']))
    {
        $page = "1"; //default page if none is selected
    }
else
    {
        $page = addslashes($_GET['page']); 
    }
 
Thanks alot for the help m8.

The changes you suggested to me did work well :)

Im going to look back through my posts to find the thread you gave me an out of the box solution, i will then give that a go as well.

Again thanks alot

Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top