简体   繁体   中英

How would I implement pagination through php get requests

I have some code that supports pagination, but I can't make my buttons work. Can anyone help?

function setData() {
var flexContainer = document.getElementById("flex");
flexContainer.innerHTML = "<?php 
  foreach ($articlesarray as $seperated) {
    $contentsContent = file_get_contents("../" . "$seperated[contentsname]");
    echo "<div class='card'><img src='$seperated[img]'' alt='uh oh photo not found' style='width:100%''><div class='container'><h4><b>$seperated[title]</b></h4><p>$contentsContent</p></div></div>";
  }
 ?>";
document.getElementById("back").disabled = "<?php 
  if ($_SERVER['REQUEST_URI'] == "/list/index.php?page=1") {
    echo "true";
  } else {
    echo "false";
  }
?>";
document.getElementById("back").style = "<?php 
  if ($_SERVER['REQUEST_URI'] == "/list/index.php?page=1") {
    echo "display: none;";
  } else {
    echo "display: inline-block;";
  }
?>";

}

and the php is:

$servername = "localhost";
$username = "root";
$password = "You can't have my server password";
$dbname = "myDB";

$badurl = "/list/index.php";
$newURL = "/list/index.php?page=1";

if ($_SERVER['REQUEST_URI']==$badurl) {
  print "uh oh spaghettios";
  header('Location: ' . $newURL);
  die();
}

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$offsetAmount = $_GET["page"] * 9 - 9;

$sql = "SELECT id, title, contentsname, img FROM articles LIMIT 9 OFFSET $offsetAmount";
$result = $conn->query($sql);

$articlesarray = array();

while($row = mysqli_fetch_assoc($result)){
   $articlesarray[] = $row;
}

//echo "<br><br><br> If you are reading this, you have found the debug screen. This website is under maintanence.";

mysqli_close($conn);

I can't work out how to add pagination using this system. Can anyone help? I have tried shifting the url but that only returned a 0 for some reason.

It's a GET request so in PHP I can just use $_GET["page"] and then add or subtract 1 accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM