Arrays and Sections
| From: | Chris Hubbard | Date: | Sun, 30 Dec 2001 18:10:42 +0000 |
| Subject: | Arrays and Sections | ||
| Groups: | php.smarty.general | ||
| Request: | Send a blank email to php-smarty-general+get-2446@lists.php.net to get a copy of this message | ||
All,
OK, I'm stuck.
I've got a simple table
status // table name
status_id INT(10) auto_increment // table index
status_name varchar (50)
And I want to display the values in the table to the browser. The values currently are:
status_id status_name
1 Open
2 Closed
3 Assigned
I've got the following code:
// connect to the database code goes here.
$tpl = new Smarty;
$tpl->debugging = true;
if ($conn->query("SELECT * FROM status")!=0)
{
while (!$conn -> movenext())
{
$data[$conn -> field("status_id")] = $conn -> field("status_name");
}
$tpl->assign('data', $data);
}
$tpl->display('mysql.tpl');
which produces the array $data as which I can see in a print_r ($data) call:
data:Array
(
[1] => Open
[2] => Closed
[3] => Assigned
)
Now the real work is done in template, and that's where I'm stuck. I tried using a
foreach, but then realized that you can't do multidimensional arrays with for each. I was able
to print to the browser either the status_id or status_name but not both. So I'm trying to get
sections to work.
Here's what I've tried:
Attempt 1
{section name=i loop=$data}
{$data[i].status_id}<br>
<b>{$data[i].status_name}</b><br>
{sectionelse}
there are no values to loop through
{/section}
Result 1
O
O
C
C
Attempt 2
{section name=i loop=$data}
{$data[status_id]} - <b>{$data[status_name]}</b><br>
{sectionelse}
there are no values to loop through
{/section}
Result 2
-
-
-
Attempt 3
{section name=i loop=$data}
{$data[i]} - <b>{$status_name[i]}</b><br>
{sectionelse}
there are no values to loop through
{/section}
Result 3
-
Open -
Closed -
I'm completely stumped. Any suggestions?
Thanks in advance,
Chris