javascript - 我将如何通过 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。
解决方案
这是一个 GET 请求,所以在 PHP 中我可以使用
$_GET["page"]
然后相应地添加或减去 1。
推荐阅读
- javascript - 控制器 ActionResult 方法参数未从 ajax 调用接收表单数据
- php - android room not null 约束在插入时失败
- python - pd.read_table 字符串解析错误
- wso2 - wso2 esb 在 xslt 转换后注入很少的值
- c - C 编程更喜欢 uint8 而不是 char
- type-conversion - 将十六进制字符串转换为十进制数
- javascript - 为什么基于 Web 的前端捆绑器需要开发服务器
- java - 具有相同原因的多个事件
- javascript - Javascript - 保存地理位置坐标
- java - 存储方法及其参数值?