I want to sum the numbers in my loop. However, I noticed that the loop is displaying the numbers as a string and not an int. Why I feel so is because when say the loop outputs 1,1,1,1, instead of adding them to make it 4, it appears as 1111.
This is what I tried and my code:
$sql5 = "SELECT * FROM test
WHERE loc = 'town'
ORDER BY UpdatedDate DESC";
$result3 = $conn->query($sql5);
if ($result3->num_rows > 0) {
// output data of each row
while($row = $result3->fetch_assoc()) {
if($row["Feedback1"]=="") {
$row["Feedback1"] = 0;
}
if($row["Feedback2"]=="") {
$row["Feedback2"] = 0;
}
if($row["Feedback3"]=="") {
$row["Feedback3"] = 0;
}
if($row["Feedback4"]=="") {
$row["Feedback4"] = 0;
}
if($row["Feedback5"]=="") {
$row["Feedback5"] = 0;
}
if($row["Feedback6"]=="") {
$row["Feedback6"] = 0;
}
if($row["Feedback7"]=="") {
$row["Feedback7"] = 0;
}
$total_qn = $row['Feedback1'] + $row['Feedback2'] + $row['Feedback3'] + $row['Feedback4'] + $row['Feedback5'] + $row['Feedback6'] + $row['Feedback7'];
$sum = 0;
$sum = $total_qn + $sum;
echo $sum;
So this is my thought process behind this code:
I summed the values of a few column and assigned it to a variable.
This variable is then looped and added onto its value so I can get the full sum.
However, as mentioned instead of doing 1 + 1 + 1 + 1 = 4, it is outputting 1111.
May I know what I am doing wrong, logically and coding wise where am I going wrong?