1

Here is my code but it displays two different sum of values. I have total with per row and it has 2 value 100 and 200 . it displays 100 and 200 seperately. But i want the sum of 100 and 200 . sum= 300 how could i do it?

$result2 = mysql_query("select * from price  where dom='$cat'",$db);
while ($myrow2 = mysql_fetch_array($result2))
{
    echo $myrow2["totalwithper"];
}

here is my table structure

CREATE TABLE `price` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `dom` varchar(255) COLLATE utf8_bin NOT NULL,
  `etiket` varchar(255) COLLATE utf8_bin NOT NULL,
  `pricestandart` int(5) NOT NULL,
  `number` int(5) NOT NULL,
  `totalunper` int(5) NOT NULL,
  `discount` int(5) NOT NULL,
  `totalwithper` int(5) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=20 ;

--
-- Dumping data for table `price`
--

INSERT INTO `price` (`id`, `dom`, `etiket`, `pricestandart`, `number`, `totalunper`, `discount`, `totalwithper`) VALUES
(18, 'Alten Group', 'flayer', 100, 1, 100, 10, 90),
(19, 'Alten Group', 'logo', 100, 2, 200, 15, 170);

4 Answers 4

1

I have spent the last couple of days trying to make the array_sum() function work and it would print out every number but wouldn't add them together. But with this code it finally worked for me TY.

 $sum=0;
 $result2 = mysql_query ("select totalwithper from price  where dom='$cat'",$db);
 while($myrow2=mysql_fetch_array($result2))
 {
    $sum=$sum+ $myrow2["totalwithper"];
 }

 echo "sum : $sum";
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

  $result2 = mysql_query ("select sum(totalwithper) from price  where dom='$cat'",$db);
  $myrow2= mysql_fetch_array($result2);
  do{
     echo $myrow2["totalwithper"];
    }
    while($myrow2=mysql_fetch_array($result2));

a small rewrite to your code :

 $result2 = mysql_query ("select sum(totalwithper) from price  where dom='$cat'",$db);
 while($myrow2=mysql_fetch_array($result2))
        {
         echo $myrow2["totalwithper"];
        }

sum by loop

 $sum=0;
 $result2 = mysql_query ("select totalwithper from price  where dom='$cat'",$db);
 while($myrow2=mysql_fetch_array($result2))
        {
         $sum=$sum+ $myrow2["totalwithper"];
        }

 echo "sum : $sum";

In $sum you will get the sum.

7 Comments

If you fix the indenting and remove the useless blank lines it will look even nicer :-)
thank all of you But now it doesnt give any result. do u have any idea? or how could use loop get all values from fetch array then adding up them? what you think?
ali can you share your table structure
i posted table structure at the top of page where i asked question.
are you looking for the sum of totalunper
|
0
SELECT Sum(totalwithper) FROM price  WHERE ...

see:

Comments

0

Use

$result2 = mysql_query ("select sum(totalwithper) from price  where dom='$cat'",$db);

1 Comment

@parvin: in quesion 'Ali Shikhiyev' has mentioned as total with per row

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.