Welcome Guest, Not a member yet? Register   Sign In
How to loop the $data in a view/template?
#1

[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!
#2

[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;
#3

[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?
#4

[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
#5

[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!
#6

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

[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.
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB