CodeIgniter Forums
cleared: one FOREACH works - other one not. Do you see why? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: cleared: one FOREACH works - other one not. Do you see why? (/showthread.php?tid=27516)



cleared: one FOREACH works - other one not. Do you see why? - El Forum - 02-13-2010

[eluser]Jan_1[/eluser]
Trying to display how many fields in userprofil and in groupprofil are filled.
First one works. Second doesn't. Error-Message is "array_values(): The argument should be an array" and "Invalid argument supplied for foreach()"

Could you please have a look? Thank you!!!

This one works:
Code:
$query = $this->db->query('SELECT * FROM user_profile');
      $number_allpfields = $query->num_fields();

      $this->db->where('id', $user_id);
      $query = $this->db->get('user_profile');
      if ($query->num_rows() > 0) { $row = $query->row_array();}
      $i = 0; FOREACH (array_values($row) as $item) {$i++;} $i--;
      $number_fullpfields = $i;
      $profilstatus = $number_fullpfields / $number_allpfields;
      echo $profilstatus;                                        // ok!

This one does not work:
Code:
$query = $this->db->get('group_profil');
      $number_allfields = $query->num_fields();
      echo $number_allfields."<br>";                             //ok!
      
      $this->db->where('id', $group_id);
      $query = $this->db->get('group_profil');
      if ($query->num_rows() > 0) { $row = $query->row_array(); }
      print_r($row);                                              // ok!

      $i = 0;
      FOREACH (array_values($row) as $item) {$i++;}   //<-- error !//
      $i--;
      $number_fullfields = $i;
      $status = $number_fullfields / $number_allfields;
      echo $status;



cleared: one FOREACH works - other one not. Do you see why? - El Forum - 02-13-2010

[eluser]Jan_1[/eluser]
Could not work if it does not get a valid id. :red: