Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Displaying multiple returns from mysql query
Old 12-02-2005, 06:04 AM Displaying multiple returns from mysql query
Skilled Talker

Posts: 58
Trades: 0
hi,

I do a SELECT on a table, and put it into a while loop.

There is 30 entries that can be displayed but I only want to display 5 entries at a time, and i would like to have a next button & a previous button at the bottom where you display the next 5 or previous 5 entries.

How do I do this in PHP?

Thanx!
Llama is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-02-2005, 09:37 PM
Nahele's Avatar
Extreme Talker

Posts: 204
Trades: 1
I had a photo gallery project similar to this. It's not to hard. Basically you want the link to be something like this:

thispage.php?page=3

now you will read the value of the page variable and skip the desired number of entries in accordance with the page (you can use a loop for this), in this case skipping the first ($_GET['page'] - 1) * 5 entries. Then have a loop to display the next 5. You will want to make sure that the next and prev buttons are not present when on the last and first page respectively.

If you still can't get it to work I'll post up an example that you can configure for what you need.
__________________
The worst things in life allow us to appreciate the best things

virtual kudos (a.k.a. talkupation) always welcome where deserved.
Nahele is offline
Reply With Quote
View Public Profile
 
Old 12-03-2005, 10:02 AM
Skilled Talker

Posts: 58
Trades: 0
Would you please post the example?

I do not really understand what you mean... sorry..
Llama is offline
Reply With Quote
View Public Profile
 
Old 12-03-2005, 08:00 PM
Nahele's Avatar
Extreme Talker

Posts: 204
Trades: 1
I appologize that I have not had a chance to formally check this...but it should give you a good idea of what is going on...and if anyone sees any errors, please point them out.

PHP Code:
<html>
<body>
<?php

//$results = your mySQL query here

//if the form www.domain.com/thispage.php
if (!isset($_GET['page'])) {
    
$pageNum 1;
}
//if the form www.domain/thispage.php?page=4
else {
    
$pageNum $_GET['page'];
}

//determine the number of entries to skip assuming 5 entries per page
$skipValue = ($pageNum 1) * 5;

//skips through a certain number of entries determined by $skipValue
for ($i 0$i $skipValuei++) {
    
$temp mysql_fetch_array($results);
}

//display the next 5 entries
for ($i 1$i <= 5i++) {
  
//check to make sure $row exists from the $results
    
if($row mysql_fetch_array($results)) {
        print 
$row[0]."<br>";
        
//you will need to add additional columns to display here
    
}
}

if (
$pagenum != 1) {
    print 
'<a href="www.domain.com/thispage.php?page='.($pagenum 1).'">Prev&nbsp;&nbsp;</a>';
}
if (
$pagenum != {
    print 
'<a href="www.domain.com/thispage.php?page='.($pagenum 1).'">Next</a>';
}
?>
</body>
</html>
__________________
The worst things in life allow us to appreciate the best things

virtual kudos (a.k.a. talkupation) always welcome where deserved.
Nahele is offline
Reply With Quote
View Public Profile
 
Old 12-07-2005, 12:17 PM
Skilled Talker

Posts: 58
Trades: 0
I know that this is going to sound really stupid... but i can't seem to get this right!


Is there any possibillity for somebpdy to explain to me ow this code works.... cause I am really lost!

Thanx!!
Llama is offline
Reply With Quote
View Public Profile
 
Old 12-07-2005, 01:38 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Skipping through the results isn't the best way of doing pages. MySQL provides the LIMIT and OFFSET clauses to do this:

PHP Code:
<?php

//Connect to your database,
//YOU WILL NEED TO FILL THIS BIT IN

//Get the page number from the URL, or set it to 1 if the URL doesn't 
//have page set to anything.
if(isset($_GET['page'])) {
  
$page $_GET['page'];
}
else {
  
$page 1;
}

$num_per_page 5;

//Calculate the starting item from the page number and the num per page
$start = ($page-1)*$num_per_page +1;

//Get the right number of items from the right place in the db.
$query="SELECT * FROM test3 LIMIT $num_per_page OFFSET $start";
$result mysql_query($query) or die(mysql_error());

//Start a counter to display the item numbers
$i $start;

//Loop over the results
while($row mysql_fetch_array($result)) {
  echo 
"[item " $i "]: ";
  echo 
$row[0] . "<br/>";
  
$i++;
}

//Print the Next and Prev links
if($page 1) {
  echo 
'<a href="'
    
$_SERVER['PHP_SELF'] . "?page=" . ($page-1
    . 
'"><--Prev </a>';
}
if(
mysql_num_rows($result) == $num_per_page) {
  echo 
'<a href="' 
    
$_SERVER['PHP_SELF'] . "?page=" . ($page+1
    . 
'"> Next--></a>';
}

?>
You'll need to fill in the top bit with your database connection (mysql_connect or whatever)
You'll also need to change the table name from test3 to whatever your table is.

This prints a very basic list of items and two links. You could extend it very easily to print nice tables or something.

Also when there are an exact multiple of five items in the database, this script adds an extra page which shouldn't be there. Getting rid of that is possible but would make this nice simple example a lot more messy.

I've tested this and put quite a few comments in it, let me know if any part of it needs clarifying....
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)

Last edited by 0beron; 12-07-2005 at 01:41 PM..
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to Displaying multiple returns from mysql query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.56736 seconds with 12 queries