View previous topic :: View next topic |
Author |
Message |
driverfiles Smarty Rookie
Joined: 10 Aug 2003 Posts: 26 Location: Epe, The Netherlands
|
Posted: Sun Aug 10, 2003 10:17 pm Post subject: Problem with multidimensional array |
|
|
HI !
Because I spend more then 2 hours on this problem I'm sick 'n tired of it and I was hoping that you guys could help me out.
Let me create a nice view of my problem. I have a table called companies which contains 2 fields, ID and company.
As I want to display the companies (as links) I want to print out a list of it, containing an url looking like this: <a href="?page=drivers&ID=1">Company</a>.
First I created an array containing all the information, this array looks like this:
$companies[ID] = 1;
$companies[company] = "Acer"
and next I assigned it like this, $smarty->assign("companies", $companies);
In the template I first tried to do this with a section, but it looked impossible to me, so I tried to do it with a for each loop. This one looks like this:
Code: |
<table class="dftable" width="100%"><tr>
{foreach from=$companies item=info}
<td>
<span class="devices"><a href="?page=drivers&ID={$info.ID}">{$info.company}</a></span>
</td>
{cycle values=",</tr><tr>"}
{/foreach}
</table>
|
But it simply doesn't work Since I'm a smarty newbie I'm not sure what I do wrong, please help me learn from my mistakes and tell me whats wrong
Thanx
Johan |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sun Aug 10, 2003 10:25 pm Post subject: Re: Problem with multidimensional array |
|
|
driverfiles wrote: |
$companies[ID] = 1;
$companies[company] = "Acer"
|
this won't lead to a two-dimensional array, but a 1-dimensional one. try:
Code: |
$company['ID'] = 1;
$company['company'] = "Acer"
$companies[] = $company;
|
then the foreach-loop you posted should work. |
|
Back to top |
|
driverfiles Smarty Rookie
Joined: 10 Aug 2003 Posts: 26 Location: Epe, The Netherlands
|
Posted: Sun Aug 10, 2003 10:49 pm Post subject: |
|
|
messju, thank you for your quick answer, I tried it but unforunately it doesn't work.
I now have the following php code:
Code: |
foreach ($res as $company)
{
$companies[company][$company->ID] = $company->company;
}
$companies[] = $companies;
$smarty->assign("companies", $companies);
|
And the following template code:
Code: |
{foreach from=$companies item=info key=key}
<td>
<span class="devices"><a href="?page=drivers&ID={$key}">{$info.company}</a>
</span>
</td>
{cycle values=",</tr><tr>"}
{/foreach}
|
What did I do wrong ? I also tried $companies[] = $company; but that didn't work either and I think it should be $companies[] = $companies;
And for learning purposes, what is the best way to access a multidimensional array in smarty ?
Thank-you
Johan |
|
Back to top |
|
driverfiles Smarty Rookie
Joined: 10 Aug 2003 Posts: 26 Location: Epe, The Netherlands
|
Posted: Sun Aug 10, 2003 11:06 pm Post subject: |
|
|
I now understand it a bit, I changed the template like this:
Code: |
<table class="dftable" width="100%"><tr>
{foreach from=$companies item=info key=key}
{foreach from=$info item=company key=key2}
<td>
<span class="devices"><a href="?page=drivers&ID={$key}">{$company}</a></span>
</td>
{cycle values=",</tr><tr>"}
{/foreach}
{/foreach}
</table>
|
and I can loop through the array with this. Now my only concern is the following. I have an array looking like this:
$company[ID] = 1
$company[company] = Compaq
Now when I use the code above I want to use $company[company] in the URL description and $company[ID] in the URL itself (like ?ID=1). But with the code above this isn't possible, because with ({$company}) I get all the values from [ID] but also from [company].
I would like to have this f.e <a href="?ID={$company.ID]">{$company[company]}</a> , could someone explain to me how that has to be done ? Thanks |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Sun Aug 10, 2003 11:37 pm Post subject: |
|
|
[php:1:15c739e804]
$companies = array();
foreach ($res as $company)
{
$companies[$company->ID] = $company->company;
}
$smarty->assign("companies", $companies);
[/php:1:15c739e804]
and in the template:
Code: |
{foreach from=$companies item=company key=id}
<td>
<span class="devices"><a href="?page=drivers&ID={$id}">{$company}</a>
</span>
</td>
{cycle values=",</tr><tr>"}
{/foreach}
|
*or*
[php:1:15c739e804]
$companies = array();
foreach ($res as $company)
{
$companies[] = array('name'=>$company->company, 'ID'=>$company->ID);
}
$smarty->assign("companies", $companies);
[/php:1:15c739e804]
and in the template:
Code: |
{foreach from=$companies item=company}
<td>
<span class="devices"><a href="?page=drivers&ID={$company.id}">{$company.name}</a>
</span>
</td>
{cycle values=",</tr><tr>"}
{/foreach}
|
the former is a bit leaner, but the latter scales if you have more than just two data-fields for each entry.
HTH
messju |
|
Back to top |
|
driverfiles Smarty Rookie
Joined: 10 Aug 2003 Posts: 26 Location: Epe, The Netherlands
|
Posted: Sun Aug 10, 2003 11:50 pm Post subject: |
|
|
Thanks a lot messju, that made a lot clear for me. Btw, I just used a <section> again to try it out, but whatever I tried I only got 2 companies back instead of 3500, mabey you know why that happened ? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Aug 11, 2003 6:30 am Post subject: |
|
|
i'd suggest you first of all do a "print_r($companies)" before the "$smarty->assign()"-call to check if everything is correct on the php-side. maybe you can show us how your array looks like here (if it has really >3000 elements, only show us the first few lines, of course!). |
|
Back to top |
|
driverfiles Smarty Rookie
Joined: 10 Aug 2003 Posts: 26 Location: Epe, The Netherlands
|
Posted: Tue Aug 12, 2003 9:54 am Post subject: |
|
|
messju, I think I already figured it out, thanks a lot for your help |
|
Back to top |
|
|