Grow your CSS skills. Land your dream job.

need help making a script

  • # December 16, 2009 at 9:21 am

    Hi everyone.

    Hi need help to build a script, here is what i want..

    i have two pages, first for the produts, second a quote form page. i whant thoose products have an icon that when the user clicks its adds to some database and then appears in the quote page with some more inputs (width, height, quantity) of the product.

    Example: the user added product #1 to quote page;

    Quote:
    products added:

    product #1 – reference bla bla bla
    quantity: __________________
    width: __________________
    height: __________________

    product #2 – reference bla bla bla
    quantity: __________________
    width: __________________
    height: __________________

    (send form)

    and the items always appears when in the same session…

    help please! dont know where to start.. thanks :)

    # December 21, 2009 at 6:17 am

    No one?

    # December 21, 2009 at 10:48 am

    I would save the persons shopping cart in a PHP session separated with commas.

    Code:
    $_SESSION['user_cart'] = ’5,5,2,4,2′;

    Then use jQuery to send information to a php page to add to the session.

    Code:
    jQuery(document).ready(function($) {

    $(‘.product’).click(function() {

    // The link will look like this
    // Add Product
    var prod_id = $(this).attr(‘rel’).replace(/id-/, ”);

    $.get(‘add2cart.php’, {item_id: prod_id});

    return false;
    });

    });

    // The PHP page

    Code:
    < ?php
    session_start();

    if( isset($_SESSION['user_cart']) ) {
    $_SESSION['user_cart'] .= ',' . $_GET['item_id'];
    }else {
    $_SESSION['user_cart'] = $_GET['item_id'];
    }
    ?>

    And then loop through the session to display each item

    Code:
    < ?php
    session_start();

    $cart = (isset($_SESSION['user_cart'])) ? $_SESSION['user_cart'] : false;

    if( $cart ) {

    $items = explode(',', $cart);
    $item_quantity = array();

    // Loop through items to get quantity
    foreach( $items as $item ) :
    if( isset($item_quantity[$item]) )
    $item_quantity[$item]++;
    else
    $item_quantity[$item] = 1;
    endforeach;

    // Loop through items again to
    // show title and quantity
    foreach( $item_quantity as $id => $quantity ) :
    echo $id . ‘ = ‘ . $quantity . ‘
    ‘;
    endforeach;

    }else
    echo “Your cart is empty”;
    ?>

    In the last foreach, you’ll want to make a function to get the item’s title from the database.
    Similar to this.

    Code:
    function title_from_id($id) {
    $id = mysql_real_escape_string($id);
    $sql = “SELECT * FROM `products` WHERE `id` = ‘$id’”;
    $res = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_assoc($res);
    return $row['title'];
    }
    # December 21, 2009 at 10:58 am

    I’ve zipped up all my files that I used to test this.

    # December 21, 2009 at 11:13 am

    Yaiks! Thanks man..

    1. There is anyway to adapte the script to use prototype instead of jquery? (its the library that im using in the page)

    2. Can i put the product name like..

    <a rel="id-product name" class="product" href="#">Add Product</a>

    # December 21, 2009 at 4:50 pm

    I don’t know prototype so you’ll have to look through the documentation.

    If you want to use the product name, then it should work just like that.

    # December 22, 2009 at 5:17 am

    Ok, thank you. One more thing: whats the code to a "remove" icon?

    Edited: 2º question.. i want to send the data by email.

    Code:
    foreach( $item_quantity as $id => $quantity ) :
    echo ‘

    Nome do producto
    Remover




    ‘;
    endforeach;

    Code:
    $nomeprod = $_POST["Nomeprod"];
    $comprimento = $_POST["Comprimento"];
    $largura = $_POST["Largura"];
    $altura = $_POST["Altura"];
    $consumo = $_POST["Consumo"];
    Code:
    Produto: $nomeprod
    Comprimento: $comprimento
    Largura: $largura
    Altura: $altura
    Consumo: $consumo

    Basiclly is that. But i want this for every products in the cart.. how can i change to dynnamic variables?

    # December 28, 2009 at 11:04 am

    The problem of sending data by email is solved.. i just need to code a "remove" button.

    # December 30, 2009 at 7:55 am

    the code for remove button…

    Code:
    if( isset($_SESSION['user_cart']) ) {
    if ($numero > 1) {
    $del = “,”;
    $del .= $_GET['del_id'];
    $_SESSION['user_cart'] = str_replace($del, “”, $_SESSION['user_cart']);

    } else {
    unset($_SESSION['user_cart']);
    }

    }else {
    echo “erro”;
    }

    Its working.. just one problem. Cant remove the first item, becase dont have the ‘,’ before.. how can i solve it? help please :)

    PROBLEM SOLVED, here is the solution:

    Code:
    if( isset($_SESSION['user_cart']) ) {
    if ($numero > 1) {
    if ($items[0] == $_GET['del_id']) {

    $del = $_GET['del_id'];
    $del .= “,”;
    $_SESSION['user_cart'] = str_replace($del, “”, $_SESSION['user_cart']);

    } else {

    $del = “,”;
    $del .= $_GET['del_id'];
    $_SESSION['user_cart'] = str_replace($del, “”, $_SESSION['user_cart']);

    }

    } else {
    unset($_SESSION['user_cart']);
    }

    }else {
    echo “erro”;
    }

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".