1

Tried finding a solution on SO and I found multiple questions that are similar but none of them were limited to a number of keys.

I am building a questionaire that has an algorithm based on which it recommends you three winners out of 7 based on the points that they got on each question answer. The table has 10 columns: id, question_no, answer and 7 that are holding the point values.

Here is the code:

<?php
include_once "connect.php";

$question = array();
$question[1] = mysqli_real_escape_string($con, $_POST['question_01']);
// the rest of the questions go here
$question[10] = mysqli_real_escape_string($con, $_POST['question_10']);

    $i = 0;
    $array_sum=[];
     while ($i < 10){
      $i++;     
       $sql =  "SELECT * FROM partners WHERE question_no = $i AND answer = '".$question[$i]."'";
       $result = mysqli_query($con, $sql);

       $final_array_1 = array();

      while ($row = mysqli_fetch_array($result, MYSQLI_NUM))
      {

        $final_array_1 = $row;
            $array_sum = array_map(function () {
                return array_sum(func_get_args());
            }, $array_sum, $final_array_1);
       print_r($final_array_1);
       echo "<br/>";
      }
}

The output of this would be something like:

Array ( [0] => 1 [1] => 1 [2] => A [3] => 5 [4] => 5 [5] => 5 [6] => 5 [7] => 5 [8] => 5 [9] => 5 )

Array ( [0] => 6 [1] => 2 [2] => B [3] => 0 [4] => 0 [5] => 0 [6] => 8 [7] => 8 [8] => 0 [9] => 0 )

And so on for the other 8 arrays.

Basically now I need to add up the values of all the keys from 3 to 9 but since I got these values in a "while" I'm not really sure how to achieve this.

I also need to keep those individual arrays that I get so I can store the options that the user selected in the form in the database.

Let me know if you need any additional details.

4
  • loop through the array from 3 to 9 and increment another variable with the value of each key? Commented Feb 13, 2017 at 13:41
  • @ADyson Yes. That's the idea. Commented Feb 13, 2017 at 13:43
  • so...you know how to make a for loop? After that it's trivial. Commented Feb 13, 2017 at 13:46
  • I know the theoretical side, haven't used them that much. Commented Feb 13, 2017 at 13:47

1 Answer 1

1

You should just be able to loop through your arrays from indexes 3 to 9 and sum the values of each index.

<?php
include_once "connect.php";
$question = array();
$question[1] = mysqli_real_escape_string($con, $_POST['question_01']);
// the rest of the questions go here
$question[10] = mysqli_real_escape_string($con, $_POST['question_10']);
$i = 0;

$sums = array(); //this will hold the sums of all the various indexes
while ($i < 10)
{
  $i++;     
  $sql =  "SELECT * FROM partners WHERE question_no = $i AND answer = '".$question[$i]."'";
  $result = mysqli_query($con, $sql);
  while ($row = mysqli_fetch_array($result, MYSQLI_NUM))
  {
    for ($count = 3; $count <= 9; $count++) {
      if (!isset($sums[$count]) $sums[$count] = 0;
      $sums[$count] += $row[$count];
    }
  }
}
var_dump($sums); //just for debugging, to show you the totals
?>
Sign up to request clarification or add additional context in comments.

8 Comments

And I add this in the final "while"?
Assuming you want to sum each of the rows in your database, yes
Ah, I think I didn't explained correctly. You're adding the values in the same array. What I meant was adding the values from the next iteration over the original ones. Like if I have an array that has the values (1 , 3) and on the next iteration it's (5 , 2) the sum should be (6, 5) not 4.
ok I see. I've updated the question with what I think should work. You'll get an array where index 3 contains the sum of all the index 3s in your database. Same for index 4, etc, up to 9.
Doesn't seem to work as intended. Basically what I need to do is add the "for" part in the while right?
|

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.