Forum » Server-Side scripting » Smarty foreach fout
Hardstyle
Berichten: 225
avatar
Offline Stuur privebericht
Hallo allemaal,

Ik heb een probleem met smarty.
Ik heb een foreach met $i--; erin.
Als ik dit probeer:

index.php
Code | Selecteer Alles
minimaliseren
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
// Copyright Messinagame 2011
// Created by Youri van Mill

require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/configuration.php';

if(!isset(
$_SESSION['name']))
{
    
header('location: /login.php');
    die;
}

$smarty = new Smarty();

$smarty->template_dir 'inc/templates/';
$smarty->compile_dir 'inc/templates/compile';
$smarty->config_dir 'inc/templates/configs';
$smarty->cache_dir 'inc/templates/cache';

$smarty->assign('name'$session['name']);

$max_items 2;

$begin = ($_GET['p'] >= 0) ? $_GET['p'] * $max_items 0;

// Create the first query
$stmt $dbh->query('SELECT * 
                     FROM ' 
NEWS_TABLE '
                     ORDER BY `date` ASC
                     LIMIT ' 
$begin ', ' $max_items);

$result = array();

while(
$res $stmt->fetch(PDO::FETCH_ASSOC)) 

    
$result[] = $res;
}

$smarty->assign('result'$result);

// End the first query
$stmt null;

// Create the second query
$stmt $dbh->query('SELECT * 
                     FROM ' 
NEWS_TABLE);

$smarty->assign('navigation'range(1ceil($stmt->rowCount() / $max_items)));

// End the second query
$stmt null;

$smarty->display('index.tpl');
?>


index.tpl
Code | Selecteer Alles
minimaliseren
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"

<
html xmlns="http://www.w3.org/1999/xhtml">

<
head
  <
meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> 
  <
title
  
xxxx
  
</title
 
  <
link rel="stylesheet" href="/static/style/style.css" type="text/css" />
  <
link rel="stylesheet" href="/static/style/tooltip.css" type="text/css" />
  
  <
script src="/static/js/functions.js" type="text/javascript"></script>
</head> 
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
        <td style="width:14%;vertical-align:top;">
{include file='menu-left.tpl'}
        </td>
        <td style="width:72%;vertical-align:top;">
            <table style="border:none;width:96%;background:#000000;" align="center" cellpadding="2" cellspacing="1"> 
                <tr>
                    <td class="header"> 
                        &nbsp;
                    </td>
                </tr>
                <tr> 
                    <td class="subheader">
                        <table style="border:none;width:100%;margin:0px;" cellspacing="0" cellpadding="0"> 
                            <tr>
                                <td style="text-align:left;">
                                    Welkom terug {$name|ucfirst}.
                                </td>
                                <td style="text-align:right;">
                                    <b>Je kan weer stemmen voor 100 kogels!</b>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            
            <table style="border:none;width:60%;background:#000000;" align="center" cellpadding="2" cellspacing="1">
                <tr> 
                    <td class="textTitle">
                        Nieuws pagina
                    </td> 
                </tr>
{foreach $result as $res}
                <tr>  
                    <td class="text"> 
                        <span style="font-size:20px;">{$res.title|ucfirst}</span>
                        
                        <span style="font-size:10px;">Geschreven op {$res.date|date_format:'%d %B %Y om %H:%S'}</span>
                        
                        {$res.message|stripslashes}
                    </td>
                </tr>
{foreachelse}
                <tr>  
                    <td class="text"> 
                        Er nog geen nieuws.
                    </td>  
                </tr>
{/foreach}
                <tr>
                    <td class="text">
                        Pagina 
{foreach $navigation as $i}
    {$i--}
                        <a href="/index.php?={$i}">{($i+1)}</a>
{/foreach}
                    </td>
                </tr>
            </table>
            
            <center>Copyright Messinagame 2011</center>
        </td>
        <td style="width:14%;vertical-align:top;">
{include file='menu-right.tpl'}
        </td>
    </tr>
</table>
</body>
</html>


Er staan 6 nieuwsberichten in de database (dus 6 records)

Ik heb LIMIT op 2 gezet dus hij laat er dan maar 2 zien dat werkt.

Hij zou dus eigenlijk dit moeten weergeven:

Code | Selecteer Alles
minimaliseren
1
Pagina 1 2 3


Maar hij laat dit zien:

Code | Selecteer Alles
minimaliseren
1
Pagina 1 1 2 2 3 3


Wat doe ik fout, wie kan me helpen?
20-02-2011 03:11
Dit topic is 199 keer bekeken door 30 verschillende leden
Reacties op: "Smarty foreach fout"
1
Vreemd
Berichten: 1246
avatar
Offline Stuur privébericht
Code | Selecteer Alles
minimaliseren
1
2
3
{foreach $navigation as $i}
                        <
a href="/index.php?={($i-1)}">{$i}</a>
{/foreach}


Zo misschien?
Ik weet niet veel van smarty, maar het lijkt me dat {$i--} geprint werd.
20-02-2011 12:23
Hardstyle
Berichten: 225
avatar
Offline Stuur privébericht
Dankjewel hij werkt nou

Bedankt!

20-02-2011 13:01
Reageer op: "Smarty foreach fout"
1
Je kan niet reageren omdat je niet bent ingelogd. Inloggen of Aanmelden