duandui5648 2019-07-31 02:13
浏览 83

用PDO,AJAX分页

I am trying to set up pagination where the questions and answers are from separate tables. If I don't echo $question->questions() nothing shows up.

AJAX: (I have created another function just like this for the answers)

function loadQuestion(question) {

$.ajax({
    url: "question.php",
    method: "POST",
    data: {question:question},
    success:function(data) {
        $('#questions').html(data);
    }
});
}

PHP:

public function question() {
$sql = "SELECT * FROM questions";
$stmt = $db->prepare($sql);
$stmt->execute();

$questionPerPage = 1;
$totalNumberOfQuestions = $stmt->rowCount();

$totalNumberOfQuestions = ceil($totalNumberOfQuestions / 
$questionPerPage);
if(isset($_POST['question'])) {
$questionNo = $_POST['question'];
} else {
$questionNo = 1;
}

$thisPageQuestion = ($questionNo - 1) * $questionPerPage;

$sql = "SELECT * FROM questions LIMIT ".$thisPageQuestion. ',' .$questionPerPage. 'ORDER BY id ASC' ; 
$stmt = $db->prepare($sql);
$stmt->execute();
$questions = $stmt->fetchAll();
$output = '';
foreach($questions as $question) {
echo "<h2>" .$question['question']."</h2>";
}

HTML:

<button class="previous" name="previous"><-Back</button>
<section id="questions">
</section>
<section id="answers">
</section>
<button type="submit" class="next" name="next">Next-></button>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序
    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题