Doing a limited display result on one page

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?
KittieAsked:
Who is Participating?
 
Thunder27Connect With a Mentor Commented:
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
 
lokeshvCommented:
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
 
bobsledbobCommented:

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
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
flyguy357Commented:
$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
 
flyguy357Commented:
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
 
snoyes_jwCommented:
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
All Courses

From novice to tech pro — start learning today.