I'm working on the shopping cart from PHP and MySQL Web development 2nd ed. I have with a little help and a lot of debugging managed to get it to almost work.
In the script and order-process the total amount/price of items are calculated. That's working fine. Then shipping is clculated (so far a set price). After that the shipping cost is added to the total price of items. That calculation DON'T work.
frakt is shipping in Swedish and total_pris is total_price. kr is the currancy.PHP Code:function display_shipping($frakt)
{
// display table row with shipping cost and total price including shipping
?>
<table>
<tr><td align="left">Frakt</td>
<td align="right"> <?php echo number_format($frakt, 2); ?></td></tr>
<tr><th align="left">Totalt inklusive frakt</th>
<th align="right"><?php echo number_format($frakt+$$_SESSION['total_pris'], 2); ?> kr</th>
</tr>
</table><br />
<?php
}
This is the cart-file (php)
This is part of the output:PHP Code:<?php
include ('book_sc_fns.php');
session_start();
@ $new = $_GET['new'];
if ($new)
{
if (!isset ($_SESSION['vagn']))
{
$_SESSION['vagn'] = array();
$_SESSION['varor'] = 0;
$_SESSION['total_pris'] = '0.00';
}
if (isset($_SESSION['vagn'][$new]))
$_SESSION['vagn'][$new]++;
else
$_SESSION['vagn'][$new] = 1;
$_SESSION['total_pris'] = calculate_price($_SESSION['vagn']);
$_SESSION['varor'] = calculate_items($_SESSION['vagn']);
}
if (isset($_POST['spara']))
{
foreach ($_SESSION['vagn'] as $bokid => $antal)
{
if ($_POST[$bokid]=='0')
unset ($_SESSION['vagn'][$bokid]);
else
$_SESSION['vagn'][$bokid] = $_POST[$bokid];
}
$_SESSION['total_pris'] = calculate_price($_SESSION['vagn']);
$_SESSION['varor'] = calculate_items($_SESSION['vagn']);
}
do_html_header('Din kundvagn');
if ($_SESSION['vagn']&&array_count_values($_SESSION['vagn']))
display_cart($_SESSION['vagn']);
else
{
echo '<p>Det finns inga varor i din kundvagn.</p>';
echo '<hr />';
}
$target = 'index.php';
if ($new)
{
$details = get_book_details($new);
if ($details['katid'])
$target = 'show_cat.php?katid='.$details['katid'];
}
display_button($target, 'fortsatt-handla', 'Fortsätt Handla');
display_button('checkout.php', 'ga-till-bestall', 'Beställ');
do_html_footer();
?>
The line "Totalt inklusive frakt" should have the total price (858.00) plus 20.00, NOT just 20.00. My guess the error is in the line "<?php echo number_format($frakt+$$_SESSION['total_pris'], 2); ?>" but I don't know what it is. I have just translated the script from English and an earlier version of PHP. $_SESSION used to be $HTTP_SESSION_VARS. Please help me.Totalt antal varor: 2
Totalt pris: 858.00 kr
Vara - Pris - Antal - Totalt
ECDL - 429.00 kr - 2 - 858.00 kr
Frakt 20.00
Totalt inklusive frakt 20.00 kr







Bookmarks