AJAX を使用して記事に無限スクロールを設定しようとしていますが、PHP ページからコンテンツを取得できません。PDO を使用してデータベースにアクセスしています。通常の制限0、12を使用してみましたが、正常にロードされます。
これが私がこれまでに行ったことです:
chapter.php :
<?php
include("includes/init.php");
$query=$db->prepare("SELECT * FROM `test_db` WHERE `test_db`.`Chapter` LIKE :chapter ORDER BY `id` ASC");
$chapter= (isset($_GET['chapter']) === true) ? $chapter= htmlentities($_GET['chapter']) : die(header('Location:..'));
$query->bindValue(':chapter', '%'. $chapter . '%', PDO::PARAM_STR);
$query->execute();
$number= $query->rowCount();
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="container">
<ul>
<script>
var page = 1;
if($(window).scrollTop() + $(window).height() == $(document).height()) {
page++;
var actual_count = "<?php echo $number; ?>";
if ((page-1)* 12 < actual_count){
$.ajax({
type: "POST",
url: "data.php",
data:{ 'page': page },
success: function(res) {$(".container ul").append(res);}
});
}
}
</script>
</ul>
</div>
</body>
</html>
data.php :
<?php
include("includes/init.php");
$page= $_POST['page'];
$articles = (($page- 1) * 12) . ", 12";
$articles=$query->prepare("SELECT * FROM `test_db` WHERE `test_db`.`Chapter` LIKE :chapter ORDER BY `id` ASC LIMIT $articles");
$chapter= (isset($_GET['chapter']) === true) ? $chapter= htmlentities($_GET['chapter']) : die(header('Location:..'));
$query->bindValue(':chapter', '%'. $chapter . '%', PDO::PARAM_STR);
$articles->execute();
$rows= $articles->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row){
echo "<li>".$row['Name']."-".$row['Description']."</li>";
};
exit;
?>