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

Previous/Next Buttons 2

Status
Not open for further replies.

overyde

Programmer
Joined
May 27, 2003
Messages
226
Location
ZA
Hi,
I'm really stuck now. I want to have a set amount of results on my page at a time (paginate) but I cant get the prev/next buttons to work to fetch the next 25 results. I'm using php4 and mysql. Here's the code I used:

<?php

// open the connection
$conn = mysql_connect(&quot;localhost&quot;, &quot;&quot;, &quot;&quot;);

// pick the database to use
mysql_select_db(&quot;db&quot;,$conn);

$limit = 25;
$query_count = &quot;SELECT count(*) FROM users&quot;;
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);

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


$limitvalue = $page * $limit - ($limit);
$query = &quot;SELECT * FROM users LIMIT $limitvalue, $limit&quot;;
$result = mysql_query($query) or die(&quot;Error: &quot; . mysql_error());

if(mysql_num_rows($result) == 0){
echo(&quot;Nothing to Display!&quot;);
}


print &quot;<table border=1><tr><th>id</th>&quot;;
print &quot;<th>name</th><th>desc</th><th>pics</th><th>pic1</th><th>pic2</th></tr>&quot;;

// go through each row in the result set and display data
while ($newArray = mysql_fetch_array($result)) {
// give a name to the fields
$id = $newArray['id'];
$name = $newArray['name'];
$desc = $newArray['desc'];
$pics = $newArray['pics'];
$pic1 = $newArray['pic1'];
$pic2 = $newArray['pic2'];


//echo the results on screen
echo &quot;
<tr>
<td>$id</td>
<td>$name</td>
<td><img src=\&quot;$pics&quot;></td>
<td>$desc</td>
<td>$pic1</td>
<td>$pic2</td>
</tr>
&quot;;
}

print &quot;</table>&quot;;

if($page != 1){
$pageprev = $page--;

echo(&quot;<a href=\&quot;$PHP_SELF&page=$pageprev\&quot;>PREV&quot;.$limit.&quot;</a>&nbsp;&quot;);
}else{
echo(&quot;PREV&quot;.$limit.&quot;&nbsp;&quot;);
}

$numofpages = $totalrows / $limit;

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


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

if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;

echo(&quot;<a href=\&quot;$PHP_SELF?page=$pagenext\&quot;>NEXT&quot;.$limit.&quot;</a>&quot;);
}else{
echo(&quot;NEXT&quot;.$limit);
}

mysql_free_result($result);

?>

Reality is built on a foundation of dreams.
 
overyde,

I just read quickly through your script.

Please give some more info about:
What is happening or what is not?

 
I understand the problem. I've never attempted to display (x) number of results per page.

I'll write a quick script and post it for you to look at. I'll try to comment it properly for you. :P

- &quot;Delightfully confusing...&quot; raves The New York Times

-Kas
 
Ok, for this I created a database with two fields:

id int primary key auto_increment
name text

I added 15 rows, which is why MAX_DISPLAY is set to 5. This will allow me to view the results 5 per page. I didn't fix it so it doesn't display the &quot;NEXT&quot; link if we reach the end of the table.


<?
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);

?>

I know I said I'd try to comment the code, but I'll just outline the major factor that allows for simple row manipulation in your results:

What this does is, if we're on the first page, start with the first row retrieved and pull 5 deep.

(You can ignore my ORDER sorting, just habit)
$query = &quot;select * from example order by id asc limit &quot;.MAX_DISPLAY.&quot;&quot;;


Here we have the next page, so we're going to have to take the number of the last row pulled, subtract one (limit takes your row number and adds one) and pull 5 deep.

$query = &quot;select * from example order by id asc limit &quot;.($page - 1).&quot;,&quot;.MAX_DISPLAY.&quot;&quot;;

Sorry if it's not clear enough. You can always goto:
(Sorry it's .de, but I grabbed the first one I found)


They define and explain LIMIT.

I would have had this done faster, but my neighbor is outside blowing up appliances with homemade pipeboms. ;)

- &quot;Delightfully confusing...&quot; raves The New York Times

-Kas
 
as kasuals said use the limit query:
&quot;select FieldName from Table LIMIT 5&quot;

when u display the records use a counter and increase its value.
in this e.g the counter will be 5 the first time.

have a hidden field and assign the value of the counter to it.

in the next page read the counter value
the sql will now be:
&quot;select FieldName from Table LIMIT 5,$counter&quot;...


Known is handfull, Unknown is worldfull
 
Brilliant!
Thanks!

DRJ478, what was happening was I was displaying my limited amount of fields but my next button was not working...it did not even register as a hyperlink.

Will try use kasuals suggestion now.

Reality is built on a foundation of dreams.
 
Hey Kasuals,
I used your script and it got me a lot closer to what I need but when I click next it just shows the first 5 results. What's going wrong here?


<?
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.
 
thats because ur $page is always 0...

include a hidden field called page.

slightly modify ur code:
<?
$page=$_POST['page'];
if($page==&quot;&quot;)
$page=1;
else
$page=$page+5;

define(MAX_DISPLAY, 5);


and in the end of the page:
<input type=&quot;hidden&quot; name=&quot;page&quot; value=&quot;<?=$page;?>&quot;>

hth

Known is handfull, Unknown is worldfull
 
I'm really getting frustrated!!!
Still when I click on next it is though nothing is happening.
The addess bar comes up with the URL
When I click next but the results aren't showing.

Reality is built on a foundation of dreams.
 
Correction...
only the first 5 results are showing. So it seems it's not fetching the data.

Reality is built on a foundation of dreams.
 
Sorted... I did not have my register globals on.
But

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

Part and Inventory Search

Sponsor

Back
Top