0

I'm trying to add values from row['scores'].

For example, if I have 6 rows that have a value of 1 for each row .. I want to be able to echo -> value of rows = 6.

The += is not working for me: I still get only the values themselves, e.g. 1,2,3,4,5,6,7 but I want the sum of it, let's say 1+2+3+4+5+6+7=28.

Thanks

<?php include("connect.php"); ?>

<html>
    <head>
        <title>Score Predictions</title>
    </head>
    <body>

        <div id = "id">

<?php
    $query = "SELECT * FROM test";
    $result = mysql_query($query);

    while ($row = mysql_fetch_array($result)) {
        $id = $row['id'];
        $home = $row['home'];
        $away = $row['away'];
    }
?>

<?php
    if (isset($_POST['submit'])) {
        $x = $_POST["test"];
        mysql_query("INSERT INTO test (home, away, score) VALUES ('$home', '$away', '$x')");
    }
?>

<?php echo $home," - ",$away; ?>

    <form method = 'post' action = 'http://albsocial.us/test/index.php'>
        <select name = 'test'> 
            <option value = "" selected = 'selected'></option>
            <option VALUE = '1'>1</option>
            <option VALUE = 'X'>X</option>
            <option VALUE = '2'>2</option>   
        </select>
        <INPUT TYPE = 'submit' name = 'submit' />
    </form>

<?php
    $query = "SELECT * FROM test";
    $result = mysql_query($query);

    while($row = mysql_fetch_array($result)) {
        $id = $row['id'];
        $score = $row['score'];
        if ($score == "1") {
            echo $sum += $score - 1;
        }
    }
?>

        </div>
    </body>
</html>
6
  • change this line: echo $sum += $score-1 ; Commented Jun 13, 2013 at 4:13
  • >Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Commented Jun 13, 2013 at 4:34
  • you are overwriting the previous result $id = $row['id']; $home = $row['home']; $away = $row['away']; and if you want to use only one result then why not use condition in query ? and your code is vulnerable to sql injection you need to escape all request Commented Jun 13, 2013 at 4:38
  • this if ($score == "1"){ echo $sum += $score-1 ; } wont do anything it will just make $sum always 0 Commented Jun 13, 2013 at 4:43
  • you are writing terrible code i am afraid but you need to rewrite code from scrap again after reading more ... Commented Jun 13, 2013 at 4:48

3 Answers 3

2
$sum=0; 
         while($row=mysql_fetch_array($result)){

                 $id = $row['id'];
                 $score = $row['score'];

                     if ($score == "1"){

                         $sum = $sum+$score;

                     }

         }
echo $sum;

try this. it sume al $score values.

Sign up to request clarification or add additional context in comments.

1 Comment

The comment I was replying to has been deleted.
2

You have to remove the if condition and add the database value to $sum variable

$sum = 0;
while($row=mysql_fetch_array($result)){

   $id = $row['id'];
   $score = $row['score'];
   $sum += (int)$score;
}   
echo $sum;

Comments

0

Several problems here, as other answers mostly fix, but to make clear:

  • your if $score == 1 doesn't seem relevant to your purpose. Did you mean if $id == 1, or were you just trying to ignore zeros? (anything + zero stays the same anyway, so you don't need to)
  • there doesn't seem to be a reason for subtracting one in $sum += $score-1 either
  • you need to finish the adding up first, and then call echo once. Currently, you have an echo for every database row, which is why you're seeing multiple numbers output.
  • if you're only displaying the sum anyway, you don't need to do this in a loop at all, just get the DB to add up for you, e.g. SELECT SUM(score) AS total_score FROM test or SELECT id, SUM(score) AS total_score FROM test GROUP BY id

Comments

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.