CodeIgniter Forums
How to loop the $data in a view/template? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to loop the $data in a view/template? (/showthread.php?tid=5411)



How to loop the $data in a view/template? - El Forum - 01-21-2008

[eluser]agartzia[/eluser]
Hi there! I've just discovered CI and I think it's amazing!

I need to loop inside a template all along the array. The structure is as shown:

Code:
$data[0][id] = 1
$data[0][name] = John Doe
$data[0][details][address] = Fake Str. 123
$data[0][details][phone] = 555-123456
$data[0][last] = 2801002101

$data[1][id] = 1
$data[1][name] = Martin Perry
$data[1][details][address] = Lied Str. 321
$data[1][details][phone] = 555-1654862
$data[1][last] = 28023323421

And so on...

The problem would be solved if I could iterate the array, but CI gives me the keys instead of the array itself, which is not very useful for me.

I've thought I might add a key...
Code:
$this->load->view('body', array('main_data' => $data));
...but I don't think it's a proper way to solve it.

Is there a way to access the $data itself?

Thanks! Keep it on!


How to loop the $data in a view/template? - El Forum - 01-21-2008

[eluser]xwero[/eluser]
You are on the good path. In your view you do something like this
Code:
<?php foreach(main_data as $member){ ?>
<p>Name : &lt;?php echo $member[name]; ?&gt;</p>
and so on ...
&lt;?php } ?&gt;



How to loop the $data in a view/template? - El Forum - 01-21-2008

[eluser]agartzia[/eluser]
I don't think I have it...

main_data isn't defined, neither as variable nor as constant.

Another hint? Can I access it without using it another array?


How to loop the $data in a view/template? - El Forum - 01-21-2008

[eluser]xwero[/eluser]
there is a mistake in my previous snippet
Code:
&lt;?php foreach($main_data as $member){ ?&gt;
<p>Name : &lt;?php echo $member[name]; ?&gt;</p>
and so on ...
&lt;?php } ?&gt;
I forgot the dollar sign before main_data


How to loop the $data in a view/template? - El Forum - 01-22-2008

[eluser]agartzia[/eluser]
Thanks, but it's not what I'm looking for.

I gotta make the
Code:
$this->load->view('body', array('main_data' => $data));
in order to loop throught the $data or there's some other way to do it?

Thanks!


How to loop the $data in a view/template? - El Forum - 01-22-2008

[eluser]dawnerd[/eluser]
You are right. Do it that way and it should work like a charm.


How to loop the $data in a view/template? - El Forum - 01-22-2008

[eluser]xwero[/eluser]
[quote author="agartzia" date="1201005938"]
I gotta make the
Code:
$this->load->view('body', array('main_data' => $data));
in order to loop throught the $data or there's some other way to do it?
[/quote]
you can use
Code:
$body['main_data'] = $data;
$this->load->view('body', $body);
if you find that more convenient.


How to loop the $data in a view/template? - El Forum - 01-22-2008

[eluser]agartzia[/eluser]
OK, thanks for your help!