在Ajax / PHP / JQuery中实现分页功能

时间:2018-08-27 11:00:50

标签: php jquery ajax

我正在尝试执行分页功能。到目前为止,我设法根据数据库中的数据号生成功能。

我正在使用MariaDB。问题是,当我尝试单击第2页时,该参数在url中正确返回:xxx.php?page = 2,但数据未更新!

页面始终显示相同的数据。日志中没有显示错误

<?php
require('config/config.php');

if(isset($_SESSION['flash']))
{
    foreach($_SESSION['flash'] as $type => $message)
    {
        echo $message;
    }
    unset($_SESSION['flash']);
}

$output = '';
$limit_per_page = 5;
$page = "";
if(isset($_POST["query"]))
{
    $query = $cnx->prepare("SELECT * FROM candidacies WHERE lastName LIKE :lastName OR firstName LIKE :firstName OR age LIKE :age OR scheduleRange LIKE :scheduleRange OR phoneNumber LIKE :phoneNumber OR email LIKE :email OR candidacyType LIKE :candidacyType");
    $element = "%".$_POST['query']."%";
    $query->bindValue(':lastName', $element, PDO::PARAM_STR);
    $query->bindValue(':firstName', $element, PDO::PARAM_STR);
    $query->bindValue(':age', $element, PDO::PARAM_INT);
    $query->bindValue(':scheduleRange', $element, PDO::PARAM_STR);
    $query->bindValue(':phoneNumber', $element, PDO::PARAM_STR);
    $query->bindValue(':email', $element, PDO::PARAM_STR);
    $query->bindValue(':candidacyType', $element, PDO::PARAM_STR);
}
else if(isset($_POST['entries']))
{
    $entries = intval($_POST['entries']);
    $query = $cnx->prepare("SELECT * FROM candidacies LIMIT :entries");
    $query->bindValue(':entries', $entries, PDO::PARAM_INT);
}
else
{       
    $page = (!empty($_GET['page']) ? $_GET['page'] : 1);
    $start = ($page - 1) * $limit_per_page;

    $query = $cnx->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM candidacies LIMIT :limit_per_page OFFSET :start");
    $query->bindValue(':limit_per_page', $limit_per_page, PDO::PARAM_INT);
    $query->bindValue(':start', $start, PDO::PARAM_INT);
}

$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
$count = $query->rowCount();

$resultFoundRows = $cnx->prepare('SELECT found_rows()');
$resultFoundRows->execute();
$listElements = $resultFoundRows->fetchColumn();

if($count > 0)
{
    $output .= '
        <table class="table table-striped" id="candidaciesTable">
        <thead class="thead-light">
            <tr>
                <th scope="col">firstName</th>
                <th scope="col">lastName</th>
                <th scope="col">Âge</th>
                <th scope="col">Schedule Range</th>
                <th scope="col">Phone number</th>
                <th scope="col">Email</th>
                <th scope="col">Candidacy Type</th>
                <th scope="col">Action</th>
            </tr>
        </thead>
        <tbody>
    ';
    foreach($result as $key => $value)
    {

        $output .= "
            <tr>
                <td>".htmlspecialchars($value["firstName"])."</td>
                <td>".htmlspecialchars($value["lastName"])."</td>
                <td>".htmlspecialchars($value["age"])."</td>
                <td>".htmlspecialchars($value["scheduleRange"])."</td>
                <td>".htmlspecialchars($value["phoneNumber"])."</td>
                <td>".htmlspecialchars($value["email"])."</td>
                <td>".htmlspecialchars($search["candidacyType"])."</td>
                <td>
                    <div class='btn-group'>
                        <button class='btn btn-light btnViewCandidacy' id=".$value['id'].">
                            <i class='far fa-eye'></i>
                        </button>
                        <button class='btn btn-info' id='btnEditCandidacy' onclick='editCandidacy(".$value['id'].")'>
                            <i class='far fa-edit'></i>
                        </button>
                        <button class='btn btn-danger' id='btnDeleteCandidacy' onclick='deleteCandidacy(".$value['id'].")'>
                            <i class='far fa-trash-alt'></i>
                        </button>
                    </div>
                </td>
            </tr>
        ";
    }
    $output .= "
            </tbody>
        </table>";

    $output .= '
        <div class="pagination-centered">
            <ul>'
        ?>
        <?php 
            $numberPages = ceil($listElements / $limit_per_page);
            if($page > 1):
                $output .= '<a href="?page='. ($page - 1).'">Prev</a> —';
            endif;
            for ($i = 1; $i <= $numberPages; $i++):
                $output .= '<a href="?page='.$i.'">'.$i.'</a>';
            endfor;
            if($page < $numberPages):
                $output .= '<a href="?page='.($page + 1).'">Next</a>';
            endif;
            $output .= '
            </ul>
        </div>';
    echo $output;
}
else
{
 echo '<div class="alert alert-info">No results found !</div>';
}
?>

<script>
 $(document).ready(function()
 {
     loadData();

     function loadData(query)
     {
         $.ajax({
             url:"search.php",
             method:"POST",
             data:{query:query},
             success:function(data)
             {
                 $('#result').html(data);
             }
        });
    }
 }
</script>

0 个答案:

没有答案