简体   繁体   中英

How can I get the total score percentage of Three Categories in my array?

I have this response

[0] => Array (
    [category_id] => 2
    [name] => Listening
    [question_id] => 10
    [question_choice_id] => 38
    [choice_level] => 0
    )
[1] => Array (
    [category_id] => 2
    [name] => Listening
    [question_id] => 11
    [question_choice_id] => 44
    [choice_level] => 0
    )
[2] => Array (
    [category_id] => 2
    [name] => Listening
    [question_id] => 12
    [question_choice_id] => 45
    [choice_level] => 0
    )
[3] => Array (
    [category_id] => 2
    [name] => Listening
    [question_id] => 13
    [question_choice_id] => 50
    [choice_level] => 0
    )
[4] => Array (
    [category_id] => 2
    [name] => Listening
    [question_id] => 14
    [question_choice_id] => 55
    [choice_level] => 0
    )
[5] => Array (
    [category_id] => 2
    [name] => Listening
    [question_id] => 15
    [question_choice_id] => 58
    [choice_level] => 1
    )
[6] => Array (
    [category_id] => 1
    [name] => Reading
    [question_id] => 1
    [question_choice_id] => 2
    [choice_level] => 0
    )
[7] => Array (
    [category_id] => 1
    [name] => Reading
    [question_id] => 2
    [question_choice_id] => 8
    [choice_level] => 0
    )
[8] => Array (
    [category_id] => 1
    [name] => Reading
    [question_id] => 3
    [question_choice_id] => 9
    [choice_level] => 0
    )
[9] => Array (
    [category_id] => 1
    [name] => Reading
    [question_id] => 4
    [question_choice_id] => 13
    [choice_level] => 1
    )
[10] => Array (
    [category_id] => 1
    [name] => Reading
    [question_id] => 5
    [question_choice_id] => 19
    [choice_level] => 0
    )
[11] => Array (
    [category_id] => 1
    [name] => Reading
    [question_id] => 6
    [question_choice_id] => 22
    [choice_level] => 1
    )
[12] => Array (
    [category_id] => 1
    [name] => Reading
    [question_id] => 7
    [question_choice_id] => 25
    [choice_level] => 1
    )
[13] => Array (
    [category_id] => 1
    [name] => Reading
    [question_id] => 8
    [question_choice_id] => 30
    [choice_level] => 0
    )
[14] => Array (
    [category_id] => 1
    [name] => Reading
    [question_id] => 9
    [question_choice_id] => 36
    [choice_level] => 0
    )
[15] => Array (
    [category_id] => 3
    [name] => Grammar
    [question_id] => 31
    [question_choice_id] => 122
    [choice_level] => 0
    )
[16] => Array (
    [category_id] => 3
    [name] => Grammar
    [question_id] => 32
    [question_choice_id] => 125
    [choice_level] => 0
    )

so here I have 3 categories which is the Listening,Reading,Grammar getting the total percentage of user using one category is easy, but I don't know how can I get and store this 3 categories in my array to show it to my view.

I want to get the percentage of the user with the formula of

Score = the_right_answer / total_count_array * 100

The correct answer has a value of 1 in the choice_level columns

so for my example is, the formula should be

Reading

TOTAL  = 2 / 9 * 100 
Listening

TOTAL  = 1 / 5 * 100 
Grammar

TOTAL  = 0 / 3 * 100 

How can I get the total from this array?

Once I get the answer I just like to return them to my view.

I also have a session which is the category_id, it is an array, I am thinking i can use this in getting the result from each category.

$category_id = [1,2,3];
  public function more_details(){

     $category_id = Session::get('category_id'); //[1,2,3];

    //more_details score of the user.
    $score = Answer::get_user_more_details();
    
    return view('pages.user.user_more_details',  [
     'title' => '',
    ]);
  
  }

This is my database query, Can I just get the result here? instead of coding it on my backend?

SELECT answer.category_id,category.name,answer.question_id,answer.question_choice_id,choice.choice_level
FROM answers AS answer
LEFT JOIN  categories AS category ON answer.category_id = category.id
LEFT JOIN question_choices AS choice ON answer.question_choice_id = choice.id
WHERE answer.user_set_id = 1 and answer.user_id = 3
Category_table

id(PK) |   name 
1                   Reading
2                   Listening
3                   Grammar
Answer_table
id(PK)  | category_id | question_id | question_choice_id
1             2            10            38
2             2            11            44
3             2            12            45
4             2            13            50
5             2            14            55
6             2            15            58
7             1            1             2
8             1            2             8
9             1            3             9
10            1            4             13
11            1            5             19
12            1            6             22
13            1            7             25
14            1            8             30
15            1            9             36
16            3            31            122
17            3            32            125
Question_choice_table

id(PK) | choice_level
38          0
44          0
45          0
50          0
55          0
58          1
2           0
8           0
9           0
13          1
19          0
22          1
25          1
30          0
36          0
122         0
125         0

Can anyone help me on how can I do this? I really don't know what to do. Any help would be really appreciated.

One of the method would be to use the groupBy method of Collections.

$original_array = collect([]); // your array

$expected_array = [];

$grouped_array = $original_array->groupBy('name')->map(function ($item, $key) 
use (&$expected_array) {

$expected_array[$key]['occurence'] = $item->where('choice_level', 1)->count();
$expected_array[$key]['total'] = $item->count();
$expected_array[$key]['percentage'] = ($expected_array[$key]['occurence'] / $expected_array[$key]['total']) * 100;

});


return view('your-view', compact('expected_array'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM