首页 > 解决方案 > 我将如何通过 php get 请求实现分页

问题描述

我有一些支持分页的代码,但我不能让我的按钮工作。任何人都可以帮忙吗?

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;";
  }
?>";

}

和 php 是:

$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);

我不知道如何使用这个系统添加分页。任何人都可以帮忙吗?我尝试过移动网址,但由于某种原因只返回了 0。

标签: javascriptphp

解决方案


这是一个 GET 请求,所以在 PHP 中我可以使用 $_GET["page"]然后相应地添加或减去 1。


推荐阅读