?
Solved

Doing a limited display result on one page

Posted on 2003-03-19
7
Medium Priority
?
179 Views
Last Modified: 2012-05-04
hi guys...any sample coding to allow a limited display of results on one page..Eg: I want display 4 results on a page and when click next, the other 4 will be display on page 2....

The coding I got from net can't work....any better answers?
0
Comment
Question by:Kittie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Accepted Solution

by:
Thunder27 earned 200 total points
ID: 8168339
Here's an example of using page numbers with 4 records per page when selecting from a MySQL database:

// FIRST GET A COUNT OF ALL RECORDS AS $count
$sQuery = "SELECT count(*) FROM your_table WHERE your_criteria";
$result = mysql_query($sQuery);
$count = mysql_result($result, 0, 0);

$numRecords = 4;

if (empty($pg)) { $pg = 1; }
if ($pg < 2) { $pg = 1; }
if ($pg > (floor($count / $numRecords) + 1))
{
  $pg = floor($count / $numRecords) + 1;
}

$numStart = ($pg * $numRecords) - $numRecords;
$numEnd = $numStart + $numRecords;
if ($numEnd > $count) { $numEnd = $count; }

// NOW LIMIT HOW MANY RECORDS TO RETURN BASED ON THE PAGE NUMBER
$sQuery = "SELECT foo,bar FROM your_table WHERE your_criteria LIMIT $numStart,$numRecords";

$recAlm = mysql_db_query ("yourdb", $sQuery, $link);

while ($almrow = mysql_fetch_array ($recAlm)) {

     // BLUE BACKGROUND FOR ODD ROWS
     $i++ % 2 <> 0 ? $fm1 = "<td nowrap>" : $fm1 = "<td nowrap bgcolor=\"#CCFFFF\">";
     $fm1 .= "<font class=\"smallind\">";

        echo "<tr>";
        echo $fm1 . $almrow[foo];
        echo $fm1 . $almrow[bar];
        echo "</tr>";
}
echo "</table>";



This probably isn't perfect, but will give you a good start.
0
 
LVL 4

Expert Comment

by:lokeshv
ID: 8168612
Use Limit in Query

and pass start point and total number of records in URL of next or prev page..

Hope it helps

Lk
0
 
LVL 2

Expert Comment

by:bobsledbob
ID: 8168946

There's also a 'pager' class available from PEAR...

PEAR DB specific:
http://pear.php.net/package-info.php?pacid=31

Generic pager:
http://pear.php.net/package-info.php?pacid=32

Sliding Window pager (like you see on google):
http://pear.php.net/package-info.php?pacid=136

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:flyguy357
ID: 8192061
$display_number = 20;

// If we dont know how many pages there are, make that calculation.
if (!isset($num_pages)) {

     // Determine the query.
     if (isset($letter)) { // Browsing a particular letter or the whole thing?
          $query1 = "SELECT field, FROM table WHERE whatever LIKE '$letter%' ORDER BY whatever ASC";
     } else {
          $query1 = "SELECT field, FROM author ORDER BY whatever ASC";
     }

     // Query the database.
     $query_result1 = mysql_query ($query1) or die (mysql_error());

     // Calculate the number of pages required.
     $num_results = @mysql_num_rows ($query_result1);
     if ($num_results > $display_number) {
          $num_pages = ceil ($num_results/$display_number);
     } elseif ($num_results > 0) {
          $num_pages = 1;
     } else {
          echo 'There are no things in this category.';
     }

     $start = 0; // Currently at item 0.
}

// Make the new, limited query.
if (isset($letter)) {
          $query = "SELECT field, FROM table WHERE whatever LIKE '$letter%' ORDER BY whatever ASC LIMIT $start, $display_number";
     } else {
          $query = ""SELECT field, FROM author ORDER BY whatever ASC LIMIT $start, $display_number";
}

// Print each item.
//your code goes there

// Make the links to other pages, if necessary.
if ($num_pages > 1) {

     echo '<hr width="50%" align="left" />';

     // Determine what page the script is on.
     if ($start == 0) {
          $current_page = 1;
     } else {
          $current_page = ($start/$display_number) + 1;
     }

     // If it's not the first page, make a Previous button.
     if ($start != 0) {
          echo '<a href="browse_whatever.php?start=' . ($start - $display_number) . '&num_pages=' . $num_pages . '&letter=' . $letter . '">Previous</a> ';
     }

     // Make all the numbered pages.
     for ($i = 1; $i <= $num_pages; $i++) {
          $next_start = $start + $display_number;
          if ($i != $current_page) {
               echo '<a href="browse_whatever.php?start=' . (($display_number * ($i - 1))) . '&num_pages=' . $num_pages . '&letter=' . $letter . '">' . $i . '</a> ';
          } else {
               echo $i . ' ';
          }
     }

     // If it's not the last page, make a Next button.
     if ($current_page != $num_pages) {
          echo '<a href="browse_whatever.php?start=' . ($start + $display_number) . '&num_pages=' . $num_pages . '&letter=' . $letter . '">Next</a> ';
     }

}




Hope that helps
0
 

Expert Comment

by:flyguy357
ID: 8192073
regarding the previous code I posted ....the variable names for sql are all different as i was trying to adapt it to your requirement...so I think i skepped a few.

Atleast the other part should give you some idea.
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11935592
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Accept: Thunder27 {http:#8168339}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

snoyes_jw
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question