SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    http://TheICE.co.uk/ The Eerie Network's Avatar
    Join Date
    Apr 2001
    Location
    London in the UK.
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with some code

    OK, here is what I have so far:
    PHP Code:
    cart.php
    <?
    require('config.php');
    if (
    $action == 'empty') {
        
    // remove items from cart
        
    session_unset();
    } elseif (
    $action == 'add') {
        
    // add items to array
        
        
    $cart_product[] = array (item_id => $producthowmany => $howmany);
        
        
    session_register('cart_product');
    }


    $body '<p class=title>Your Cart</p>';

    if (isset(
    $cart_product)) {
        
    $body .= '<p>';
        foreach(
    $cart_product as $list_product) {
            
    $body .= 'Item id: '.$list_product[item_id].' How Many: '.$list_product[howmany].'<br>';
        }
        
    $body .= '</p>';
    } else {
        
    $body .= '<p> Your cart is empty</p>';
    }



    // Output all details

    echo $body;

    ?>

    config.php

    ?>

    session_start();
    function mysqlconnect($db='job2look') {
            $user = 'root';
            $pass = 'whatever';
            $GLOBALS[connect] = mysql_connect( 'localhost', $user, $pass );
            if ( ! $GLOBALS[connect] )
                die( 'Could not connect to MySQL' );
            mysql_select_db( $db, $GLOBALS[connect] )
                or die ( 'Could not open $db: '.mysql_error() );
    }
    ?>
    Now as you may have worked out, i'm trying to build a shopping cart, but i'm getting knowhere fast.

    I'm considdering killing all this code (just to put it out of it's mysery) and start again, before I do, can anyone give me some hints and tips, or maybe even some sample code that I can look though.

    I've read a lot of tutorials, but each one seams to have flaws, and i'm just not really getting anywhere.

    Thanx in advance
    Daniel Hollands
    New: My Webcam <-- Come and watch me doing what I do
    TheICE.co.uk <-- New sites for the best in UK lifestyle and Competitions.
    The ofDan.Network Forums <-- Come and join in the fun.

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What exactly is the problem? I presume from your code that you aren't able to loop through the $cart_product array and print anything. Your syntax is a bit off in that area.

    PHP Code:
    require('config.php');
    if (
    $action == 'empty') {
        
    // remove items from cart
        
    session_unset();
    } elseif (
    $action == 'add') {
        
    // add items to array
        
        
    $cart_product[] = array (item_id => $producthowmany => $howmany);
        
        
    session_register('cart_product');
    }


    $body '<p class=title>Your Cart</p>';

    if (isset(
    $cart_product)) {
        
    $body .= '<p>';
        foreach(
    $cart_product as $list_product) {
            
    $body .= 'Item id: '.$list_product[item_id].' How Many: '.$list_product[howmany].'<br>';
        }
        
    $body .= '</p>';
    } else {
        
    $body .= '<p> Your cart is empty</p>';

    Actually needs to look more like

    PHP Code:

    require('config.php');
    if (
    $action == 'empty') {
        
    // remove items from cart
        
    session_unset();
    } elseif (
    $action == 'add') {
        
    // add items to array
        
        
    $cart_product[] = array (item_id => $producthowmany => $howmany);
        
        
    session_register('cart_product');
    }


    $body '<p class=title>Your Cart</p>';

    if (isset(
    $cart_product)) {
        
    $body .= '<p>';
            foreach(
    $cart_product as $key => $list_product) {
            
    $body .= 'Item id: '.$cart_product[$key]['item_id'].' How Many: '.$cart_product[$key]['howmany'].'<br>';
        }
        
    $body .= '</p>';
    } else {
        
    $body .= '<p> Your cart is empty</p>';

    As the $list_product is not going to produce the correct results, you actually need to access the $cart_product elements.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    http://TheICE.co.uk/ The Eerie Network's Avatar
    Join Date
    Apr 2001
    Location
    London in the UK.
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thank you for the correction, however that was not the major problem. The problem was editing the values in the arrays once they had been set, and, for example, if one of the elements in the array was set to:

    Code:
    product |  quantity
    Tea     |  1
    If the user wanted to change the quanity to say 3, how would I do this? I hope this is making sense to you
    Daniel Hollands
    New: My Webcam <-- Come and watch me doing what I do
    TheICE.co.uk <-- New sites for the best in UK lifestyle and Competitions.
    The ofDan.Network Forums <-- Come and join in the fun.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Store the items and quantities in a temporary table, display text boxes next each one and allow user to change value in textbox resubmit and update the records in the temporary table with the new values. Then when time to checkout, you have the records from the temporary table to work with and you can then remove them from the temporary table.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    http://TheICE.co.uk/ The Eerie Network's Avatar
    Join Date
    Apr 2001
    Location
    London in the UK.
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This sounds like a good way of doing it, i'll have to try it and see what happens, cheers
    Daniel Hollands
    New: My Webcam <-- Come and watch me doing what I do
    TheICE.co.uk <-- New sites for the best in UK lifestyle and Competitions.
    The ofDan.Network Forums <-- Come and join in the fun.


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
  •