• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie: how to acces array values?

#1
[eluser]victor76[/eluser]
Hi,

I've got this Model function:
Code:
function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
                
    }

...and this Controller function:
Code:
<?php
class Blog_controller extends Controller {

    function blog()
    {
        $this->load->model('Blogmodel','',TRUE);

        $data['query'] = $this->Blogmodel->get_last_ten_entries();

        $this->load->view('blogview', $data);
    }
}
?>

In my View, I've used a simple print_r($data) to see if I would get anything from my DB.
This is wat it spits out:

Code:
Array
(
    [view] => blogview
    [vars] => Array
        (
            [query] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 1
                            [title] => 1st entry
                            [content] => blabla
                            [date] => 2007-10-18
                        )

                    [1] => stdClass Object
                        (
                            [id] => 2
                            [title] => 2nd entry
                            [content] => blabla
                            [date] => 2007-10-26
                        )

                )

        )

    [return] =>
)

...and now for my newbie question Smile
How can I acces these array values in my View?

I would like to display a simple <li>&lt;?php echo $data[0]['content']; ?&gt;</li>

But somehow this is not working...
Can anyone point me in some direction? Thnx!

#2
[eluser]nirbhab[/eluser]
foreach($data as $datavalue)
{
echo '<li>'.$datavalue.'</li>';
}

or

for($i=0;$i<sizeof($data);$i++)
{
echo $data[$i];
}

accessing 2d array:

use $data['id'];
and $data[0]['id'];

i think this might help you out.

#3
[eluser]victor76[/eluser]
Thnx, but this didn't help:

When I use:
Code:
foreach($data as $datavalue)
{
echo ‘<li>’.$datavalue.’</li>’;
}

I only get 'views' and 'vars' in my <li>, which is ofcourse the first key in the array.
When I try to get deeper in the array with [0]['id'] , I get this error notice. saying:
"Undefined index: id"...

I read somewhere on this forum that the DB Get()method in the model, returns an object, instead of an Array..but still have no idea how to get the values back in my <li>...

Any ideas?

#4
[eluser]Matthew Pennell[/eluser]
You shouldn't really have a $data variable in your view - the array keys you assign in the controller ('query', in your example) are the ones you use in the view to retrieve your data, so your view should look something like this:

Code:
&lt;?php foreach ($query as $item): ?&gt;
    <li><b>&lt;?= $item->title ?&gt;</b> - &lt;?= $item->data ?&gt;</li>
&lt;?php endforeach; ?&gt;

Note that you use object notation (->) instead of array notation as you're using result() instead of result_array().

#5
[eluser]victor76[/eluser]
Thank you so much! Smile

#6
[eluser]obiron2[/eluser]
Quote:
Code:
&lt;?php foreach ($query as $item): ?&gt;
    <li><b>&lt;?= $item->title ?&gt;</b> - &lt;?= $item->data ?&gt;</li>
&lt;?php endforeach; ?&gt;

or go one stage further..

Code:
&lt;?php foreach ($query as $item)?&gt;
<li>
   &lt;?php for each $item as $key=>$val: ?&gt;
    <b>&lt;?= $key ?&gt; :: &lt;?= $val?&gt; , &lt;?= endforeach;
&lt;?php endforeach; ?&gt;

The data you pass to the view is an array of objects ($data).
Each of these objects can also be an array of objects ($query).
Each of these objects can also be an array of object .... ad infinitum ($item)

You can then access this bottom level object either using the first method using object notation, this requires that you know the property names, or using the second method where the property names get converted to $key and the values get converted to $value.

Generally I find the first more useful for 'live' code but the second for debugging because you get to see all the properties, especially useful if one of the properties itself is an array or object because you then know you have to iterate again....


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.