Page 1 of 1

Php shop help

Posted: Sun Apr 27, 2008 12:36 pm
by jibber4568
Hi have created a shopping cart through a number of examples using php and ajax.
This cart allows me to drag an item into the cart and then the cart updates displaying that items class name.

I have never done any php before so am having trouble adding to this.

I want to add to the script an if statement that would say for example

if(id="item1")
{
price=20
}
else
if{(id="item2")
{
price=15
}

etc etc

I hope you can understand what i mean.

Would be extremely gratefull if someone could help me with how to write this within my script.

html

Code: Select all

 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" href="stylesheet/design.css" type="text/css">
    <script type="text/javascript" src="javascript/effect.js"></script>
    <script type="text/javascript" src="javascript/prototype.js"></script>
    <script type="text/javascript" src="javascript/src/scriptaculous.js?load=effects,dragdrop"></script>
    <link rel="stylesheet" href="StyleSheet.css" type="text/css" media="screen" />
    <script language="JavaScript" type="text/javascript" src="Javascript/JScript.js"></script>
    
 
</head>
<body>
 
    <div id="wrapper">
    
       <div id="bordertop">&nbsp;
       </div>
        <div id="bordermiddle">&nbsp;
        
            <div id="header" style=" position: relative; left: 3.5%; bottom: 20px; width: 96.5%; height: 200px;">
            </div>
        
          
             <div id="menu" style="position: relative; left: 3%; bottom: 20px; width: 96.5%">
            
           <a href ="http://www.google.com"><img src="images/images/home_btn.png" border="0" alt="Home"/></a><a href ="javascript&#058;artists();"><img src="images/images/artists_btn.png" border="0" alt="Artists" /></a><a href ="index2.php"><img src="images/images/shop_btn.png" border="0" alt="Shop" /></a><a href ="javascript&#058;accordion();"><img src="images/images/events_btn.png" border="0" alt="Events" /></a><a href ="/mp3/mp3player.swf" onclick="loadSWF(this.href); return false;"><img src="images/images/listen_btn.png" border="0" alt="Listen" /></a><a href ="http://www.google.com"><img src="images/images/contact_btn.png" border="0" alt="Contact" /></a> 
            </div>
           
            <div id="content" style="position:relative; left:3%;">
           <div class="item" id="WaxhedsCd">
             <img  id="item1" src="images/img.jpg">
         </div>
 
          <div class="item" id="KoasteCd">
             <img src="images/koastecd.jpg">
         </div>
         
         
          <div class="item" id="WaxhedsTee">
             <img src="images/waxtee.png">
         </div>
         
         
          <div class="item" id="SmashingCap">
             <img src="images/smashcap.png">
         </div>
         
         
          <div class="item" id="WaxCup">
             <img src="images/waxcup.png">
         </div>
         
         
         <div class="item" id="SmashingPillow">
             <img src="images/smashpillow.png">
         </div> 
         
         
         <div class="item" id="OlliePCd">
             <img src="images/olliecd.png">
         </div>   
   
<div class="cart">
        <img id="cart_img" src="images/cart.gif" style="float:left"> <label id="titles">Drag your items here...</label>
        <div id="split_line"></div>
        <div id="items" style="width:215;height:200">
            
        </div>
    </div>
               
               
            </div>
          </div>
        <div id="borderbottom">&nbsp;
        </div>
       
      
    </div>
   
 
</body>
 
  <script>
    makeDraggableItems();
  </script>
</html>
 
 
php

Code: Select all

 
<?php
session_start();
$elements_arr;
$elementID = $_GET['id'];
if(isset($_SESSION['user_id']))
{
    $elements_arr = $_SESSION['user_id'];
    if(isset($elements_arr[$elementID]))
        {
            $no_of_items = $elements_arr[$elementID];
            $no_of_items = (int)$no_of_items + 1 ;
            $elements_arr[$elementID] = $no_of_items ;
        }
        else
            $elements_arr[$elementID] = 1;
    $_SESSION['user_id'] = $elements_arr;
}
else
{
    $elements_arr[$elementID] = 1;
    $_SESSION['user_id'] = $elements_arr;
}
$keys = array_keys($elements_arr);
for($i = 0 ; $i < count($keys) ; $i++)
 
{
    print("<div id='div'".$keys[$i].">");
    print("&nbsp;<img src='images/point.png' style='margin-top:5px;' width=20px height= 20px>&nbsp;");
    print("<label class='ajax_item'>".$keys[$i]."  (".$elements_arr[$keys[$i]].")</label>&nbsp;");
    print("&nbsp;<img src='images/DeleteIcon.gif' class='delete_img' onclick=(removeItem('".$keys[$i]."'));>");
    print("<br>");
    print("<div id='split_line'></div>");
    print("</div>");
}
?>  
 
ajax

Code: Select all

 
var elementID;
var url;
var updatedDIV = "items";
var xmlHttp = false;
 
function makeDraggableItems()
{
    var items = document.getElementsByClassName('item');
    for(var i = 0; i < items.length ; i++)
        {
            new Draggable(items[i].id,{revert: true,ghosting: true});
            Droppables.add('items', {accept:'item',onDrop:function (element)
                {
                    elementID = element.id;
                    url = "add_to_cart.php?id=" + elementID;
                    makeAJAXRequest();
                }});
        }
  }
 
If it helps you can also see this example at the following url

http://jibber4568.gigacities.net/index2.php

Many thanks

Jibber4568

Re: Php shop help

Posted: Sun Apr 27, 2008 12:56 pm
by aceconcepts

Code: Select all

 
if($id=="item1")
{
$price=20;
}
elseif($id=="item2")
{
$price=15;
}
 
The above example illustrates the correct syntax to use for PHP. You could clean this up a bit by using a switch() - http://uk3.php.net/switch

Re: Php shop help

Posted: Sun Apr 27, 2008 1:02 pm
by jibber4568
great thanks alot for that, i'll see what i can do with it.

Although am i right in thinking for the above example i would be saying

if($elementID=="item1")

etc etc

Re: Php shop help

Posted: Sun Apr 27, 2008 1:08 pm
by aceconcepts
It would be a product id - an id from a database, because you'd be checking a product right?

Re: Php shop help

Posted: Sun Apr 27, 2008 1:12 pm
by jibber4568
No its not actually going to use a database for the products.

I can add each image and it will return their class id.

I was hoping now i could write some sort of if statement to assign a price depedning on the class name, and then return that price along with the class name.

Re: Php shop help

Posted: Sun Apr 27, 2008 1:17 pm
by aceconcepts
In that case you are correct.

Re: Php shop help

Posted: Sun Apr 27, 2008 1:37 pm
by jibber4568
Great, cheers for that.

However as i said before i am pretty new to the devlopment world, especially php.

I was hoping you could possibly point out the best place to put this and how to call it with the class id.

Code: Select all

 
<?php
session_start();
$elements_arr;
$elementID = $_GET['id'];
if(isset($_SESSION['user_id']))
{
    $elements_arr = $_SESSION['user_id'];
    if(isset($elements_arr[$elementID]))
        {
            $no_of_items = $elements_arr[$elementID];
            $no_of_items = (int)$no_of_items + 1 ;
            $elements_arr[$elementID] = $no_of_items ;
        }
        else
            $elements_arr[$elementID] = 1;
    $_SESSION['user_id'] = $elements_arr;
}
else
{
    $elements_arr[$elementID] = 1;
    $_SESSION['user_id'] = $elements_arr;
}
 
 
 
 
$keys = array_keys($elements_arr);
for($i = 0 ; $i < count($keys) ; $i++)
 
{
    print("<div id='div'".$keys[$i].">");
    print("&nbsp;<img src='images/point.png' style='margin-top:5px;' width=20px height= 20px>&nbsp;");
    print("<label class='ajax_item'>".$keys[$i]."  (".$elements_arr[$keys[$i]].")</label>&nbsp;");
    print("&nbsp;<img src='images/DeleteIcon.gif' class='delete_img' onclick=(removeItem('".$keys[$i]."'));>");
    print("<br>");
    print("<div id='split_line'></div>");
    print("</div>");
}
?>  
 
Should the if statment go just before the open curly bracket for the print lines?

The main bit i don't understand is in the line:

Code: Select all

 
print("<label class='ajax_item'>".$keys[$i]."  (".$elements_arr[$keys[$i]].")</label>&nbsp;");
 
how it is calling the class/emement id and how to add the price from my if statement into that.

Many thanks

Jibber4568

Re: Php shop help

Posted: Mon Apr 28, 2008 12:20 pm
by jibber4568
Ok, ive kinda managed to do what i want but obviously as other items get added to the cart the previous items price is updated to match the new one.

What would be the best way for me to work round this.

Cheers

Code: Select all

 
<?php
session_start();
$elements_arr;
$elementID = $_GET['id'];
if(isset($_SESSION['user_id']))
{
    $elements_arr = $_SESSION['user_id'];
    if(isset($elements_arr[$elementID]))
        {
            $no_of_items = $elements_arr[$elementID];
            $no_of_items = (int)$no_of_items + 1 ;
            $elements_arr[$elementID] = $no_of_items ;
        }
        else
            $elements_arr[$elementID] = 1;
    $_SESSION['user_id'] = $elements_arr;
}
else
{
    $elements_arr[$elementID] = 1;
    $_SESSION['user_id'] = $elements_arr;
}
 
 
switch ($elementID) {
case "WaxCup":
    $price=" @ £4.99";
    break;
case "KoasteCd":
    $price=" @ £3.99";
    break;
case "OlliePCd":
    $price=" @ £4.99";
    break;
case "WaxhedsCd":
    $price=" @ £4.99";
    break;
case "WaxhedsTee":
    $price=" @ £8.99";
    break;
case "SmashingCap":
    $price=" @ £5.99";
    break;
case "SmashingPillow":
    $price=" @ £6.99";
    break;
 }
 
 
$keys = array_keys($elements_arr);
for($i = 0 ; $i < count($keys) ; $i++)
 
{
    print("<div id='div'".$keys[$i].">");
    print("&nbsp;<img src='images/point.png' style='margin-top:5px;' width=20px height= 20px>&nbsp;");
    print("<label class='ajax_item'>".$keys[$i]."  (".$elements_arr[$keys[$i]].")</label>&nbsp;");
    print("&nbsp;<img src='images/delete.png' class='delete_img' onclick=(removeItem('".$keys[$i]."'));>");
    print("<br>");
    print("$price");
    print("<div id='split_line'></div>");
    print("</div>");
}
 
 
?>