10

I have a MySQL query

SELECT * FROM 'redirect'
WHERE 'user_id'= \''.$_SESSION['user_id'].' \' 
ORDER BY 'timestamp'`

I want to paginate 10 results per page. How Can I do it?

Yacoby
  • 54,544
  • 15
  • 116
  • 120
Francesc
  • 1,339
  • 4
  • 16
  • 34

4 Answers4

35

Here is a nice starting point:

<?php

// insert your mysql connection code here

$perPage = 10;
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
$startAt = $perPage * ($page - 1);

$query = "SELECT COUNT(*) as total FROM redirect
WHERE user_id = '".$_SESSION['user_id']."'";
$r = mysql_fetch_assoc(mysql_query($query));

$totalPages = ceil($r['total'] / $perPage);

$links = "";
for ($i = 1; $i <= $totalPages; $i++) {
  $links .= ($i != $page ) 
            ? "<a href='index.php?page=$i'>Page $i</a> "
            : "$page ";
}


$r = mysql_query($query);

$query = "SELECT * FROM 'redirect'
WHERE 'user_id'= \''.$_SESSION['user_id'].' \' 
ORDER BY 'timestamp' LIMIT $startAt, $perPage";

$r = mysql_query($query);

// display results here the way you want

echo $links; // show links to other pages
code_burgar
  • 12,025
  • 4
  • 35
  • 53
8

Use LIMIT.

SELECT *
FROM redirect
WHERE user_id = '35251' 
ORDER BY timestamp
LIMIT 40, 10

40 is how many records to skip, 10 is how many to display.

There are also a few problems with your PHP. You use backticks (not single quotes) to surround table and column names. And you shouldn't use string concatenation to build your query.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • There is nothing intrinsically wrong with using string concatenation to build an sql query if the data is known to be safe. – code_burgar Apr 11 '10 at 10:44
  • @code_burgar: It won't give the wrong result, but it will result in a new query plan for every new set of parameters. You should use bind parameters. – Mark Byers Apr 11 '10 at 13:22
4

Here is my code
which contains next and Previous button

<?php  
    $limit = 3;  //set  Number of entries to show in a page.
    // Look for a GET variable page if not found default is 1.        
    if (isset($_GET["page"])) {    
    $page  = $_GET["page"];    
    }    
    else { $page=1;    
    } 
    //determine the sql LIMIT starting number for the results on the displaying page  
    $page_index = ($page-1) * $limit;      // 0

    $All_Users=mysqli_query($con,"select * from users limit $page_index, $limit");
    while($row=mysqli_fetch_array($All_Users))
    {
        //show  data in table or where you want..
    }
    $all_data=mysqli_query($con,"select count(*) from users");
    $user_count = mysqli_fetch_row($all_data);   // say total count 9  
    $total_records = $user_count[0];   //9
    $total_pages = ceil($total_records / $limit);    // 9/3=  3
    if($page >= 2){
        echo "<a href='blog.php?page=".($page-1)."' class='btn 
     customBtn2'>Previous</a>";
      }
    
    if($page<$total_pages) {
        echo "<a href='blog.php?page=".($page+1)."' class='btn customBtn2'>NEXT</a>";   
    }       
?>
Santosh Dangare
  • 685
  • 1
  • 4
  • 15
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Pouria Hemi Nov 23 '20 at 05:48
  • 1
    @PouriaHemati ok. I thought it will be enough for that question. also I have added commnets in the code for better understanding – Santosh Dangare Nov 23 '20 at 07:01
2

Use the LIMIT clausule of the query to limit the amount of results you retrieve from the database.

See: http://dev.mysql.com/doc/refman/5.1/en/select.html

Rody
  • 2,675
  • 1
  • 22
  • 40