1

I'm making a catalogue of clubs and I'd like to limit every page to 10 clubs for example, but I can't wrap my head around it. I've tried SmartyPaginate, but it doesn't work (spoke to the developer, told me not to use it).

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}

I'd like to get Next and Previous and show only X amount of records. How can I go about this ?

observer
  • 2,925
  • 1
  • 19
  • 38

1 Answers1

0

In your current TRY statement, before all you should get your current page (suppose from the query string)

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

and then define the clubs to fetch from the 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");

Basically you are getting 10 records from 0th record for page 1, 10 records from 9th record for page 2, etc.

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

and of caurse put links PREV and NEXT in the smarty template. Hint hide PREV in page 1 and hide NEXT in the last page

<a href="your_url.php?page=$page-1">PREV</a>
<a href="your_url.php?page=$page+1">NEXT</a>
Yasen Zhelev
  • 4,045
  • 3
  • 31
  • 56
  • can't get it to work, my code looks like this: clubs.php http://pastebin.com/UM1d3pzs clubs.tpl http://pastebin.com/36p1Jvte I've limited it to "1" on purpose to check if it works, because I don't have 10 records yet. I don't get any errors, but the counter doesn't go further than 0 and 1 and if I manually go to ?page=2, I don't see anything. –  Jun 27 '11 at 18:09
  • Ok, that is because you are still using $start_from = $page == 1 ? 0 : (($page - 1) * 10 - 1); use $start_from = $page == 1 ? 0 : (($page - 1) * 1 - 1); instead. 10 is the nuber of clubs to show on a page, decrease it to 1 as the LIMIT in SQL query – Yasen Zhelev Jun 28 '11 at 08:10
  • I also have to say that I do nto like how you do $page--; $page++; I know that we end with the original $page value, but it is not correct to change $page value. I would prefer to have it my way ($page-1) and ($page+1). :) – Yasen Zhelev Jun 28 '11 at 08:13
  • $page+1 and $page-1 won't work for some reason :/ no errors, but it just won't link. –  Jun 28 '11 at 22:51