|
Posted by Mayo on 10/20/38 11:16
Yikes,
Who would have thought something so easy would be such a pain in the
butt.
I solved it with embedded if-else clauses.
Thx all
-----Original Message-----
From: mayo [mailto:mayo@nycinteractive.com]
Sent: Tuesday, May 17, 2005 9:19 AM
To: 'php'
Subject: [PHP] array, trouble updating
I usually work with cold fusion and took on a little project to get my
feet wet in php and am spinning my wheels. What I thought might be
difficult was easy and what I thought would be a piece of cake has
caused me much grief over the last few days.
I'm making a little shopping basket, writing the results to a file,
ftping the file to a distributor and sending the CC data to merchant
services company.
Write to file - easy
FTP file to distributor - easy
Updating the array in the shopping basket -- not so easy !!??!!
A shopping basket should be able to add and delete items (done)
You should be able to delete the whole basket (done)
Now I need to be able to update quantity - I've spent at least 5 or 6
hours on this and have gotten nowhere!!! This learning exercise is
getting to be a major time drag.
Any hints would be good.
The code is below:
<?php
if ($action == "empty")
{
while ($ses_basket_items > -1)
{
array_splice ($ses_basket_name,
$ses_basket_items, 1);
array_splice ($ses_basket_amount,
$ses_basket_items, 1);
array_splice ($ses_basket_price,
$ses_basket_items, 1);
array_splice ($ses_basket_id,
$ses_basket_items, 1);
$ses_basket_items--;
}
}
if ($action2 == "deleteItem")
{
array_splice ($ses_basket_name, $position, 1);
array_splice ($ses_basket_amount, $position, 1);
array_splice ($ses_basket_price, $position, 1);
array_splice ($ses_basket_id, $position, 1);
$ses_basket_items--;
}
?>
<?php
if ($_GET['basket']!=""){
if (session_is_registered('ses_basket_items')){
$basket_position_counter=0;
$double=0;
if ($_SESSION['ses_basket_items']>0){
foreach ($_SESSION['ses_basket_name'] as $basket_item){
if ($basket_item==$_GET['basket']){
$double=1;
$subtract=1;
$basket_position=$basket_position_counter;
}
$basket_position_counter++;
}
}
if ($double==1){
$oldamount=$_SESSION['ses_basket_amount'][$basket_position];
$_SESSION['ses_basket_amount'][$basket_position]++;
$amount=$_SESSION['ses_basket_amount'][$basket_position];
$oldprice=$_SESSION['ses_basket_price'][$basket_position];
$newprice=($oldprice/$oldamount)*$amount;
$_SESSION['ses_basket_price'][$basket_position]=$newprice;
}else{
$_SESSION['ses_basket_name'][]=$_GET['basket'];
$_SESSION['ses_basket_amount'][]=1;
$_SESSION['ses_basket_price'][]=$_GET['price'];
$_SESSION['ses_basket_id'][]=$_GET['id'];
$_SESSION['ses_basket_items']++;
}
}else{
$_SESSION['ses_basket_items']=1;
$_SESSION['ses_basket_name'][0]=$_GET['basket'];
$_SESSION['ses_basket_amount'][0]=1;
$_SESSION['ses_basket_price'][0]=$_GET['price'];
$_SESSION['ses_basket_id'][0]=$_GET['id'];
session_register("ses_basket_items");
session_register("ses_basket_name");
session_register("ses_basket_amount");
session_register("ses_basket_price");
session_register("ses_basket_id");
}
}
if ($_SESSION['ses_basket_items']>0){
for
($basket_counter=0;$basket_counter<$_SESSION['ses_basket_items'];$basket
_counter++){
// basket output
$price=sprintf("%01.2f",$_SESSION['ses_basket_price'][$basket_counter]);
$amount=$_SESSION['ses_basket_amount'][$basket_counter];
$name=$_SESSION['ses_basket_name'][$basket_counter];
$aaa=$basket_counter;
echo "$amount $name $price";
echo "<a href=\"2.php?action2=deleteItem&position=" . $aaa .
"\">DEL</a>";
echo "<a
href=\"2.php?id=1001&price=25&basket=mouse&subtract=yes\">-sub</a>";
echo "$aaa";
echo "<BR>\n";
}
} else {
$_SESSION['ses_basket_items']=0;
unset($_SESSION['ses_basket_name']);
unset($_SESSION['ses_basket_amount']);
unset($_SESSION['ses_basket_price']);
unset($_SESSION['ses_basket_id']);
}
?>
Navigation:
[Reply to this message]
|