hello dears daniweb friend
i'm trying to get total price in php with the below code but i get only the shipping price when i request echo total what is wrong in my code ???

<form name="valide" method="POST" action="<?php echo $editFormAction; ?>">
          <label for="Sub total"></label>
          <label for="shipping">Sub total</label><input name="subtotal2" type="text" value="<?php echo number_format($row_total['sub_total'], 2, '.', ''); ?>" readonly disabledid="subtotal2">
          <label for="shipping">Shipping</label>
           <input name="shipping" type="text" id="shipping" value="<?php echo number_format($row_total['shipping'], 2, '.', ''); ?>" readonly>
          <label for="total"></label>
          <select name="shipping">
          <?php $shipping1='20.78';
          $shipping2='12.89';
          $shipping3='0.00';
          $itemprice=number_format ($row_panier['prix'], 2, '.', '');
          ?>
          <?php $totalpay= number_format ($row_panier['prix'], 2, '.', '')+($shipping1);?>
            <option value="20.00"><?php echo $shipping1 ?> </option>
            <option value="0.00"><?php  echo $shipping2 ?> </option>
            <option value="12.00"><?php echo $shipping3 ?></option>
          </select>
          <label for="shipping">Total</label><input name="total" type="text" id="total" value="<?php echo $shipping1+number_format ($row_panier['subtotal'], 2, '.', ''); ?>" readonly>
          <input name="total" type="text" value="<?php echo $totalpay; ?>" readonly>
  1. You are mixing values with formatted values (doubles and strings).
  2. what is $row_panier?
commented: very helpfull +2

For as far as I know, when you do something like this:

<?php echo $shipping1+number_format ($row_panier['subtotal'], 2, '.', ''); ?>

it is not just an echo that is taking place; rather the script is parsed one by one and $row_panier['subtotal'] is added to $shipping1. I think this can easily be fixed by containing your echo in parentheses, example:

<?php echo ($shipping1 + number_format($row_panier['subtotal'], 2, '.', '')); ?>
$row_panier?

is DB table
i have try this

<?php echo ($shipping1 + number_format($row_panier['subtotal'], 2, '.', '')); ?>

still same i get only

$shippping1

in navigator

You could try

<?php  echo number_format($shipping1 + $row_panier['subtotal'], 2, '.', ''); ?>

instead of

    <?php echo ($shipping1 + number_format($row_panier['subtotal'], 2, '.', '')); ?>

as it makes more sense to number format an int instead of number formatting an int to a string and then adding it to another int (as pritaeas says). Of course, both variables need to be set in order to do a sum. If this doesn't work, you could try checking for the presence of a value for both variables, for example:

<?php
echo '<p>The value of $shipping1 is:<br>';
var_dump($shipping1); echo '</p>';

echo '<p>The value of $row_panier[subtotal] is:<br>';
var_dump($row_panier['subtotal']); echo '</p>';

please help me with the same way the last post din't change anything

Could you add the following code right before your Total input and tell us what it says?

<?php
echo '<p>$shipping1 is now:';
var_dump($shipping1); echo '</p>';

echo '<p>$row_panier[subtotal] is now:';
var_dump($row_panier['subtotal']); echo '</p>';

echo '<p>The same stuff, number formatted is now:';
var_dump(number_format($shipping1 + $row_panier['subtotal'], 2, '.', '')); echo '</p>';

it done this one $shipping1 is now:string(5) "20.78"

$row_panier[subtotal] is now:NULL

The same stuff, number formatted is now:string(5) "20.78"
and the old result stay same the code is below

<?php $totalpay= number_format ($row_panier['prix'], 2, '.', '')+($shipping1);?>
            <option name="express" value="20.00"><?php echo $shipping1 ?> </option>
            <option name="free" value="0.00"><?php  echo $shipping2 ?> </option>
            <option name="zunaphone" value="12.00"><?php echo $shipping3 ?></option>
          </select><br>
         <?php echo '<p>$shipping1 is now:';
var_dump($shipping1); echo '</p>';
echo '<p>$row_panier[subtotal] is now:';
var_dump($row_panier['subtotal']); echo '</p>';
echo '<p>The same stuff, number formatted is now:';
var_dump(number_format($shipping1 + $row_panier['subtotal'], 2, '.', '')); echo '</p>';?>
          <label for="shipping">Total</label><input name="total" type="text" id="total" value="<?php echo ($shipping1+number_format ($row_panier['subtotal'], 2, '.', '')); ?>" readonly><br>
          <label for="total">Total</label><input name="total" type="text" value="<?php echo $totalpay; ?>" readonly>

please check $row_panier has value or not i think it taking value from database so check you query and query data having value or not

Member Avatar
diafol

php has loose typing so all the following should work...

echo number_format('3.456' + 1.457, 2, '.', '');
echo number_format('3.456' + NULL, 2, '.', '');
echo number_format(3.456 + NULL, 2, '.', '');

If however you have uninitialised variables...

echo number_format('3.456' + $x, 2, '.', '');
//Notice: Undefined variable: x in...

Or...

$x = array();
echo number_format('3.456' + $x['me'], 2, '.', '');
//Notice: Undefined index: me in... 

You should get error messages as commented

Apparently your $row_panier['subtotal'] has no value. I guess you could start by finding out why that is =).

i would like to thank Mr Diafol and Mr Manitauros and Aarti all yours help was helpfull, i undestund my problem is cause i mixed format number and integer..., it is solved with this code

<?php $shipping1='20.78';
          $shipping2='12.89';
          $shipping3='0.00';
          ?>
          <?php $totalpay=  ($row_total['sub_total'])+($shipping1);?>
            <option name="express" value="20.00"><?php echo $shipping1 ?> </option>
            <option name="free" value="0.00"><?php  echo $shipping2 ?> </option>
            <option name="zunaphone" value="12.00"><?php echo $shipping3 ?></option>
          </select><br>
          <label for="shipping">Total</label>
          <input name="total" type="text" value="<?php echo number_format ($totalpay, 2, '.',''); ?>" readonly><br>