1

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?

  • You have not initialised the variables ` $row["Feedback1"] to $row["Feedback7"]`. What if `if ( $row["Feedback1"] == '') {` condition fails. It would be definitely not integer. – Pupil May 03 '19 at 04:09
  • Why not use `SELECT Feedback1+Feedback2+... FROM test`, you don't need to check empty entries at all. – shingo May 03 '19 at 04:19
  • 1
    please provide correct code, the code shows 7 feedbacks data but you are giving 1+1+1+1 output – fmsthird May 03 '19 at 04:26
  • 1
    Possible duplicate of [How do I convert a string to a number in PHP?](https://stackoverflow.com/questions/8529656/how-do-i-convert-a-string-to-a-number-in-php) –  May 03 '19 at 04:27
  • No its not, the answers below does suggest (int) but its not working for me. Thus, I posted this question. –  May 03 '19 at 04:29
  • Why downvoted? Provided what I tried, the question and yet get downvoted. Why is this such an unfriendly environment for someone new? –  May 03 '19 at 04:46
  • @simmy can you post what you tried that didn't work for you in an edit? –  May 03 '19 at 05:32
  • The above is what I tried. –  May 03 '19 at 05:55
  • `(int)` isn't thin the code you posted, though. When you tried `(int)`, what did you try? –  May 03 '19 at 19:56

3 Answers3

2

Change your query to get SUM directly:-

$sql5 = "SELECT SUM(Feedback1+Feedback2+Feedback3+Feedback4+Feedback5+Feedback6+Feedback7) as sum FROM test WHERE loc = 'town' ORDER BY UpdatedDate DESC";
$result3 = $conn->query($sql5);
if ($result3->num_rows > 0) {
    while($row = $result3->fetch_assoc()) {
        echo $row['sum'];
        echo PHP_EOL; // new line to distinguish each output
    }
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

You need to typecast them as integer or float, so that PHP won't consider them as a string. DO this way

$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 = (int)$row['Feedback1'] + (int)$row['Feedback2'] + (int)$row['Feedback3'] + (int)$row['Feedback4'] + (int)$row['Feedback5'] + (int)$row['Feedback6'] + (int)$row['Feedback7'];
        $sum = 0;
        $sum = $total_qn + (int)$sum;
        echo $sum;
Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45
0

Use the function intval(): example

intval($row['Feedback1'])

G.Man
  • 11
  • 5