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
php paging mysql results
Old 02-03-2008, 07:11 AM php paging mysql results
andyp's Avatar
Extreme Talker

Posts: 165
Name: andy patterson
Trades: 0
how do i split my mysql result into several pages?.
andyp is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-03-2008, 07:30 AM Re: php paging mysql results
Skilled Talker

Posts: 71
Trades: 0
It's called Pagination. There are several good tutorials out there, try Google.
CrazeDizzleD is offline
Reply With Quote
View Public Profile
 
Old 02-03-2008, 12:57 PM Re: php paging mysql results
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
@Craze: Why post if you're not going to be helpful?

@andyp: This should get you started:
PHP Code:
<?php
// Assume $database is an instance of class mysqli ( http://us2.php.net/manual/en/function.mysqli-connect.php )

define('RESULTS_PER_PAGE',25);

//Make sure query has SQL_CALC_FOUND_ROWS
$query_string "SELECT SQL_CALC_FOUND_ROWS * FROM my_table WHERE ...";

//Initialize variables
$page 1;
$total_rows 0;
$num_pages 0;

if (isset(
$_GET['page'])) {
  
$page $_GET['page'];
}
if (
$page == 0) {
  
$page 1;
}

//Limit total number of results
$query_string .= " LIMIT ".(($page-1) * RESULTS_PER_PAGE).",".RESULTS_PER_PAGE;

if (
$query_results $database->query($query_string)) {
  if (
$query_results->num_rows 0) {
    
//Find page count information
    
if ($row_count_query $database->query("SELECT FOUND_ROWS() AS total_rows")) {
      if (
$row_count_query->num_rows 0) {
        
$row_count $row_count_query->fetchObject();
        
        
$total_rows $row_count_query->total_rows;
        
        
$num_pages ceil ($total_rows/RESULTS_PER_PAGE);
      }
    }
    
//Fetch query results
    
while ($a_result $query_results->fetchObject()) {
      
print_r($a_result); //Obviously, you need to customize this.
    
}
    
//Echo out pagination
    
if ($num_pages 1) {
      echo 
'<form method="get">';
      echo 
'<select name="page" onChange="this.form.submit();">';
      for (
$page_number=1;$page_number<=$num_pages;$page_number++) {
        echo 
'<option value="'.$page_number.'"'.(($page_number==$page)?' selected="selected"':'').'>'.$page_number.'</option>';
      }
      echo 
'</select>';
      echo 
'</form>';
    }
  }
}
?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 02-03-2008, 01:36 PM Re: php paging mysql results
andyp's Avatar
Extreme Talker

Posts: 165
Name: andy patterson
Trades: 0
Thanks for that
andyp is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to php paging mysql results
 

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.20794 seconds with 12 queries