Foreach in smarty
Ik heb een foreach loop in smarty met $i--;
Hoe kan ik die gebruiken in smarty?
Heeft geen zin, dat werkt niet.
Wie kan me helpen?
Gewijzigd op 20/02/2011 11:38:48 door Youri van Mill
Ik post de index.php en de index.tpl wel, misschien kunnen jullie daar wat mee.
index.php:
Code (php)
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
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(1, ceil($stmt->rowCount() / $max_items)));
// End the second query
$stmt = null;
$smarty->display('index.tpl');
?>
// 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(1, ceil($stmt->rowCount() / $max_items)));
// End the second query
$stmt = null;
$smarty->display('index.tpl');
?>
index.tpl
Code (php)
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
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">
</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>
"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">
</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>
Hij moet dus eigenlijk Pagina 1 2 3 outputten.
Maar hij laat Pagina 1 1 2 2 3 3 zien.
Wat doe ik fout?