Proper way for count empty variables
Question
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
Solution
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;
}
OTHER TIPS
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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow