使用数据

时间:2017-11-11 21:06:37

标签: php pagination

我正在尝试为我的项目添加分页。 在我进入页面的那一刻,分页从Page = 0开始,我可以浏览页面。

URL如下所示:*domain*.com/users/?pageNr=0(使用GET方法按钮更改页码)

输入搜索输入后,网址将更改为:*domain*.com/users/?input=JohnDoe&action=search

但是当我尝试在页面之间移动时,搜索值会从网址中删除,我会再次看到未经过滤的数据(pageNr=0)。

是否可以在网址中保留搜索值并将pageNr附加到其中,还是应该更改我的分页方法? 编辑:数据

$action = filter_input(INPUT_GET,'action',FILTER_SANITIZE_STRING);
$input = filter_input(INPUT_GET,'input',FILTER_SANITIZE_STRING);

$pageNr = filter_input(INPUT_GET, 'pageNr', FILTER_VALIDATE_INT);
$next = $pageNr+1;
$previous = $pageNr-1;

if(empty($pageNr)) {
    $pageNrInDb = 0;
} else {
    $pageNrInDb = $pageNr * MAX_USERS_ON_PAGE;
}

$countUsers = User::count_all();

$pagesCount = ceil( $countUsers / MAX_USERS_ON_PAGE);

if (isset($action) && $action ==='search'){
    if (!User::find_by_username($input)) {
        echo infoMessage('info', 'No results to <b>' . $input . '</b>');
        $users = User::findAll($pageNrInDb, MAX_USERS_ON_PAGE);
    } else {
        $users = User::find_by_username($input);
    }
} else {
    $users = User::findAll($pageNrInDb, MAX_USERS_ON_PAGE);
}


?>

<div class="row">
    <div class="col-sm-4">
        <h3><a href="<?php echo ADMIN_URL . "?page=user"; ?>"><span class="glyphicon glyphicon-plus-sign"></span> <?php echo translateX('Add') ?></a></h3>
    </div>
    <div class="col-sm-4">
        <form method="get">
            <div class="input-group">
                <input type="text" class="form-control" name="input" value="<?php echo isset($input) ? $input : ''; ?>" placeholder="Search by username">
                <span class="input-group-btn">
                    <button class="btn btn-secondary" name="action" value="search" type="submit"><span class="glyphicon glyphicon-search"></span></button>
                </span>        
            </div>
        </form>
    </div>
    <div class="col-sm-4">
        <h3 class="text-right"><?php echo translateX($pages[$page]['name']) ?></h3>
    </div>
</div>

<div class="row">
    <div class="col-lg-12">
        <?php echo isset($session->message) ? $session->message : '' ?>
        <div>
            <?php if (!empty($users)) : ?>
                <table class="table">
                    <thead>
                    <tr>
                        <th><?php echo translateX("ID") ?></th>
                        <th><?php echo translateX("Email") ?></th>
                        <th><?php echo translateX("Added") ?></th>
                        <th><?php echo translateX("Language") ?></th>
                        <th><?php echo translateX("Rights") ?></th>
                        <th><?php echo translateX("Status") ?></th>
                        <th><?php echo translateX("Edit") ?></th>
                        <th><?php echo translateX("Delete") ?></th>

                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($users as $user) : ?>
                        <tr>
                            <td><?php echo $user->ID ?></td>
                            <td><?php echo $user->username ?></td>
                            <td><?php echo $user->added ?></td>
                            <td><?php echo $user->lang ?></td>
                            <td><?php echo $user->rights ?></td>
                            <td><?php echo $user->status ?></td>
                            <td><a href="<?php echo ADMIN_URL . "?page=user&ID=" . $user->ID; ?>"><span class="glyphicon glyphicon-pencil"></span></a></td>
                            <td><a>
                    <span
                            data-url = "pages/users/delete"
                            data-user-delete-id = "<?php echo $user->ID; ?>"
                            class="glyphicon glyphicon-trash user-delete-confirm" style="cursor: pointer;">
                     </span>
                                </a></td>

                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            <?php elseif(!empty(!$input)) :
                    echo infoMessage('info', 'No users');
            endif; ?>
        </div>
    </div>
</div>
<ul class="pager">
    <form method="get">
            <li>
                <div class="btn-group">
                    <?php $pageNr == 0 ? $disabled = "disabled='disabled'" : $disabled = ''; ?>
                    <button class="btn btn-default btn-xs" <?php echo $disabled ?> type="submit" name="pageNr" value="<?php echo $previous ?>"><span class="glyphicon glyphicon-arrow-left"></span></button>
                    <?php for ($i = 0;$i < $pagesCount;$i++) : ?>
                        <?php $pageNr == $i ? $active = 'active' : $active = '';?>
                        <button class="btn btn-default btn-xs <?php echo $active ?>" style="font-weight: bold;" type="submit" name="pageNr" value="<?php echo $i ?>"><?php echo $i+1 ?></button>
                    <?php endfor; ?>
                    <?php $pageNr == $pagesCount-1 ? $disabled = "disabled='disabled'" : $disabled = ''; ?>
                    <button class="btn btn-default btn-xs" <?php echo $disabled ?> type="submit" name="pageNr" value="<?php echo $next ?>"><span class="glyphicon glyphicon-arrow-right"></span></button>
                </div>
            </li>
    </form>
</ul>

1 个答案:

答案 0 :(得分:0)

您只需要使用您的分页表单发送初始值,请尝试以下操作:

<form method="get">
        <li>
            <div class="btn-group">
                <?php $pageNr == 0 ? $disabled = "disabled='disabled'" : $disabled = ''; ?>
                <button class="btn btn-default btn-xs" <?php echo $disabled ?> type="submit" name="pageNr" value="<?php echo $previous ?>"><span class="glyphicon glyphicon-arrow-left"></span></button>
                <?php for ($i = 0;$i < $pagesCount;$i++) : ?>
                    <?php $pageNr == $i ? $active = 'active' : $active = '';?>
                    <button class="btn btn-default btn-xs <?php echo $active ?>" style="font-weight: bold;" type="submit" name="pageNr" value="<?php echo $i ?>"><?php echo $i+1 ?></button>
                <?php endfor; ?>
                <?php $pageNr == $pagesCount-1 ? $disabled = "disabled='disabled'" : $disabled = ''; ?>
                <button class="btn btn-default btn-xs" <?php echo $disabled ?> type="submit" name="pageNr" value="<?php echo $next ?>"><span class="glyphicon glyphicon-arrow-right"></span></button>
            </div>
        </li>
        <!-- Add these lines -->
        <input type="hidden" name="action" value="<?= $action ?>">
        <input type="hidden" name="input" value="<?= $input ?>">
</form>

您可以看到我在表单中添加了2个隐藏字段,当页面发生变化时,它们会发送inputaction值。