|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
MaximuM Smarty n00b
Joined: 27 Jul 2015 Posts: 2
|
Posted: Mon Jul 27, 2015 10:09 pm Post subject: Notice: Undefined offset: |
|
|
Hi
guys why i have this error ?
Code: |
Notice: Undefined offset in [b]templates_c\d97f176212804904824c4fd059a709b03891d7b2_0.file.banlist.html.cache.php on line 116[/b]
|
the line 16 is :
Code: |
<th><?php echo $_smarty_tpl->tpl_vars['bans']->value[$_smarty_tpl->getVariable('smarty')->value['section']['sec1']['index']]['userID'];?>
|
i use smarty 3
and this is my index.php :
Code: |
<?php
if (empty($_GET["p"])) {
header("location: ?p=check");
exit;
}
require '../libs/SmartyBC.class.php';
$smarty = new SmartyBC;
Switch($_GET["p"])
{
Case $_GET["p"] == "banlist":
include_once(.'/include/banlist.php');
break;
default:
break;
}
$smarty->display($_GET["p"] . '.html');
?>
|
and my banlist.php :
Code: |
<?php
include_once('./include/db.php');
$bansql = "SELECT * FROM ban";
$banresult = $db->sql_query($bansql);
while($row = $db->sql_fetchrow($banresult)) {
$smarty->assign("bans", array("userID" => $row['member_id'],
"name" => $row['name'],
"email" => $row['email'],
"hwid" => $row['serialnumber'],
"reason" => $row['pf_can_play_reason']
));
}
?>
|
and banlist.html is:
Code: |
<thead>
<tr>
<th>#</th>
<th>userID</th>
<th>Name</th>
<th>E-mail</th>
<th>HWID</th>
<th>reason</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{section name=sec1 loop=$bans}
<tr>
<th></th>
<th>{$bans[sec1].userID}</th>
<th>{$bans[sec1].name}</th>
<th>{$bans[sec1].email}</th>
<th>{$bans[sec1].hwid}</th>
<th>{$bans[sec1].reason}</th>
<th>Actions </th>
</tr>
{/section}
</tbody>
</table>
|
|
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Tue Jul 28, 2015 12:08 am Post subject: |
|
|
You're assig value to "$bans", then trying to read it from "$bans[sec1]".
Makes no sense. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Tue Jul 28, 2015 2:37 am Post subject: |
|
|
AnrDaemon wrote: | You're assig value to "$bans", then trying to read it from "$bans[sec1]".
Makes no sense. |
That is the way {section} loops are working.
Your comment did again not address the problem!
The problem is the $smarty->assign() in your while loop. It does overwrite the template variable 'bans' again and again just with the value of the last fetched row.
Fix:
Code: | <?php
include_once('./include/db.php');
$bansql = "SELECT * FROM ban";
$banresult = $db->sql_query($bansql);
$bans = array();
while($row = $db->sql_fetchrow($banresult)) {
$bans[] = array("userID" => $row['member_id'],
"name" => $row['name'],
"email" => $row['email'],
"hwid" => $row['serialnumber'],
"reason" => $row['pf_can_play_reason']
);
}
$smarty->assign("bans", $bans);
?> |
I suspect that your $db object has also a method like sql_fetchall() which does return an array of all rows.
Then you could do something like:
Code: | $smarty->assign("bans", $db->fetchall($banresult)); |
Note: {section} in this use case has some bad performance.
Better:
Code: | {foreach $bans as $row}
<tr>
<th></th>
<th>{$row.userID}</th>
<th>{$row.name}</th>
<th>{$row.email}</th>
<th>{$row.hwid}</th>
<th>{$row.reason}</th>
<th>Actions </th>
</tr>
{/foreach} |
|
|
Back to top |
|
MaximuM Smarty n00b
Joined: 27 Jul 2015 Posts: 2
|
Posted: Tue Jul 28, 2015 10:55 am Post subject: |
|
|
Thank you |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|