Re: Arrays and Sections
| From: | Andrei Zmievski | Date: | Mon, 31 Dec 2001 14:26:30 +0000 |
| Subject: | Re: Arrays and Sections | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-2454@lists.php.net to get a copy of this message | ||
It's not possible to do this with Smarty version before 1.5.0. If you
are using one of those, you have to pass the id and name as two arrays.
If you are using 1.5.0 or later, use {foreach} construct.
{foreach key=key item=item from=$data}
{$key} - {$item}
{/foreach}
On Sun, 30 Dec 2001, Chris Hubbard wrote:
> 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
>
>
>
> ---
> You are currently subscribed to smarty as: andrei@ispi.net
> To unsubscribe send a blank email to $subst('Email.Unsub')
> Visit Smarty's homepage at
> http://www.phpinsider.com/php/code/Smarty/
-Andrei
"Tomorrow the sun will rise. And who knows what the tide will bring?"
- Tom Hanks, in "Cast Away"