swipeales
swipeales

Reputation: 127

pagination works but it doesn't go to next page

i have added pagination on my site, everything is fine but whenever i click next it will go to same page url/dataload2.php?page=0. This is my php code, i think it is something wrong in calculations or looping, thank you for any help.

EDIT: some more code added

    <?php

include 'conn.php';
$num_rec_per_page=2;

if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page; 

$sql = "SELECT * from crewlist ORDER BY name ASC LIMIT $start_from, $num_rec_per_page";

$result = $conn->query($sql);

?>

and

 <?php 
    $sql = "SELECT * FROM crewlist"; 
    $rs_result = mysql_query($sql); //run the query
    $total_records = mysql_num_rows($rs_result);  //count number of records
    $total_pages = ceil($total_records / $num_rec_per_page); 

    echo "<a href='dataload2.php?page=1'>".'|<'."</a> "; // Goto 1st page  

    for ($i=1; $i<=$total_pages; $i++) { 
                echo "<a href='dataload2.php?page=".$i."'>".$i."</a> "; 
    }; 
    echo "<a href='dataload2.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
    ?>

EDIT: update syntax

 <?php 
$sql = "SELECT * FROM crewlist"; 
$rs_result = mysqli_query($sql); //run the query
$total_records = mysqli_num_rows($rs_result);  //count number of records
$total_pages = ceil($total_records / $num_rec_per_page); 

echo "<a href='dataload2.php?page=1'>".'|<'."</a> "; // Goto 1st page  

for ($i=1; $i<=$total_pages; $i++) { 
            echo "<a href='dataload2.php?page=".$i."'>".$i."</a> "; 
}; 
?>
<?php
echo "<a href='dataload2.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>

Upvotes: 1

Views: 285

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

As per your edit:

$rs_result = mysqli_query($sql);
  • mysqli_query() requires a db connection be passed as the first parameter

Change your code to read as:

$rs_result = mysqli_query($conn, $sql);

and you'll be good to go.

Reference:

Plus, do read up on prepared statements, or PDO with prepared statements as you could get hit by an SQL injection.

  • Best to be on the safe side ;-)

Additional references:

Upvotes: 1

Related Questions