0

I have read multiple stack overflow answers but still cant find one specific to my needs. Some are outdated. I am using bootstrap and I want to display the first set of records which I am doing by using 'LIMIT 9'. I just have no idea how to get the next 10 records and every 10 records after that to display once the link at the bottom has been clicked. I don't want to open a new page for every 10 records.

 `<div class="container-fluid">
<div class="row row-fluid">
    <div class="col-xs-12 col-sm-12 col-md-8 offset-md-2 col-lg-8 offset-lg-2">
    <h2>List of current Database entries</h2>

         <?php
    $servername = "localhost";
    $username = "xxxxx";
    $password = "xxxxxx";
    $dbname = "xxxxxxxxx";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT id, business_name, email, website, phone FROM table LIMIT 0, 9";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "<table class='table table-striped'>";
        echo "<thead>";
        echo "<tr>";
        echo "<th>ID</th><th>Business Name</th><th>Email</th><th>Website</th><th>Phone</th>";
        echo "</tr>";
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>"."id : ". $row["id"]."</td>
            <td>".$row["business_name"]."</td>
            <td>".$row["email"]."</td>
            <td>".$row["website"]."</td>
            <td>".$row["phone"]."</td>";
            echo "</tr>";
        }
        echo "</tbody>";
        echo "</table>";
    } else {
        echo "0 results";
    }

    $conn->close();
    ?> 
    <a href="#" class="more">Next 10</a>
        </div>
        </div>
    </div>

That is what I have so far. I would really appreciate some help

This image shows how it looks nowimage of how it looks I dont want people to leave the page for the results. Thank you.

Edward68
  • 63
  • 1
  • 10

2 Answers2

1

You would usually get a GET/POST parameter specifying current "page". For example

$page = isset($_GET['page']) ? intval($_GET['page']) : 1; // current page, default is 1
$perPage = 10; // records per one page
$fromLimit = ($page - 1)*$perPage;
$toLimit = $perPage*$page;

$sql = ".... LIMIT $fromLimit, $toLimit";

I have a feeling that you are new to php programming, so be sure to take time and check this: How to prevent SQL injection

The point is you need a dynamic parameter that you give to a php page so you can dynamically change the content.

Links to your pages would look something like that:

<a href="yourdomain.com/test.php?page=1">1</a>
<a href="yourdomain.com/test.php?page=2">2</a>
<a href="yourdomain.com/test.php?page=3">3</a>
Cԃաԃ
  • 1,259
  • 1
  • 15
  • 29
0

please try to remplace this code:

  $sql = "SELECT id, business_name, email, website, phone FROM table LIMIT 0, 9";

to:

if ($_GET['pagenum'] > 0){
 $a = (int) filter_var($_GET['pagenum'],FILTER_SANITIZE_NUMBER_INT);
}else{
 $a = 0; 
}
$Startnumber = ($currectid * 10); $limit_read = ($Limit_number + 10);
$sql = "SELECT id, business_name, email, website, phone FROM table LIMIT ".$Startnumber .", ".$limit_read ;