• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Single value return "Array to string conversion?"

#1
[eluser]Sky Warden[/eluser]
Hi everyone. I'm having a small problem with my data. I have this model :

Code:
function get_user($user_id){
    
        $this->db->select('username');
        $this->db->from('users');
        $this->db->where('id', $user_id);
        
        $query = $this->db->get();
        
        #echo $query->num_rows();
        
        return $query->result();
    }

Then I try to get the data like this :

Code:
$uploader = $this->user_model->get_user($user_id);
echo 'by '.$uploader;

That $uploader becomes an array, so I can't echo it directly. My question is, isn't my query result just a single value? The username and user_id is unique. I just select the username by the user_id. In the database, for example, I have user "Sky Warden" with id.1. Therefore, since the id is unique (primary key) shouldn't $uploader just contains "Sky Warden?"

Does every data fetched from query, no matter the value, is formatted as an array? Then how can I use it just to print "Sky Warden" because I don't know the element. I've tried echo $uploader['username'] but it returns undefined index, so I guess it's not the element. So...help me please?

P.S. I've tried the num_rows() and there's only just one matching value.

#2
[eluser]jmadsen[/eluser]
Even though your result() object only has one row, it is still a collection of rows. You might want to look at result_array() just to make it easier to visualize with var_dump()

The result_array() will be an array of arrays, each of those sub arrays being a row(). The function won't change up the return object simply because the requested data changes to something simpler - you have to do that yourself.

However, there are a couple things that can help:

Code:
->row()/->row_array() will grab the first row - so if you know you only have one, just use that

    ->row()->username / ->row_array()['username'] - can get a value directly

Just use them with caution - easy to make logic bugs until you're comfortable

#3
[eluser]Sky Warden[/eluser]
[quote author="jmadsen" date="1363948530"]Even though your result() object only has one row, it is still a collection of rows. You might want to look at result_array() just to make it easier to visualize with var_dump()

The result_array() will be an array of arrays, each of those sub arrays being a row(). The function won't change up the return object simply because the requested data changes to something simpler - you have to do that yourself.

However, there are a couple things that can help:

Code:
->row()/->row_array() will grab the first row - so if you know you only have one, just use that

    ->row()->username / ->row_array()['username'] - can get a value directly

Just use them with caution - easy to make logic bugs until you're comfortable[/quote]

Whoa! Thank you! It's working now. I totally forgot that row() will grab the first row. :lol:

I know it's not relevant to the topic, but do you think it's a good idea to load models from view? Some people say it's fine, while some other don't. In some cases, I need to load it from view instead of loading it in controller then give the data to the view. Is there any disadvantage of this method?

#4
[eluser]jmadsen[/eluser]
Tell the people who think it is fine they are clueless, and hand them a link to an MVC page

Of the bad design mistakes you can make, it is one of the more terrible ones in terms of how horrible it is to try to debug & trace code

#5
[eluser]Sky Warden[/eluser]
[quote author="jmadsen" date="1363949563"]Tell the people who think it is fine they are clueless, and hand them a link to an MVC page

Of the bad design mistakes you can make, it is one of the more terrible ones in terms of how horrible it is to try to debug & trace code[/quote]
I see. Models are supposed to be loaded in controllers. Usually I use views just to show the data. Well, thanks for clearing that up. Smile

#6
[eluser]jmadsen[/eluser]
There are different design patterns, and down the road you might decide you like one of them better. However, one thing I think they all have in common is - we don't load the data directly into the view

In any case, Codeigniter is built around using a controller to strap these together, and when you load models into a view, yo invariably need to add a lot of "presenter" type logic there as well, making a real mess of your code & making it very difficult for others to trace your code & understand it.

The rule of thumb I generally go by is:

If you need to manipulate the data at all (I would include calling it from the data source in this), do it in your controller first & just display

If you are only looking to format data (format your date, capitalize words, etc), you can make a helper function & feel free to call that directly in the view.

That's just my way of doing it, but it works pretty well for me.

#7
[eluser]Sky Warden[/eluser]
[quote author="jmadsen" date="1364004593"]There are different design patterns, and down the road you might decide you like one of them better. However, one thing I think they all have in common is - we don't load the data directly into the view

In any case, Codeigniter is built around using a controller to strap these together, and when you load models into a view, yo invariably need to add a lot of "presenter" type logic there as well, making a real mess of your code & making it very difficult for others to trace your code & understand it.

The rule of thumb I generally go by is:

If you need to manipulate the data at all (I would include calling it from the data source in this), do it in your controller first & just display

If you are only looking to format data (format your date, capitalize words, etc), you can make a helper function & feel free to call that directly in the view.

That's just my way of doing it, but it works pretty well for me. [/quote]
Yes. That's how I do it most of the time. I usually do data manipulation in the controller, with the help of some models if necessary. Then I warp those data in an array which I send to the view as the second parameter. :lol:


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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