results per page and next 7 results

I have not use PHP for a while, I just wrote this and I am trying to figure this out. I have no problem with the form, but I am trying to get my results which I can but I have 2 issues that I am trying to figure out.

  1. I want 7 results per page
  2. Then I want to click next to get my next 7 results
    How do I do that?

Here is what I have so far and I know this work, it connected to the MYSQL database

<php

$query = mysql_query(“SELECT * FROM my_test_results WHERE first_last = ‘smith’”);

WHILE($rows = mysql_fetch_array($query)):

		$first_name = $rows ['first_name'];
		$last_name = $rows ['last_name'];
		$age = $rows ['age'];
		$zip = $rows ['zip'];
		

echo "$first_name<br>";
echo "$last_name<br>";
echo " $age<br>";
echo "$zip<br><br><br>"; 		

endwhile;

?>

What you are talking about is called pagination. It is a bit complicated to explain to you. Your best bet is to do a search for pagination tutorials or scripts. There are many out there.

You are using deprecated MySQL code. You need to use PDO or mysqli with prepared statements. There is a link to a PDO bump start database in my signature to get you going.

TY Kevin

But I am trying with pagination and I am having issues which I hope someone can help me out with. The firt forum worked but when I added the pagination scrip I am getting an Internal Server Error. What am I doing wrong?

<?php $sql = mysql_query("SELECT * FROM test); $nr = mysql_num_rows($sql); // Get total of Num rows from the database query if (isset($_GET['pn'])) { // Get pn from URL vars if it is present $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new) //$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated) } else { // If the pn URL variable is not present force it to be value of page number 1 $pn = 1; } //This is where we set how many database items to show on each page $itemsPerPage = 4; // Get the value of the last page in the pagination result set $lastPage = ceil($nr / $itemsPerPage); // Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage if ($pn < 1) { // If it is less than 1 $pn = 1; // force if to be 1 } else if ($pn > $lastPage) { // if it is greater than $lastpage $pn = $lastPage; // force it to be $lastpage's value } // This creates the numbers to click in between the next and back buttons // This section is explained well in the video that accompanies this script $centerPages = ""; $sub1 = $pn - 1; $sub2 = $pn - 2; $add1 = $pn + 1; $add2 = $pn + 2; if ($pn == 1) { $centerPages .= '  ' . $pn . '  '; $centerPages .= '  ' . $add1 . '  '; } else if ($pn == $lastPage) { $centerPages .= '  ' . $sub1 . '  '; $centerPages .= '  ' . $pn . '  '; } else if ($pn > 2 && $pn < ($lastPage - 1)) { $centerPages .= '  ' . $sub2 . '  '; $centerPages .= '  ' . $sub1 . '  '; $centerPages .= '  ' . $pn . '  '; $centerPages .= '  ' . $add1 . '  '; $centerPages .= '  ' . $add2 . '  '; } else if ($pn > 1 && $pn < $lastPage) { $centerPages .= '  ' . $sub1 . '  '; $centerPages .= '  ' . $pn . '  '; $centerPages .= '  ' . $add1 . '  '; } // This line sets the "LIMIT" range... the 2 values we place to choose a range of rows from database in our query $limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; // Now we are going to run the same query as above but this time add $limit onto the end of the SQL syntax // $sql2 is what we will use to fuel our while loop statement below $sql2 = mysql_query("SELECT * FROM $limit"); //////////////////////////////// END Adam's Pagination Logic //////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////// Adam's Pagination Display Setup ///////////////////////////////////////////////////////////////////// $paginationDisplay = ""; // Initialize the pagination output variable // This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display if ($lastPage != "1"){ // This shows the user what page they are on, and the total number of pages $paginationDisplay .= 'Page ' . $pn . ' of ' . $lastPage. '      '; // If we are not on page 1 we can place the Back button if ($pn != 1) { $previous = $pn - 1; $paginationDisplay .= '  Back '; } // Lay in the clickable numbers display here between the Back and Next links $paginationDisplay .= '' . $centerPages . ''; // If we are not on the very last page we can place the Next button if ($pn != $lastPage) { $nextPage = $pn + 1; $paginationDisplay .= '  Next '; } } ///////////////////////////////////// END Adam's Pagination Display Setup /////////////////////////////////////////////////////////////////////////// // Build the Output Section Here WHILE($rows = mysql_fetch_array($sql)): $first_name = $rows ['first_name']; $last_name = $rows ['last_name']; $age = $rows ['age']; $zip = $rows ['zip']; echo "$first_name
"; echo "$last_name
"; echo "$age
"; echo "$zip


"; endwhile; ?>

:smiley: I figure it out. :stuck_out_tongue:

Kevin Rubio

TY again because you pointed me in the right direction

Sponsor our Newsletter | Privacy Policy | Terms of Service