1

I want to display records from database using mysql_fetch_array. But the problem is i don't know how to fix the size to display in the page and move the rest of records to next page as there is too many records to be displayed.

Eg: Display first 10 rows(or records) in page1, then another 10 rows in another page?

Heres are my code:

$result=mysql_query("$selectKL UNION $selectKlang UNION $selectPJ UNION $selectSJ 
ORDER BY restaurant_name LIMIT 6")

$searchQuery = search($budgetRange,$city,$category);

while($row = mysql_fetch_array($searchQuery))
{
   echo"<tr>";
   echo'<td>'.$row['restaurant_id'].'</b></td>';
   echo'<td>'.$row['restaurant_name'].'</b></td>';
   echo'<td>'.$row['category'].'</b></td>';
   echo'<td>'.$row['budget'].'</b></td>';
   echo'<td>'.$row['halal'].'</b></td>';
   echo "</tr>";
}

There will be 30+ results to be displayed from my database but how do i display the first 10 result in 1st page, then another 10 result in next page and so on? I've already set the LIMIT to 10 in the query and yes it does only display 10 result, but i don't know how to store or pass the rest of result(is it possible?). Any help is appreciate... please?

1

2 Answers 2

1

Pagination can be tricky. You'll need to set up some parameters (probably query string parameters) to indicate enough information to know where to pick up your pagination, namely, either the number of results per page and the page number, or the number of the last result shown.

In your query, you can use LIMIT x,y where x is the starting row number and y is the number of rows to retrieve.

Pagination is a problem that's been solved many times over - that doesn't mean it's easy, but it means there's lots of help available. Try a quick search: https://encrypted.google.com/search?hl=en&q=php+mysql+pagination

Sign up to request clarification or add additional context in comments.

Comments

0
/*
    Place code to connect to your DB here.
*/


$tbl_name="xyz";        //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 3;

/* 
   First get total number of rows in data table. 
   If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name WHERE approved`=1  ";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages['num'];

/* Setup vars for query. */
$targetpage = "index.php";  //your file name  (the name of this file)
$limit = 5;                                 //how many items to show per page
$page = (!empty($_GET['page']) ? $_GET['page'] : null);

if($page) 
    $start = ($page - 1) * $limit;          //first item to display on this page
else
    $start = 0;                             //if no page var is given, set start to 0

/* Get data. */
$sql = "SELECT * FROM $tbl_name WHERE `approved`=1  ORDER BY `id` DESC LIMIT $start, $limit ";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
$prev = $page - 1;                          //previous page is page - 1
$next = $page + 1;                          //next page is page + 1
$lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;                      //last page minus 1

include ('includes/pagination.php');


    while($row = mysql_fetch_array($result))
    {

    //designate variables




echo  'Your content';
}
$pagination

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.