Jump to content

how do i..? arrays and checkboxes (some codeigniter)


xjermx

Recommended Posts

Hi,  total newbie here.  Working on teaching myself PHP and codeigniter, largely through a cargo cult style of learning.

 

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.

 

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

 

gives me:

 

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:

 

                $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?

Link to comment
Share on other sites

Your array appears to actually be an array of objects, which are stored in your main array under the key 'multi'. To iterate through them you would want something along the lines of...

 

foreach( $data['multi'] as $v ) {
   echo $v->id;
   echo $v->name;
   echo $v->ip;
   // etc
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.