Deepak Keynes
Deepak Keynes

Reputation: 2329

Retrieving data from Multi dimensional Array in Codeigniter

I can't quite get my data from an array, it just shows a blank list of list items when I run my foreach loop.

When I print my array it outputs the following:

Array (
 [Description] => Array (
                [0] => sidewindo 
                [1] => sidewindo 
                [2] => sidewindo 
                [3] => testing ) 
 [comments] => Array   (
               [0] => sidewindo 
               [1] => sidewindo 
               [2] => sidewindo 
               [3] => testing ) 
 [Fld_id] => 625 
 [fld_IsPublic] => Array (
              [0] => 1 ) 
 [action] => s 
 [pk_id] => 7 )

My code so far

public function save_data($data)
{
  foreach ($data as $row)
  {
    foreach ($row['Description'] as $show)
      {
         echo $show;
      }  
  }

And i have tried the following kind of for loop also to retrieve data from the array. I am trying to retrieve data from the array and update it in another table in the database. This code I have written in my model before passing it to the view.

for($i=0;$i<count($comments);$i++)
   { 
     $this->db->update->update('tblname',array('TK_id'=>$data['pk_id'],'Fld_id'=>$data['Fld_id'],'fld_description'=>$data['Description'],'fld_comments'=>$data['comments'],'fld_IsPublic'=>$data['fld_IsPublic']),array('TK_id'=>$data['pk_id'],'Fld_id'=>$data['Fld_id']));
    }
}

Upvotes: 2

Views: 918

Answers (2)

Praveen Kumar
Praveen Kumar

Reputation: 2408

Change Your code from

public function save_data($data)
{
  foreach ($data as $row)
  {
    foreach ($row['Description'] as $show)
      {
         echo $show;
      }  
  }

to

 public function save_data($data)
 {    
    foreach ($data["comments"] as $key=>$row)
    {
        $data_update = array(   
                                'TK_id'             => $data['pk_id'],                                                                 
                                'Fld_id'            => $data['Fld_id'],
                                'fld_description'   => $data['Description'][$key],                      
                                'fld_comments'      => $row,
                                'fld_IsPublic'      => $data['fld_IsPublic'][0]
                            );
        $this->db->update('tblname',$data_update,array('TK_id'=>$data['pk_id'],'Fld_id'=>$data['Fld_id']));
            //but this will update single row again and again
            //my suggetion is use $this->db->insert('tblname',$data_update); instead 
    }
 }

ANd you will be just fine

Upvotes: 0

David
David

Reputation: 1145

First of all you should know that echo an array will produce a Notice ( Array to string conversion ), so depending in what environment you are working on you will prefer to use print_r.

Answering your question, before you echo, you should check if it's an array or not

public function save_data($data){
    $update = array(); 
    foreach ($data as $key=>$row){

        if(is_array($row)){
            foreach($row as $key2=>$row2){
                $update[$key2] = $row2;
                //echo $row2 . "<br>"; 
            }

        }else{
           $update[$key] = $row;             
           //echo $row ."<br>";
        }
     }  
  }

And now you can use $update to update your table , include this on inside save_data() function, and if you needed, include save_data() inside a foreach. Remember that is recommended to use a where when updating data so:

//$this->db->where('id', $id); 
$this->db->update('tblname', $update); 

hope that helps

Upvotes: 3

Related Questions