1

i am doing a shopping cart.i had a variable $total.when i add 1st product of price 200 i get $total=200 , when i add 2nd product of price 100 i get $total=200100 when i add 3rd product of price 400 i get $total=200100400 . i want to get $total=700. how to fix this problem?

 session_start();
           $ar20=Db::getInstance()->ExecuteS('SELECT * FROM '._DB_PREFIX_.'price WHERE id="'.$_SESSION['y'].'"  AND prodid="'.$row['id_product'].'" ORDER BY size DESC');

            for($i=0;$i < count($ar20);$i++)
            {
                $test=$test+$ar20[$i]['price'];
            }




             $row['price'] += $test ;
 $row['total'] = $row['price'] * intval($row['quantity']);

if 3 products then echo the $row['total'] will get this 100200300

3
  • 2
    In php + does an implicit conversion to a numeric value. This might sound like a stupid question, but are you sure you're adding the numbers together in PHP an not in a Javascript code block embedded in a PHP doc? Commented Apr 2, 2010 at 5:21
  • session_start(); $ar20=Db::getInstance()->ExecuteS('SELECT * FROM '._DB_PREFIX_.'price WHERE id="'.$_SESSION['y'].'" AND prodid="'.$row['id_product'].'" ORDER BY size DESC'); for($i=0;$i < count($ar20);$i++) { $test=$test+$ar20[$i]['price']; } echo $test; $row['price'] += $test ; $row['total'] = $row['price'] * intval($row['quantity']); if 3 products then echo the $row['total'] will get this 100200300 Commented Apr 2, 2010 at 5:36
  • 1
    Please edit your question instead of posting code in comments, it's messy. Commented Apr 2, 2010 at 5:38

5 Answers 5

1

Use + instead of . operator

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

1 Comment

@Hmwd So, you're doing your math in javascript, not in PHP
1

Just get the value of price each time and add it in previous on like wise, the below logic may help u say $newprice is a variable where u get the price..

$total = 0;
$total +=$newprice;

1 Comment

i tried like as you shown but no changes, i gt the same like $total=200100400
0

Are you doing this?

$total = '100';
$total += '300';
$total += '300';
echo $total;

PHP is forgiving to a point, especially to the JavaScript fans: It allows you to use the + operator as concatenation if the operands are both strings. Make sure that you don't surround them in quotes, single or double, like so:

$total = 100;
$total += 300;
$total += 300;
echo $total;

1 Comment

session_start(); $ar20=Db::getInstance()->ExecuteS('SELECT * FROM '._DB_PREFIX_.'price WHERE id="'.$_SESSION['y'].'" AND prodid="'.$row['id_product'].'" ORDER BY size DESC'); for($i=0;$i < count($ar20);$i++) { $test=$test+$ar20[$i]['price']; } echo $test; $row['price'] += $test ; $row['total'] = $row['price'] * intval($row['quantity']); if 3 products then echo the $row['total'] will get this 100200300
0

Your code isn't clear because it has no context, but this should work:

$test = (integer) $test;

for($i=0; $i < count($ar20); $i++)
{
    $test = $test + (integer) $ar20[$i]['price'];
}

$row['price'] = (integer) $row['price'] + (integer) $test;

$row['total'] = $row['price'] * intval($row['quantity']);

2 Comments

i tried but the result is same. i used the code inside a foreach loop
I'm finding that kind of hard to believe, honestly.
0
session_start();

$records=Db::getInstance()->ExecuteS('SELECT * FROM '._DB_PREFIX_.'price WHERE id="'.$_SESSION['y'].'"  AND prodid="'.$row['id_product'].'" ORDER BY size DESC');

$row = array();
$test = '0';

if (count($records) < 1) {
    $row['price'] = '0';
    $row['total'] = '0';
} else {
    foreach($records as $record)
    {
        $test = bcadd($test, $record['price']);
    }

    $row['price'] = bcadd($row['price'], $test);
    $row['total'] = bcmul($row['price'], $row['quantity']);
}

PHP: bcadd

1 Comment

Check the bcadd, bcmul specification for scale.

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.