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

[eluser]mjstenbe[/eluser]
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

Code:
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:

Code:
$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:
Code:
Array
(
    [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.

Thanks,
Mika
#2

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

// view
foreach ($content as $row):
  var_dump($row);
endforeach;
#3

[eluser]smilie[/eluser]
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:
Code:
# 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!

Regards,
Smilie
#4

[eluser]mjstenbe[/eluser]
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,
Mika
#5

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

Code:
$this->load->library('your_library');

then in the view:

Code:
$this->your_library_name_here->method_name_here();

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

-greg




Theme © iAndrew 2016 - Forum software by © MyBB