Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Problem with multidimensional array

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
driverfiles
Smarty Rookie


Joined: 10 Aug 2003
Posts: 26
Location: Epe, The Netherlands

PostPosted: Sun Aug 10, 2003 10:17 pm    Post subject: Problem with multidimensional array Reply with quote

HI ! Smile

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 Sad 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 Smile

Thanx

Johan
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sun Aug 10, 2003 10:25 pm    Post subject: Re: Problem with multidimensional array Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
driverfiles
Smarty Rookie


Joined: 10 Aug 2003
Posts: 26
Location: Epe, The Netherlands

PostPosted: Sun Aug 10, 2003 10:49 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website MSN Messenger
driverfiles
Smarty Rookie


Joined: 10 Aug 2003
Posts: 26
Location: Epe, The Netherlands

PostPosted: Sun Aug 10, 2003 11:06 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Sun Aug 10, 2003 11:37 pm    Post subject: Reply with quote

[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
View user's profile Send private message Send e-mail Visit poster's website
driverfiles
Smarty Rookie


Joined: 10 Aug 2003
Posts: 26
Location: Epe, The Netherlands

PostPosted: Sun Aug 10, 2003 11:50 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Aug 11, 2003 6:30 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
driverfiles
Smarty Rookie


Joined: 10 Aug 2003
Posts: 26
Location: Epe, The Netherlands

PostPosted: Tue Aug 12, 2003 9:54 am    Post subject: Reply with quote

messju, I think I already figured it out, thanks a lot for your help Smile
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP