×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

PHP how to limited record per page
2

PHP how to limited record per page

PHP how to limited record per page

(OP)
Hello and happy new year,

My question is:
How can I show limited number of records per page?
for example, I need to display the records from a table (mysql) on a page, but since the number of records are too long, I need to break them up per page.

Any Idea, How can I do this and generate automatically the number of links needed in the bottom of the page, so when the user clicks on the for example page 2 it would show the remaining records.?
Thanks much for your help

please send your response to
TeamLinux@hotmail.com

RE: PHP how to limited record per page

2
this code will give you an idea.

##########################################################
#setup some variables
##########################################################
$cfgHost = "localhost";
$cfgUser = "someone";
$cfgPassword = "somepass";
$cfgDb = "databasename";

##########################################################
#setup database connection functions
##########################################################
function db_connect() {
     global $cfgHost; $cfgUser, $cfgDb, $cfgPassword;
     $db =  mysql_connect($cfgHost, $cfgUser, $cfgPassword);
      mysql_select_db($cfgDb, $db);
      return $db;
}
##########################################################
#setup sql query functions
##########################################################

function query_database($querystring = "") {

     global $db;

     $db = db_connect();
     $result = mysql_query($querystring) or die("FAILED:".mysql_error()."for".$querystring."\n");
        
      return $result;

}

##########################################################
#start the page
##########################################################

#number of stories per page
$stories_per_page=10;

#how many stories to get at a time
$limit = $stories_per_page;

#set the offset to 0 if not set yet
#otherwise this should be the page number multiplied by
#stories_per_page minus the stories actually on the page

if (!$page):
$offset = 0;
else :
$offset = (($stories_per_page * $page)-$stories_per_page);
endif;

#set up the mysql strings
#$sqlstr retrieves all the rows in the database, using the
#offset and limit functionality to retrieve only certain
#rows.  $sqlstr_wo_limits will be used to count the number
#total rows later on, to work out page numbers

$sqlstr = "SELECT column1, column2 FROM yourtable";
$sqlstr_wo_limits = $sqlstr;
$sqlstr.= " LIMIT ".$offset.", ".$limit;

#print the records
$result = query_database($sqlstr);

do {

echo $myrow[column1];
echo $myrow[column2];

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

#calculate the total number of records
$total_records = query_database($sqlstr_wo_limits);
$count = mysql_num_rows($total_records);

#if no page number variable is present, set it to page 1
if(!$page) {$page=1;}

#work out the offset needed, based on page number
$offset = ($page-1)*$stories_per_page;

#work out how many pages we need to link to
$page_count = ($count-($count%$stories_per_page)) / $stories_per_page + 1;

#set the next page link
$nextpage=$page+1;

#print links to each page
$i = 1;
$output_string.="Page : ";
while ($i <= $page_count) {

      if($i != $page)
     
      {

      $output_string.="<a href=\"$PHP_SELF?page=$i\">$i</a>\n";

      } else {$output_string.="<b>$i</b>";}

                        $i++;
      }

# print the "next page" button
      
     if ($page < $page_count) {
                        $output_string.="<a href=\"$PHP_SELF?page=$nextpage\">";
                        $output_string.="Next $stories_per_page record(s)...</a>";
                        } else {
                        $output_string.="No more records to display!";
                        }

Andres Jugnarain
Wireless Editor
www.thetuckshop.com

RE: PHP how to limited record per page

(OP)
Thanks so very much.

it work just fine with your code.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close