SubZero
SubZero

Reputation: 87

Data in nested foreach loops used in update query

This is my code:

session_start();
/* loops through each row in the global $_SESSION variable which
contains the array and uses the $value to GET the data in the text
boxes and output them */

// studevent_result = 
foreach ($_SESSION['arrayNameResult'] as $value) {
    $studResult = $_GET[$value];
    echo $studResult;
    echo "<br>";
}

// result_postion = 
foreach ($_SESSION['arrayNamePosition'] as $value) {
    $studPosition = $_GET[$value];
    echo $studPosition;
    echo "<br>";
}

echo "<br>";

// stud_id = 
foreach ($_SESSION['arrayId'] as $value) {
    echo $value;
    echo "<br>";
}

// UPDATE query, this will update the studevent_result and result_position
// column in the database for the specific stud_id.
$updateQuery = "
    UPDATE result
    SET studevent_result = '00:20:33',
        result_position = '6'
    WHERE result.stud_id = '12'
";

$updateRow = mysqli_query($conn, $updateQuery);

I use $_SESSION variables which all store an array. I extract the results of these arrays using foreach loops.

In $updateQuery, I want to make studevent_result = to the results of my first foreach loop above, result_position = to the results of the second foreach loop above and the result.stud_id = to the results of the third foreach loop above.

After me editing the code my code now looks like this:

foreach ($_SESSION['arrayNameResult'] as $value) {
$studResult = $_GET[$value];
        foreach ($_SESSION['arrayNamePosition'] as $data) {
            $studPosition = $_GET[$data];
    foreach ($_SESSION['arrayId'] as $idValue) {
echo $idValue;
$updateQuery = "
    UPDATE result
    SET studevent_result = '$studResult',
        result_position = '$studPosition'
    WHERE result.stud_id = '$idValue'
";
$updateRow = mysqli_query($conn, $updateQuery);
            }
        }
    }

I nested the foreach loops. But the problem now is that for the last foreach loop in the nested loops, $idValue in the query only uses the last element in the array $_SESSION['arrayId']. How can I fix this to loop throught the whole array, so that the query uses all the values in the array?

Thanks in advance.

Upvotes: 0

Views: 386

Answers (1)

Sahith Vibudhi
Sahith Vibudhi

Reputation: 5205

If I understood your issue this should help you

session_start();
$i = 0;
$studResult = array(); 
foreach ($_SESSION['arrayNameResult'] as $value) {
$studResult[$i] = $_GET[$value]; 
$i++;
}

$studPosition= array();
$i=0;
foreach ($_SESSION['arrayNamePosition'] as $value) {
$studPosition[$i] = $_GET[$value];
$i++;
}


$stud_id = array(); $i=0; 
 foreach ($_SESSION['arrayId'] as $value) {
$stud_id[$i] = $value; $i++;
}

for($j =0; $j<$i; $j++){
  $updateQuery = "
  UPDATE result
  SET studevent_result = '$studResult[$j]',
    result_position = '$studPosition[$j]'
  WHERE result.stud_id = '$stud_id[$j]'
  ";

  $updateRow = mysqli_query($conn, $updateQuery);
}

Hope it will be helpful. Happy coding :)

Upvotes: 1

Related Questions