CodeIgniter Forums
[Solved] Work out a percentage of a item set in array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [Solved] Work out a percentage of a item set in array (/showthread.php?tid=66422)



[Solved] Work out a percentage of a item set in array - wolfgang1983 - 10-20-2016

On my codeigniter project I have a user profile page

I can not work out the correct percentage there are 11 items in my list but only 10 items are set.

Question: How can I work out the correct percentage for items that are set it should be at 90% or there abouts and if all 11 items are set would be 100%

It's for a progress bar


PHP Code:
$this->data['profile'] = array(
    'username' => $username,
    'email' => $email,
    'description' => '6',
    'firstname' => $firstname,
    'lastname' => $lastname,
    'image' => $image,
    'location' => '5',
    'website' => '4',
    'google_plus' => '3',
    'twitter' => '2',
    'github' => ''
);

$is_set count(array_filter($this->data['profile']));
$percent1 100 $is_set;
$percent2 $percent1 $is_set;

echo 
$percent2;

$this->data['profile_progress'] = $percent2


View


PHP Code:
<script type="text/javascript">
$(
document).ready(function() {
    $(".progress-bar").animate({
        width"<?php echo $profile_progress;?>%"
    }, 2500);

});
</
script



RE: Work out a percentage of a item set in array - salain - 10-21-2016

Hi,

This should do it:


PHP Code:
$cnt_keys count($array); // 11 keys 
$cnt_values count(array_filter($array)); // 10 values 
$percent $cnt_values $cnt_keys 100// 90.9091 %
echo round($percent,2); 

Array_filter will only return non empty values.
If you need a more complex filter you can pass a function to array_filter as second parameter.


RE: Work out a percentage of a item set in array - wolfgang1983 - 10-21-2016

(10-21-2016, 12:18 AM)salain Wrote: Hi,

This should do it:


PHP Code:
$cnt_keys count($array); // 11 keys 
$cnt_values count(array_filter($array)); // 10 values 
$percent $cnt_values $cnt_keys 100// 90.9091 %
echo round($percent,2); 

Array_filter will only return non empty values.
If you need a more complex filter you can pass a function to array_filter as second parameter.


That worked fine thanks


RE: [Solved] Work out a percentage of a item set in array - salain - 10-21-2016

Your welcome.