I want to allow my users to store values in the $_SESSION
variable, I have this code:
<?php
if(!empty($_SESSION["shopping_cart"])) {
$total = 0;
foreach($_SESSION["shopping_cart"] as $keys => $values) { ?>
<tr>
<td><?php echo $values["item_name"]; ?></td>
<td><?php echo $values["item_quantity"]; ?></td>
<td>฿<?php echo $values["item_price"]; ?></td>
<td><?php echo number_format($values["item_quantity"] *
$values["item_price"], 7); ?></td>
<td><a href="atcform.php?action=delete&id=<?php echo $values["item_id"]; ?>
"><span class="remove-span">Remove</span></a></td>
</tr>
<?php
$total = $total + ($values["item_quantity"] * $values["item_price"]);
}
?>
<tr>
<td colspan="3" align="right">Total</td>
<td align="right">$<?php echo number_format($total, 7); ?></td>
</tr>
<?php
}
?>
When I run it it says:
Notice: A non well formed numeric value encountered in C:\\Users\\auser\\Desktop\\XAMPP\\htdocs\\Website1\\atcform.php on line 76 and line 72
Those two lines are where the number_format
s are, I would like to eliminate these errors, do tell me if you need to see more code.
Thank you!
Looks like one of the values inside of number_format()
are not of a numerical type.
Try casting the numerical values in floatval()
:
number_format(floatval($values["item_quantity"]) * floatval($values["item_price"]), 7);
Or even better, ensure that the values are floats before doing any numerical operation on them by re-assigning them at the beginning of the loop.
foreach ($_SESSION["shopping_cart"] as $keys => $values) {
$values["item_quantity"] = floatval($values["item_quantity"]);
$values["item_price"] = floatval($values["item_price"]);
....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.