SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy $shipping+$$_SESSION['total_price'], 2 not working

    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.

    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($frakt2); ?></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
    }
    frakt is shipping in Swedish and total_pris is total_price. kr is the currancy.

    This is the cart-file (php)
    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&auml;tt Handla');
    display_button('checkout.php''ga-till-bestall''Best&auml;ll');

    do_html_footer();
    ?>
    This is part of the output:
    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
    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.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't looked to well into your cod, but in the line you're pointing out causing the trouble, you have a double dollar sign ($$). If you're not using variables variable you should only have one $. Try
    PHP Code:
    <?php echo number_format($frakt+$_SESSION['total_pris'], 2); ?>
    -Helge

  3. #3
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was thinking that the double $ was an error, but I'm still a newbie in this and thought that maybe it was supposed to be a variable variable. I'll try what you suggest.

    EDIT... It was the double $. I removed it and now it works. Don't they test their example-files?
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reptilianfeline
    Don't they test their example-files?
    Very often they don't do it enough.

    Glad you got it working though.

    -Helge

  5. #5
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now I have to build the rest on my own. The shopping cart seem to work. Items can be placed in it, and changed or removed, and the order is stored in the DB. There is a mock CC form that I think I can replace with whatever the CC processor gives me.

    So... I have to add a function that e-mails the order to me. I also have to add some kind of confirmation extra (not just "Your order have been placed") for the customer. It hasn't been decided what CC processor or merchant account to use with this.

    I also have to create a function to calculate the real shipping costs. It will be based on weight, so I have to weigh all the products . Anything else I should look into?
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •