Welcome Guest, Not a member yet? Register   Sign In
Trying to get property of non-object
#1

[eluser]xjermx[/eluser]
I'm doing the following:

In my controller:

Code:
$data['multi'] = $this->smoke_model1->list_users();
      
      $this->load->view('smoke_modusers', $data);


in my view:

Code:
foreach ($multi as $r) :

    <p>&lt;?php echo form_checkbox($r->id); ?&gt; <label for="name">&lt;?php echo $r->name ?&gt; </label></p>
    &lt;?php echo '<div>id:  ' .  $r->id . '</div>';
    echo '<div>name:  ' . $r->name . '</div>';

And this works fine. It spits out a fairly ugly list of users in my database, per my list_users function.

I'm also attempting the following:
Controller:
Code:
$data['multi2'] = array(
array('id'=>'78','name'=>'Blue','ip'=>'5465465465'),
array('id' => '17','name'=>'Ice Cube','ip'=>'255.255.255.0'),
array('id' => '14', 'name' => 'Snoop Doggy Dogg','ip' =>'127.0.0.1'),

View:
Code:
foreach ($multi2 as $r) :

    <p>&lt;?php echo form_checkbox($r->id); ?&gt; <label for="name">&lt;?php echo $r->name ?&gt; </label></p>
    &lt;?php echo '<div>id:  ' .  $r->id . '</div>';
    echo '<div>name:  ' . $r->name . '</div>';


endforeach;

This does NOT work, I get a bunch of "Trying to get property of non-object". I can do print_r's to check out the data in the view, and both of them show up, but the multi2 shows as "Array" and multi shows as "stdClass Object"

Am I simply missing a step, or perhaps just approaching this the wrong way?
#2

[eluser]mddd[/eluser]
It's simple. In multi you are getting a list of objects. To display an object's properties you use $r->id or $r->name etc.
In multi2 you are building a list of arrays. To display each array's items you can't use $r->id or $r->name because that is for objects, not arrays.
If you use $r['id'] instead of $r->id, displaying the array will work just fine.
#3

[eluser]xjermx[/eluser]
Thanks very much!

I'm reverse engineering my way through some code as I teach myself PHP and codeigniter.


I've got a followup question. I've spent a few days banging head against the wall trying to do something that I think is relatively simple.


I have an array which I'll pulled via a function in my model from a mysql database.

Code:
$data = $this->smoke_model1->list_users();
      print_r($data);

gives me:

Quote:Array ( [multi] => Array ( [0] => stdClass Object ( [id] => 1 [name] => jerm1 [ip] => ) [1] => stdClass Object ( [id] => 2 [name] => name [ip] => ) [2] => stdClass Object ( [id] => 3 [name] => 0 [ip] => ) [3] => stdClass Object ( [id] => 4 [name] => 0 [ip] => ) [4] => stdClass Object ( [id] => 5 [name] => asdf56 [ip] => ) [5] => stdClass Object ( [id] => 6 [name] => asdf57 [ip] => ) [6] => stdClass Object ( [id] => 7 [name] => asdf58 [ip] => ) [7] => stdClass Object ( [id] => 8 [name] => asdf59 [ip] => ) [8] => stdClass Object ( [id] => 9 [name] => 0 [ip] => ) [9] => stdClass Object ( [id] => 10 [name] => bsdf2 [ip] => ) [10] => stdClass Object ( [id] => 11 [name] => laksdjie11 [ip] => ) [11] => stdClass Object ( [id] => 12 [name] => bargle2 [ip] => 75.66.223.193 ) [12] => stdClass Object ( [id] => 13 [name] => Booblesnort [ip] => 75.66.223.193 ) ) )


I am trying to fiddle with how that array is setup. Ultimately I'm trying to pass the data to a view, display the data with checkboxes and a submit button, and then based on which are checked, pass them back to the model to be deleted from the mysql table.

I've successfully passed the data to the view, and gotten it to display, but I think that I need to tweak the array - the lack of key names is throwing me off. I've managed to make the following code work for testing:

Code:
$data['multi'] = array(
                    
                     array('name'=> 'xid[]','id'=>'78','name2'=>'Bob Barker','ip'=>'5465465465','value'=>'hi my name is Bob'),
                    array('name'=> 'xid[]','id' => '17','name2'=>'Joe Smith','ip'=>'255.255.255.0','value' => 'Hi my name is Joe'),
                    array('name'=> 'xid[]','id' => '14', 'name2' => 'Sam Turner','ip' =>'127.0.0.1','value'=> "Hi my name is Sam"),
                    );

because I can use foreach ($multi as $var) and make calls to $var and $var['name'] etc. Hence, my assumption is that if I can plug the values from my sql table into an array that has proper key names, then I can use them in the view.

Is there a simpler way to do this?
#4

[eluser]mddd[/eluser]
I have the feeling that working with objects is causing the problems for you. Probably somehere in the list_users() method, there is a call to CI's db function "result()". If you change that to "result_array()" it will return its information as arrays in stead of objects and you'll probably be more comfortable with that style.




Theme © iAndrew 2016 - Forum software by © MyBB