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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STILL no luck with prev/next buttons 3

Status
Not open for further replies.

overyde

Programmer
May 27, 2003
226
0
0
ZA
Hi,
I'm still really stuck!!!
I have managed the first part of paginating my code but however the previous and next buttons don't work. When I click on "next" the same results (first 5) are shown. I'm using php4 and MySQL.

Here's my code:

<?
define(MAX_DISPLAY, 5);

$link = mysql_connect(&quot;localhost&quot;, &quot;yourdb&quot;, &quot;yourpass&quot;)
or die(&quot;Could not connect : &quot; . mysql_error());

mysql_select_db(&quot;genoa&quot;)
or die(&quot;Could not select database&quot;);

printf(&quot;<br>Row Start: %d Next Row: %d<br><br>\n&quot;, $page, $page+MAX_DISPLAY);
if(!$page) {
$page = 1;
printf(&quot;<a href='test.php?page=%s'>Next Page<br><br></a>&quot;, ($page + MAX_DISPLAY));
$query = &quot;select * from example order by id asc limit &quot;.MAX_DISPLAY.&quot;&quot;;
} else {
$query = &quot;select * from example order by id asc limit &quot;.($page - 1).&quot;,&quot;.MAX_DISPLAY.&quot;&quot;;
$prevpage = ($page - MAX_DISPLAY);
$page = ($page + MAX_DISPLAY);
printf(&quot;<a href='test.php?page=%s'>Next Page</a>&quot;, $page);
printf(&quot;<a href='test.php?page=%s'>Prev Page</a><br><br>&quot;, $prevpage);
}


$result = mysql_query($query)
or die(&quot;Could not complete query : &quot; . mysql_error());

if(!$result) {
printf(&quot;No entries found.\n&quot;);
exit;
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf(&quot;[%d] - %s<br>\n&quot;, $row[&quot;id&quot;], $row[&quot;name&quot;]);
}

mysql_free_result($result);
mysql_close($link);

?>


Reality is built on a foundation of dreams.
 
The following previous/next works on my site.
Hope it helps

if($page != 1){
$pageprev = $page-1;
echo(&quot;<a href=\&quot;}else{
echo(&quot;PREV&nbsp;&quot;);
}

if(($totalrows - ($limit * $page)) > 0){
$pagenext=$page+1;
echo(&quot;<a href=\&quot;}else{
echo(&quot;&nbsp; NEXT &quot;.$limit);
}
 
Have you checked out if register globals is ON?
It sounds like it is OFF.

Instead of refering to $page you'd be better off to refer to $_GET['page]'.

When register globals is off GET, POST, COOKIE variables are not translated into plain variable names. PHP installs now are by default set to register globals OFF.

Your code should work. The only reasong that always the same results are displayed is that $page is always the same, namely not set.
You set it in the URL, so it should be a GET parameter.
Try using $_GET['page'] and let me know what happens.

 
Would it be possible to get the complete script as I can not seem to integrate it into what I have?

Reality is built on a foundation of dreams.
 
Last message was for likelylad.
Thanks as well DRJ478

Reality is built on a foundation of dreams.
 
See below the full code.
You will have to change the server location,database,password,select statements,$productcode= and href=
Let me know if it works for you

<?php
$db = mysql_connect(&quot;x.x.x.x&quot;, &quot;mypassword&quot;);

mysql_select_db(&quot;mydatabase&quot;,$db);


$result = mysql_query(&quot;SELECT fields from mytable&quot;,$db);
$limit=50;
$totalrows=mysql_num_rows($result);

$page=$_GET[page];

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

$limitvalue = $page * $limit - ($limit);
$query1= (&quot;SELECT fields from mytable LIMIT $limitvalue, $limit&quot;);
$result1=mysql_query($query1);

if ($myrow = mysql_fetch_array($result1)) {

echo &quot;<table border=2>\n&quot;;
do {

$productcode=$myrow[&quot;productcode&quot;];

printf(&quot;<tr><td><center>$productcode</center></td></tr>\n&quot;);

} while ($myrow = mysql_fetch_array($result1));


echo &quot;</table>\n&quot;;

} else {

echo &quot;Sorry, no records were found!&quot;;

}
if($page != 1){
$pageprev = $page-1;
echo(&quot;<a href=\&quot;}else{
echo(&quot;PREV &quot;);
}

$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page) {
echo($i.&quot; &quot;);
}else{
echo(&quot;<a href=\&quot;}
}

if(($totalrows % $limit) != 0){
if($i == $page) {
echo($i.&quot; &quot;);
}else{
echo(&quot;<a href=\&quot;}
}

if(($totalrows - ($limit * $page)) > 0){
$pagenext=$page+1;
echo(&quot;<a href=\&quot;}else{
echo(&quot;  NEXT &quot;.$limit);
}

?>
 
That was spot-on DRJ478!!! Where would we be without people like you?

Thanks as well likelylad.

Quick question: Why does having register globals &quot;on&quot; pose a security risk?

Reality is built on a foundation of dreams.
 
Brilliant,
Thanks likelylad!

Reality is built on a foundation of dreams.
 
Having a problem with display an image though it was very good! Helps alot! Thanks!

Reality is built on a foundation of dreams.
 
Sorted the image thing out as well!!
Thanks [cat]

Reality is built on a foundation of dreams.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top