1

I want to create leveling system with Codeigniter. I retrieve all criteria from the table, then User fill the quistionare and finally system make a decission, level 1, level 2, or level 3.

Here is my controller:

if ($this->input->post('submit_asesmen')) {
        $hitung_asesmen=count($_POST['id_komponen']);
        $level='';
        for ($i=0; $i < $hitung_asesmen; $i++) { 

            $id_asesmen=$_POST['id_komponen'][$i];
            $yes=$_POST['yesorno'][$i];
            $bukti=$_POST['cek'][$i];
            $lihat_level=$this->m_asesmen->select_level($id_asesmen);

            $data_asesmen = array(                  
                'id_komponen_asesmen' => $id_asesmen, 
                'yes_or_no' => $yes, 
                'level' => $lihat_level, 
                );

            $hitung_level_1=$this->m_asesmen->jml_level_1();
            $macam_level_1=$this->m_asesmen->macam_level_1();

            if ($lihat_level=='1' && $yes=='1') {
                echo "Level 1 <br>";
            }
            else{
                echo "Level 0 <br>";
            }
        }
    }

My temporary output is like

Level 1
Level 0
Level 0
Level 0
Level 1
Level 1
Level 1
Level 1
Level 0
Level 0
Level 0

How to create output like : ?

Level 0 : 6 values
Level 1 : 5 values

3 Answers 3

1

This can be achieved with PHP's function array_count_values(). Just pass your array to this function and it will return an associative array containing the value/count pairs.

Example:

$tempOutput = array('Level 1', 'Level 0', 'Level 0', 'Level 0', 'Level 1', 'Level 1', 'Level 1', 'Level 1', 'Level 0', 'Level 0', 'Level 0');

// will return array('Level 1' => 5, 'Level 0' => 6);
$newOutput = array_count_values($tempOutput);

Since in your code you do not save the values in an array, it would be a better and easier solution to manually count the values:

// array holding the counts for each level
$levels = [];
if ($this->input->post('submit_asesmen')) {
    $hitung_asesmen=count($_POST['id_komponen']);
    $level='';
    for ($i=0; $i < $hitung_asesmen; $i++) { 

        $id_asesmen=$_POST['id_komponen'][$i];
        $yes=$_POST['yesorno'][$i];
        $bukti=$_POST['cek'][$i];
        $lihat_level=$this->m_asesmen->select_level($id_asesmen);

        $data_asesmen = array(                  
            'id_komponen_asesmen' => $id_asesmen, 
            'yes_or_no' => $yes, 
            'level' => $lihat_level, 
            );

        $hitung_level_1=$this->m_asesmen->jml_level_1();
        $macam_level_1=$this->m_asesmen->macam_level_1();

        if ($lihat_level=='1' && $yes=='1') {
            echo "Level 1 <br>";
        }
        else{
            echo "Level 0 <br>";
        }

        // check if a count exists in $levels
        if(!isset($levels[$lihat_level])) {
            // count does not exist, initialize with 1
            $levels[$lihat_level] = 1;
        } else {
            // count already exists, increase by 1
            $levels[$lihat_level] ++;
        }
    }
}

// And this will create the desired output like you wrote it in your question
foreach($levels as $level => $count) {
    echo 'Level ' . $level . ' : ' . $count . ' values<br />';
}
Sign up to request clarification or add additional context in comments.

2 Comments

could you like to write some example based from my case?
Okay, thanks. Its work for me, but I change if(!isset($levels[$yes])) not if(!isset($levels[$lihat_level])) because the key of my level is if yes==1. Thanks
0

Nice Example but it doesn't explain every well what he really wants Try out this

Example

$arr = array("Level 1","Level 2","Level 3", "Level 4");
$newData = array_count_values($arr);
foreach($arr as $data){
echo $data.":  ".$newData[$data]." value<br>";
}

Result

Level 1: 1 value
Level 2: 1 value
Level 3: 1 value
Level 4: 1 value

Comments

0
if ($this->input->post('submit_asesmen')) {
            $hitung_asesmen=count($_POST['id_komponen']);
            $level='';
            // Define Level Array
            $levels_ary=array();
            for ($i=0; $i < $hitung_asesmen; $i++) { 

                $id_asesmen=$_POST['id_komponen'][$i];
                $yes=$_POST['yesorno'][$i];
                $bukti=$_POST['cek'][$i];
                $lihat_level=$this->m_asesmen->select_level($id_asesmen);

                $data_asesmen = array(                  
                    'id_komponen_asesmen' => $id_asesmen, 
                    'yes_or_no' => $yes, 
                    'level' => $lihat_level, 
                    );

                $hitung_level_1=$this->m_asesmen->jml_level_1();
                $macam_level_1=$this->m_asesmen->macam_level_1();


                if ($lihat_level=='1' && $yes=='1') {
                    $levels_ary[]="Level 1";
                }
                else{
                    $levels_ary[]="Level 0";
                }
            }

            print_r(array_count_values($levels_ary));
        }

2 Comments

hi Satish. Try posting an explanation of your code as it increases the chances of future audience to grab a firm grasp of the concepts.
I cannot see the output, I try to change input but the result cannot change too

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.