I've been getting problems with my formula to total the deposit and price. I'm getting and error on this line

$total += $total + $deposit['deposit'] * $pamount['price_status'];

Here's more but only an error with the total.

$prod_refid=$_REQUEST['refid'];
  $productsql="select * from products where prod_refid='$prod_refid'";
  $resquery = @mysql_query($productsql);
  $resproduct_fetch=mysql_fetch_array($resquery);
  $image_path="products/";
  $thumb_path="products/thumb/";

  $product_id=$resproduct_fetch['product_id'];
  $prod_refid=$resproduct_fetch['prod_refid'];
  $product_image=$resproduct_fetch['product_image'];
  $product_name=$resproduct_fetch['product_name'];
  $prod_country=$resproduct_fetch['prod_country'];
  $price_range=$resproduct_fetch['price_range'];
  $pamount = number_format($price_range, 2, '.', '');
  $price_hour=$resproduct_fetch['price_hour'];
  $price_hour = number_format($price_hour, 2, '.', '');
  $price_week=$resproduct_fetch['price_week'];
  $price_week = number_format($price_week, 2, '.', '');
  $price_month=$resproduct_fetch['price_month'];
  $price_month =number_format($price_month, 2, '.', '');
  //$pamount = number_format(((float)$price_range),2);
  $deposit=$resproduct_fetch['deposit'];
  $deposit = number_format($deposit, 2, '.', '');
  $prod_userid=$resproduct_fetch['prod_userid'];
  $prod_usertype=$resproduct_fetch['prod_usertype'];
  $prod_desc=$resproduct_fetch['prod_desc'];
  $prod_minduration=$resproduct_fetch['prod_minduration'];
  $prod_maxduration=$resproduct_fetch['prod_maxduration'];
  $prod_avail_days=$resproduct_fetch['prod_avail_days'];
  $prod_paymethod=$resproduct_fetch['prod_paymethod'];
  $total += $total + $deposit['deposit'] * $pamount['price_status'];
  $cntry_sql=mysql_query("select country,countrycode,country_abbr from country where countryid ='".$prod_country."' and country_status='0'");

            $cntry_fetch=mysql_fetch_array($cntry_sql);

            $user_sql=mysql_query("select country,state,city,firstname,lastname,address1,address2,postalcode,companyname,userid from userregister where userid ='".$prod_userid."' and status='1'");
            $userfetch=mysql_fetch_array($user_sql);

Dani AI

Generated

A short diagnosis and a clean pattern to avoid the problems seen in this thread.

The original error was caused by treating scalar values as arrays (for example accessing something like $deposit['deposit'] after $deposit was overwritten with a formatted string) and by doing multiple assignments/arithmetics inside one expression. Assignment is right‑associative in PHP, so combining assignments and math inside an if can silently produce the wrong operands (which explains the unexpected “7” total). was correct to simplify the operator use, and ’s request for the exact error message was the right diagnostic move.

A safer, clearer approach is: initialize totals, pull raw numeric values, do math on plain floats, then format only for output. Example pattern:

$total  = 0.0;
$price  = (float) $resproduct_fetch['price_range'];
$deposit = (float) $resproduct_fetch['deposit'];

$total = $price + $deposit;            // use += only when intentionally accumulating
echo number_format($total, 2, '.', '');

Troubleshooting checklist (apply before calling number_format or embedding calculations in an if):

  • Ensure keys exist in $resproduct_fetch (use var_dump($resproduct_fetch) to inspect).
  • Don’t use array syntax on scalars (avoid $scalar['key'] unless it’s an array).
  • Avoid complex assignments inside conditionals; compute first, then test:
    $total = $price + $deposit;
    if ($total > 0) { echo number_format($total,2,'.',''); } else { echo '0.00'; }
  • Keep numbers as numeric types until final display; number_format returns a string.

Also note: suppressing errors with @ and using old mysql_* functions is fragile and insecure. Migrate queries to mysqli or PDO with prepared statements and validate/filter input (prod_refid) to prevent SQL injection. These small changes make totals predictable and easy to debug.

Recommended Answers

All 9 Replies

could you post what kind of error it is?

it would be helpful for us to give a solution if you post the error message here

happy coding

$total += $total + $deposit['deposit'] * $pamount['price_status'];

change these to

$total = $total + $deposit['deposit'] * $pamount['price_status'];

that and then chek the calculation

The error is gone now.
But now when it's calculating, it only gives me a 7 as an answer
Example: Price:150.00 + Deposit:700.00 = Total:7

Here's my code now...

 <?php if($total += $pamount=$resproduct_fetch['price_status'] * $deposit=$resproduct_fetch['price_status']) echo $resproduct_fetch['total']." ".$total;else echo "0.00";   ?>

i couldn't understand what you are going to do in the given bellow statement

whether you assigning value to some varable or comparing values of 2 variables

if($total += $pamount=$resproduct_fetch['price_status'] * $deposit=$resproduct_fetch['price_status'])

please be more brefly about your requirement

could you explain what you are going to do with that?

put your calculation part outside if condition take all calculation what you wnat in a varibale and then apply them in if condition.

I guess, what I'm trying to do is to fetch the variables from here...

$prod_refid=$_REQUEST['refid'];
  $productsql="select * from products where prod_refid='$prod_refid'";
  $resquery = @mysql_query($productsql);
  $resproduct_fetch=mysql_fetch_array($resquery);
  $image_path="products/";
  $thumb_path="products/thumb/";

  $product_id=$resproduct_fetch['product_id'];
  $prod_refid=$resproduct_fetch['prod_refid'];
  $product_image=$resproduct_fetch['product_image'];
  $product_name=$resproduct_fetch['product_name'];
  $prod_country=$resproduct_fetch['prod_country'];
  $price_range=$resproduct_fetch['price_range'];
  $pamount = number_format($price_range, 2, '.', '');
  $price_hour=$resproduct_fetch['price_hour'];
  $price_hour = number_format($price_hour, 2, '.', '');
  $price_week=$resproduct_fetch['price_week'];
  $price_week = number_format($price_week, 2, '.', '');
  $price_month=$resproduct_fetch['price_month'];
  $price_month =number_format($price_month, 2, '.', '');
  //$pamount = number_format(((float)$price_range),2);
  $deposit=$resproduct_fetch['deposit'];
  $deposit = number_format($deposit, 2, '.', '');
  $prod_userid=$resproduct_fetch['prod_userid'];
  $prod_usertype=$resproduct_fetch['prod_usertype'];
  $prod_desc=$resproduct_fetch['prod_desc'];
  $prod_minduration=$resproduct_fetch['prod_minduration'];
  $prod_maxduration=$resproduct_fetch['prod_maxduration'];
  $prod_avail_days=$resproduct_fetch['prod_avail_days'];
  $prod_paymethod=$resproduct_fetch['prod_paymethod'];
  $cntry_sql=mysql_query("select country,countrycode,country_abbr from country where countryid ='".$prod_country."' and country_status='0'");

            $cntry_fetch=mysql_fetch_array($cntry_sql);

            $user_sql=mysql_query("select country,state,city,firstname,lastname,address1,address2,postalcode,companyname,userid from userregister where userid ='".$prod_userid."' and status='1'");
            $userfetch=mysql_fetch_array($user_sql);

Thank you guys, by the way...
I very much apreciate you helping me ^_^

I got it working :)
Thanks for the ideas...
I did this :)

<?php if($total = $total + $pamount + $deposit) echo $resproduct_fetch['total']." ".$total;else echo "0.00";   ?>

if your problem is solved then mark your question solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.