2

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']));
    }
}
7
  • 2
    well you can just use foreach($data['Description'] as $desc) {} right out in the first loop Commented Feb 5, 2016 at 3:34
  • by the way, there is an ->update_batch() method in CI, might as well create a group of array in row batches then use that method, much better Commented Feb 5, 2016 at 3:49
  • 1
    Markdown cleanup on the code blocks. Commented Feb 5, 2016 at 3:59
  • @Ghost, if i do that i can get the values of Description alone.. Commented Feb 5, 2016 at 4:18
  • echo $show; check with var_dump Commented Feb 5, 2016 at 5:46

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

yes i had an issue (Notice : Array to String conversion) for the echo statemet
Can i use php's array_key_exists() to check an array entry while save?
yes you can try using array_key_exists() but why would you use it when you used foreach, so you know all the keys will be there ?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.