views:

91

answers:

2

Hi, I use kohana and when you try to fetch data from database it returns class variables(like $user->firstname) as a database data. User table have a 12 columns and i fetch 8 columns but at this point some of columuns maybe empty(like $user->phone). How can i found empty column number ?(Proper way..)

Thanks A Lot

+2  A: 

Generically, you could try something like:

/**
 * Count number of empty data members in a row object.
 */
function countEmpty($row){
  $fields = array_keys($row->as_array());
  $cnt = 0;
  foreach($fields as $f){
    if (empty($row->$f)) $cnt++;
  }
  return $cnt;
}
timdev
A: 

i found solution. PHP have magic get_object_vars function:

$data = User_Model::factory()->read(
    array('id' => $user_id),
    'firstname, lastname, birthday, country, mobilephone, landphone, address'
);

$filled_data = 0;
foreach(get_object_vars($data) as $v)
{
    if ($v != '') $filled_data++;
}
return round($filled_data / count(get_object_vars($data)) * 100);
mTuran
you can just use count(array_filter($data->as_array()));
gpilotino