Shopping Basket

Discussion in 'PHP' started by gazza52_2000, Aug 13, 2007.

  1. #1
    Hi,

    I'm trying to design a very basic shopping basket.

    I can add items to the basket from a mysql search, empty the basket and also display a note on screen of the number of items in the basket.

    One thing i cannot work out is how to remove an individual item.

    Here is my code.

    add.php
    <?
    $items=$_GET["items"];
    $propertyid=$_GET["propertyid"];
    echo "You have $items items in your basket<br>";
    echo"<a href=\"basket.php\">View Basket</a>";
    ?>
    </div>
    <h1>Student Accommodation Services</h1>
    <div id="content">
    <?
    if (isset($_GET["add"]))
    {
    $_SESSION["basket"][]=$_GET["add"];
    if (isset($_GET["add"]))
    $_SESSION["basketcount"] = $_SESSION["basketcount"] + 1;
    echo "item added<BR><BR>";
    }
    if (isset($_GET["empty"]))
    {
    unset($_SESSION["basket"]);
    $_SESSION["basketcount"] = 0;
    echo "basket emptied<BR><BR>";
    }

    ?>

    basket.php

    <?

    if (isset($_SESSION["basket"]))
    {
    foreach ($_SESSION["basket"] as $item)
    {
    print ("<tr>");
    print ("<td>$propertyid</td>");
    print ("<td>$item</td>");
    print ("<td><a href=\"add.php?\">Remove</a></td>");
    print ("<tr>");

    }
    }
    ?>

    results.php (my results from mysql search)

    <? $hostname = "localhost"; // The DB server.
    $username = "root"; // The username you created for this database.
    $password = ""; // The password you created for the username.
    $usertable = "sas"; // The name of the table you made.
    $dbName = "cp1079"; // This is the name of the database you made.
    $metode = $_POST["metode"];
    $search = $_POST["search"];

    MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
    @mysql_select_db( "$dbName") or die( "Unable to select database");
    ?>
    <?
    //error message (not found message)begins
    $XX = "No Record Found, to search again please close this window";
    //query details table begins
    $query = mysql_query("SELECT * FROM $usertable WHERE $metode LIKE '%$search%' LIMIT 0, 50");
    while ($row = @mysql_fetch_array($query))
    {
    $variable1=$row["propertyid"];
    $variable2=$row["address"];
    $variable3=$row["town"];
    $variable4=$row["postcode"];
    $variable5=$row["landlord"];
    $variable6=$row["bedrooms"];
    $variable7=$row["photo"];
    $variable7="<a href=\"add.php?add=$variable2, $variable3, $variable4, $variable5,\">Add Accommodation</a>";
    //table layout for results

    print ("<tr>");
    print ("<td>$variable2</td>");
    print ("<td>$variable3</td>");
    print ("<td>$variable4</td>");
    print ("<td>$variable5</td>");
    print ("<td>$variable6</td>");
    print ("<td>$variable7</td>");
    print ("</tr>");
    }
    //below this is the function for no record!!
    if (!$variable1)
    {
    print ("$XX");
    }
    //end
    ?>

    Thanks again
     
    gazza52_2000, Aug 13, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you're effectively using an array stored in the session to hold the items?

    unset() will remove an element from the array. unset(array['elementKey']);

    note, however, that your link to remove the items points to add.php!

    
    print ("<td><a href=\"add.php?\">Remove</a></td>"); 
    
    PHP:
     
    ecentricNick, Aug 14, 2007 IP
  3. gazza52_2000

    gazza52_2000 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I understand what you mean here, but going from my code how do i select the id to remove.
     
    gazza52_2000, Aug 15, 2007 IP
  4. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, to be painfully blunt (sorry) I think much of the code is flawed.

    Correct me if I'm wrong, but I'm not sure you've got your head around what you're actually storing in the session. In particular, you have an array named "basket" but what you put in it, and how you get stuff out of it feels wrong to me.

    So, I think you need to step back a moment and look at this problem from first principles.

    Can I ask you to state the design principles you're working from? In particular, here's how I think a basket should work. Tell me if you're working from a radically different direction...

    - You require a shopping basket.
    - Each user's basket will be unique.
    - You only require (at least at present) the basket to have a life span of the user's visit (in other words, if they leave the site and come back tomorrow, you don't mind that they lose their basket).

    In order to accomodate the above, the $_SESSION object will be sufficient.

    So, the key to the design is deciding how to store everything in the $_SESSION.

    Your basket is made up of one or more items. Property ID appears to be the uniquely identifying piece of information that is critical to store in the basket?

    So, the key funcitonality you're going to require is going to be...

    Initialisation... setting up an empty basket for the user
    Adding an item... appending a new Property ID to the basket, possibly for convenience with some other details to save repeated database lookups.
    Listing the items... outputting the basket contents
    Removing an item... given a property ID, removeing that item from the basket
    Clearing the basket... probably the same as initialisation
    Counting the items... either performing a dynamic count or maintaining a counter of the number of items in the basket

    Presumably, you are not interested in...
    Managing quantities - ie, can you put the same item in the basket more than once? But you may want to explicitly forbid that.
    Dealing with pricing at that point?
     
    ecentricNick, Aug 15, 2007 IP
  5. gazza52_2000

    gazza52_2000 Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I have a static login, so only one basket required.

    I connect to a simple mysql database which returns results.

    The results are

    Propertyid - id field
    address
    town
    bedrooms
    landlord.

    What i have done as you can see from my code is add a link to add to basket which stores these items into an array called items which is then named as session basket.

    I can output the contents of items to my basket when they are added, view them. I can use the unset method empty the contents of the session basket.

    What i need to do is use the unset method to just remove one item from the array, when i click the remove button.

    I need help just creating this part, from what i gather i need to get the id field (propertyid) for the item i want removing and unset it.

    Trouble is i havent got a clue.

    Please help.
     
    gazza52_2000, Aug 15, 2007 IP
  6. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok, but, as I mentioned, I think your code is a little flawed.

    You pick up the values in $_GET['add'] to place in your array. But, that is just a comma delimited list. So you end up with....

    Basket[0]="value 1, value2, value 3...etc"
    Basket[1]="another value 1, another value 2, another value 3...etc"

    So it's not really very structured in that you can't do much with those without extra work.

    It would have been better to pass through each element seperately....

    add.php?id=<property id>&address=<property address>&someotherthing=<someothervalue>......

    That way, you could have done...
    $item['id']=$_GET['id]
    $item['address']=$_GET['address']
    :etc

    then stuffed $item into your basket.

    That way, when you're doing your...for each $basket as item, you can test what $item['id'] is and see if that's the basket element you need to unset.

    Even better would have been to also key the results in basket...
    $basket[$_GET['id']]=$item;

    which means you can remove an item from the basket with unset($basket[$_GET['id']])

    I think you're on the right track - you just need to read up a bit more on array handling to make sure you know how to reference individual elements by key.

    http://www.php.net/types.array

    I really don't mean the above to sound so personally disparraging. It's clear you have a pretty good grasp on this - you're just not quite there. But you should be able to get this working pretty quickly.
     
    ecentricNick, Aug 15, 2007 IP