Foreach & Arrays in PHP

I'm terribly confused about how I write this function ... it basically clogs the test, and it WORKS for a single user value.

Let's say I need values ​​for the following user: $id = 2... it works! If $id = array(2,3,4,5)not working!

function get_score_a($id){
    // Case 4
    foreach($this->get_results_a($id,4)->row() as $key=>$a){
        if ($a >= 2 && $a <= 4) {
            $score_a += 2;
        } else if ($a > 4 && $a < 8) {
            $score_a += 3;
        } else if ($a > 8) {
            $score_a += floor($a - 8) * .5;
            $score_a += 3;
        }
    };

    return $score_a;
}


function get_results_a($id, $method) {
        $select_cols = array(
                            1 => array('a_1','a_2','a_4'),
                            2 => array('a_6','a_8','a_11','a_12','a_14'),
                            3 => array('a_3','a_10'),
                            4 => array('a_5','a_7','a_9','a_13')
                            );
        return $this->db->select($select_cols[$method])
                        ->where_in('id', $id)
                        ->get('be_survey');
    }

This returns a score ... however, if I run multiple identifiers ... it just adds all the numbers, I think ...

Instead, I need this to output individual points for individual users ...

Can you notice that I'm a complete noob! Very cheerful explanations are greatly appreciated. :)

Edit Please review my code ... how I should have been more clear! Sorry I apologize! In summation, it selects the correct values ​​from the table based on

, ! Edit ! Codeigniter!

+3
3

-, row(). foreach ($array as $key=>$value)
-, foreach $score_b . , .
:

foreach(array(5,5,5,6,7,78,8,7,7,6,5) as $key=>$a){
         if ($a >= 0 && $a <= .5) {
             $score_b[$key] += 0;
         } else if ($a > .5 && $a < 2) {
             $score_b[$key] += 1;
         }
     else if ($a > 2 && $a < 4) {
             $score_b[$key] += 2;
         }
           else if ($a > 4) {
          $score_b[$key] += floor($a - 8) * .5;
             $score_b[$key] += 2;
         }
     };

$score_b .

EDIT: :

$id = array(2,3,4,5);
function get_score_array($ids) {
    foreach ($ids as $id) {
        $scores[$id] = get_score_a($id);
    }
    return $scores;
}

$scores $id = > $score.
, (, , )

+4

, row() array. , array , .

, , ->row().

-, ? , . foreach $score_b .

+2

Set the variable $userIDoutside the foreach loop as follows:

$userScores = array(
    'bobby' = > array(5,5,5,6,7,78,8,7,7,6,5),
    'sue' = > array(5,5,5,6,7,78,8,7,7,6,5),
    'joe' = > array(5,5,5,6,7,78,8,7,7,6,5)
);

foreach($userScores as $name => $a){
    $score_b[$name] = 0; //initialize
    if ($a >= 0 && $a <= .5) {
         $score_b[$name] += 0;
    } else if ($a > .5 && $a < 2) {
    $score_b[$name] += 1;
     }
 else if ($a > 2 && $a < 4) {
         $score_b[$name] += 2;
     }
       else if ($a > 4) {
      $score_b[$name] += floor($a - 8) * .5;
         $score_b[$name] += 2;
     }
 };

your end result should be something like (I didn't do any real math)

$score_b['bobby'][100]
$score_b['sue'][75]
$score_b['joe'[90]
0
source

Source: https://habr.com/ru/post/1783502/


All Articles