Welcome Guest, Not a member yet? Register   Sign In
Nested data arrays in CI views - howto access?

Im having major difficulties in prosessing nested MySQL data in my views. See sample data below.

Im pulling a set of RSS data from MySQL db to be processed and displayed for the user. The MySQL data is fetched using

function get_news($offset=5) {

        $query = $this->db->query("SELECT * FROM News ORDER BY pubDate DESC LIMIT $offset");
    return $query->result_array();        

I pass the data to a view, using:

$query = $this->db_model->get_news();
$this->load->view('news', $query);

I cannot, however, seem to access to this data from my view in any means. I tried looping and printing out the $query-arrays, even accessing them through global variables but nope. Could someone point me to the right directions here, please? What am I doing wrong, or did I miss something obvious Smile

Sample data clip:
    [0] => Array
            [Title] => Ympäristömerkillä ei vaikutusta tuotehintaan
            [Description] => Ympäristömerkillä ei juuri ole vaikutusta tuotteiden hintoihin. Pohjois-Suomen aluehallintoviraston tekemä selvitys osoittaa, että ympäristöystävällinen valinta on joissakin tapauksissa jopa halvempi kuin vähemmän ympäristöystävällinen.
            [pubDate] => 2010-09-08 14:08:27
            [Link] => http://yle.fi/alueet/oulu/2010/09/ymparistomerkilla_ei_vaikutusta_tuotehintaan_1964381.html?origin=rss
            [Source] => Yle uutiset
            [id] => 1908
            [category] => Oulu
            [channel] => YLE Uutiset | Tuoreimmat uutiset

    [1] => Array
            [Title] => Tutkijoiden työnkuva hukassa rokoteuutisissa
            [Description] => Hyvin harvoin tieteelle annetaan niin paljon palstatilaa kuin viime viikkoina. En usko että suuri yleisö on kuitenkaan päässyt viisastumaan näiden uutisten avulla. Lähinnä kirjoitukset ja keskustelut ovat maalanneet sangen erikoista kuvaa tieteenteki
            [pubDate] => 2010-09-08 14:04:17
            [Link] => http://yle.fi/uutiset/kotimaa/2010/09/tutkijoiden_tyonkuva_hukassa_rokoteuutisissa_1964415.html?origin=rss
            [Source] => Yle uutiset
            [id] => 1909
            [category] => Kotimaa
            [channel] => YLE Uutiset | Tuoreimmat uutiset

Is there any way to print out all the variable data passed to a specific view? This would be very useful?

Any help would be highly appreciated.


// controller
$data['content'] = $this->db_model->get_news();
$this->load->view('news', $data);

// view
foreach ($content as $row):

Yes, as n0xie pointed out, when passing data to view CI assumes it is an array. So you will have to send it as array to your view as stated here above.

Also, you can do this as well:
# Controller
$data['info'] = "something";
$data['error'] = "Oh no, error!";

$this->load->view('news', $data);

# view
echo $info; # shows: something
echo $error; # shows: Oh no, error!


Thanks guys for your fast reply. I knew the solution would be simple. Smile

Another quick question: my views need to use some utility function I have written in a separate library class. I tried to include this file on top of the view, but CI doesnt seem to like this. What would be the proper way use my pre-written classes in my CI code?

Thanks again,

[eluser]Greg Aker[/eluser]
Do you have your class in application/libraries? load it up in your controller with:


then in the view:


or if it's procedural code, you can just use helpers.


Theme © iAndrew 2016 - Forum software by © MyBB