dyer926 dyer926 - 7 months ago 34
SQL Question

foreach loop keeps adding previouse result to the next in each table row

I am trying to create a table that shows the date of a training plan, the plan its self and the members of the group that can attend.
The first two are no problem but its who can attend that is the issue.
The attendees is stored as a string in the database and explode it to an array but when i use a foreach loop to display the arrays it adds the previous row to the next.

The code I have is:

$sql_group_name = mysqli_query($db_conx,"SELECT groupName,accepted FROM maingroup");
while ($row = mysqli_fetch_array($sql_group_name)){
$accepted= $row["accepted"];
$groupName = $row["groupName"];
$accept_array = explode(",", $accepted);
if (in_array($u,$accept_array)){
$sql = "SELECT * FROM training WHERE groupName='$groupName' ORDER BY sessDate ASC";
$training_query = mysqli_query($db_conx, $sql);

while ($row = mysqli_fetch_array($training_query, MYSQLI_ASSOC)) {
$id = $row["id"];
$date = $row["sessDate"];
$session = htmlspecialchars_decode(stripslashes($row['plan']), ENT_QUOTES);
$groupName = $row["groupName"];
$attending = $row["attending"];
$attend_array = explode(",",$attending);
foreach($attend_array as $value){
$attendList .= "" .$value. "<br/>";
}

if (in_array($u,$attend_array)){
$attendBtn = '<form name="sessUnAttend" id="sessUnAttend" method="post">
<input type="submit" value="Unattend">
<input type="hidden" name="id" id="id" value="' .$id. '">
<input type="hidden" name="unattend" id="unattend">
</form>';

} else { $attendBtn = '<form name="sessAttend" id="sessAttend" method="post">
<input type="submit" value="Attend">
<input type="hidden" name="id" id="id" value="' .$id. '">
<input type="hidden" name="attend" id="attend">
</form>';

}

$sessTable .= '<tr>
<td>' .$date. '</td>
<td>' .$session. '</td>
<td>' .$groupName. '</td>
<td>' .$attendList. '</td>
<td>' .$attendBtn. '</td>
</tr>';
}
}
}


the result I get is:

Date | Plan | Attending

4.4.16 | some plan | me

5.4.16 | some plan | me, me

6.4.16 | some plan | me, me

Obviuosly "me" can only attend once. It would keep repeating me,me until some one else said they could attend and then add their name and repeat that until some one else said the could attend and so on.

I have read that the while loop should be
mysqli_fetch_assoc
instead of
mysqli_fetch_array
but that seems to mess every thing else up.

Answer

The comment from @MarkBaker did the trick with some more help from a friend.

All i had to do was reset the $attendList variable.

     $sessTable .= '<tr>
                    <td>' .$date. '</td>
                    <td>' .$session. '</td>     
                    <td>' .$groupName. '</td>
                    <td>' .$attendList. '</td>
                    <td>' .$attendBtn. '</td>
                    </tr>';
                    $attendList = ''; 

What was happening was the variable was being set from the previous loop and then it just stayed as that until more got added to it.

What happens now is each time the loop finishes it resets the variable so the new id starts from scratch.

Comments