Welcome Guest, Not a member yet? Register   Sign In
Displaying Array Data
#1

[eluser]NateL[/eluser]
I have some simple information coming out of my database, and displaying in an array called "$result"

Code:
<pre>&lt;?=print_r($result)?&gt;</pre>

returns

Code:
&lt;body&gt;
Array
(
    [0] => stdClass Object
        (
            [id] => 4
            [description] => This is our description!

        )

)
1
&lt;/body&gt;

I know I can use a foreach statement to display multiple database results, but in this instance, I just need to access and display the Description.

Seems fairly elementary to me, but I keep hitting an error Sad
#2

[eluser]davidbehler[/eluser]
What about something like this?
Code:
echo $result[0]->description;
#3

[eluser]NateL[/eluser]
That works - but I feel like "$result[0]" is not very descriptive and could lead to confusion later on down the road.

I will go back and see if there's a way I can clean it up a bit, or if anyone has any other suggestions?

My model looks like this:

Code:
function editAnimal()
    {
        $this->db->from('available');
        $this->db->where('id', $this->uri->segment(3));
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result();
    }

So, when someone clicks "Edit", it brings up all the fields for this animal that they're editing....a foreach loop is not necessary for 1 item, right?

I think this is a bit easier to read

Code:
Description: &lt;?=$row->description?&gt;
#4

[eluser]louis w[/eluser]
You can access the first item like this:

$row = $query->row(1);
then $row->description

Also, FYI this is not an array, it's an object. CI returns db results as an object. You need to convert it to an array if you want a true array.
#5

[eluser]NateL[/eluser]
Thanks Louis Smile
#6

[eluser]louis w[/eluser]
NP

Check out this page, it outlines the different ways that you can retrieve database results. You can also do things like $query->first_row();

http://ellislab.com/codeigniter/user-gui...sults.html
#7

[eluser]NateL[/eluser]
Awesome Thanks!

So I read over that link and it looks like row() is what I need to use.

Inside my model:
Code:
function editAnimal()
    {
        $this->db->from('available');
        $this->db->where('id', $this->uri->segment(3));
        $this->db->limit(1);
        $query = $this->db->get();
        if ($query->num_rows() > 0)
        {
            $row = $query->row();
            return $row;
        }
    }

Inside my controller:
Code:
function edit(){
        $data['row'] = $this->animal_model->editAnimal();
        $data['page_title'] = "Edit Animal";
        
        
        $this->load->view('animal_edit_view', $data);
    }

and inside my view:
Code:
&lt;?=$row->description?&gt;

Works great!!

I'm starting to get the whole MVC architecture and I am really enjoying learning this technique.
#8

[eluser]louis w[/eluser]
Looks good bro. Trust me you will really like MVC once you get it worked in to your everyday development architecture. It really helps you to create a flexible application.

You may even want to rename $data['row'] to something like data or animal. So that in your view it's a little more obvious what that data set is. If in the future you want to do a second model call to look up an animal's region or something 'row' would make it hard to know which db row it is.
#9

[eluser]NateL[/eluser]
Thanks again Louis. I just changed it to $data['animal'] = $this->animal_model->editAnimal(); and it works like a charm and looks much better Smile

&lt;?=$animal->description?&gt;

super simple to understand




Theme © iAndrew 2016 - Forum software by © MyBB