与Smarty一起分页(上一页|下一页)

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (64)

我正在制作一个俱乐部目录,我想限制每个页面到10个俱乐部,但我无法绕过它。我尝试过SmartyPaginate,但它不起作用(与开发人员交谈,告诉我不要使用它)。

clubs.php

<?php

include('configs/pdo.inc.php');
include('libs/Smarty.class.php');

// create object
$smarty = new Smarty;

// Clubs ophalen
try {
    $query = $oPDO->prepare("SELECT * FROM V_clubs WHERE Zichtbaar = 1 ORDER BY ID ASC LIMIT 10");
    $query->execute();

    $t = array();
    foreach ($query as $row) {
        $t[$row['ID']] = $row;
    }

    $smarty->assign('clubs', $t);

    // Categorieen ophalen
    $categorie = $oPDO->query("SELECT * FROM t_categorie ORDER BY D_categorie ASC");
    $smarty->assign('categorie', $categorie);
    // Provincies ophalen
    $prov = $oPDO->query("SELECT * FROM t_provincies ORDER BY D_provincie ASC");
    $smarty->assign('prov', $prov);
    // Clubteller
    $xclubs = $oPDO->prepare("SELECT ID from V_clubs");
    $xclubs->execute();
} catch (PDOException $e) {
    echo '<pre>';
    echo 'Regelnummer: ' . $e->getLine() . '<br>';
    echo 'Bestand: ' . $e->getFile() . '<br>';
    echo 'Foutmelding: ' . $e->getMessage() . '<br>';
    echo '</pre>';
}

// display it
$smarty->display('extends:layout.tpl|header.tpl|clubs.tpl|footer.tpl');
?>

clubs.tpl

{extends file="layout.tpl"}
{block name=title}Clubs{/block}
{block name=content}
<form name="clubsearch" method="POST" action="{$SCRIPT_NAME}">
    <div class="span-6">
        <p><label for="categorie">Categorie:</label><br />
            <select id="categorie" name="categorie">
                <option value="*">Alle disciplines</option>
        {foreach $categorie as $c}
                <option value="{$c.D_categorie}">{$c.D_categorienaam}</option>
        {/foreach}
            </select></p>
    </div>
    <div class="span-4">
        <p><label for="provincie" name="provincie">Provincie:</label><br />
            <select id="provincie" name="provincie">
                <option value="*">Alle provincies</option>
        {foreach $prov as $p}
                <option value="{$p.D_provincie}">{$p.D_provincienaam}</option>
        {/foreach}
            </select><p>
    </div>
    <div class="span-4">
        <p><label for="gemeente">Gemeente:</label><br />
            <select id="gemeente" name="gemeente">
                <option value="*">Alle gemeentes</option>
        {foreach $clubs as $c}
                <option value="{$c.Gemeente}">{$c.Gemeente}</option>
        {/foreach}
            </select></p>
    </div>
    <div class="span-2">
        <input type="submit" name="zoekclub" id="zoekclub" value="Zoeken">
    </div>
</form>
<hr>
{if isset($smarty.get.id)}
<div class="span-6 colborder">
    <table>
        <tr>
            <td style="font-weight: bold;">Club</td>
        </tr>
        <tr>
            <td>{$clubs[$smarty.get.id].Naam}</td>
        </tr>
        <tr>
            <td style="font-weight: bold;">Categorie</td>
        </tr>
        <tr>
            <td>{$clubs[$smarty.get.id].Categorie}</td>
        </tr>
        <tr>
            <td style="font-weight: bold;">Provincie</td>
        </tr>
        <tr>
            <td>{$clubs[$smarty.get.id].Provincie}</td>
        </tr>
        <tr>
            <td style="font-weight: bold;">Gemeente</td>
        </tr>
        <tr>
            <td>{$clubs[$smarty.get.id].Gemeente}</td>
        </tr>
        <tr>
            <td style="font-weight: bold;">Website</td>
        </tr>
        <tr>
            <td><a href="{$clubs[$smarty.get.id].Contact}" target="_blank">{$clubs[$smarty.get.id].Contact}</a></td>
        </tr>
    </table>
</div>
<div class="span-8 last">
    <table>
        <tr>
            <td style="font-weight: bold;">Info</td>
        </tr>
        <tr>
            <td>{$clubs[$smarty.get.id].Extra}</td>
        </tr>
    </table>
</div>
<div class="span-4 first"><p><a onClick="history.go(-1)"><< Terug</a></p></div>
{else}
<table>
    <tr>
        <th>Club</td>
        <th>Categorie</td>
        <th>Provincie</td>
        <th>Gemeente</td>
    </tr>
{foreach $clubs as $c}
    <tr>
        <td><a href="{$SCRIPT_NAME}?id={$c.ID}"><b>{$c.Naam}</b></a></td>
        <td>{$c.Categorie}</td>
        <td>{$c.Provincie}</td>
        <td>{$c.Gemeente}</td>
    </tr>
{/foreach}
</table>
{/if}
{/block}

我想获得Next和Previous,只显示X量的记录。我怎么能这样做?

提问于
用户回答回答于

在您当前的TRY语句中,在您应该获取当前页面之前(假设来自查询字符串)

$page = empty($_GET['page']) ? 1 : (int)$_GET['page'];

然后定义要从DB获取的球杆

$start_from = $page == 1 ? 0 : (($page - 1) * 10 - 1);
$query = $oPDO->prepare("SELECT * FROM V_clubs WHERE Zichtbaar = 1 ORDER BY ID ASC LIMIT ".$start_from.", 10");

基本上你从第1页的第0条记录获得10条记录,从第2页的第9条记录获得10条记录,等等。

page 1 LIMIT 0,10
page 2 LIMIT 9,10
page 3 LIMIT 19,10

并且在智能模板中将链接PREV和NEXT放在一起。提示在第1页隐藏PREV并在最后一页隐藏NEXT

<a href="your_url.php?page=$page-1">PREV</a>
<a href="your_url.php?page=$page+1">NEXT</a>

扫码关注云+社区

领取腾讯云代金券