hello..heres my probelem
first of all i'm not a true php guy i'm just a developer with a probably stupid prolem. i have a shopping cart that i installed on a site. instead of writing the orders to a db it writes it to a flat file. everything works fine except when it wirtes the order it does not write all the items. only the last item listed in the shopping cart. it writes the totals for the other items but not the item itself. i'm more than willing to actualluy pay someone a few bucks to help me out here. below is the code for the viewart section and the check out section...
viewcart
<?php
if($submit == "Checkout")
{
header("Location: http://www.bplowestprices.com/checkout.php");
}
print "$bpcart <br>";
$cklist = preg_split("/+/",$bpcart);
if($submit == "delete items")
{
$flag = "no";
foreach($cklist as $newcookie)
{
foreach ($HTTP_POST_VARS as $var => $value)
{
if($HTTP_POST_VARS[$var] == $newcookie)
{
$flag = "yes";
}
}
if($flag != "yes")
{
if($toset == "")
{
$toset = $newcookie;
}
else
{
$toset = $toset."+".$newcookie;
}
}
$flag = "no";
}
setcookie("bpcart",$toset,time()+3600);
#print "New cookie value = $toset<br>";
print "<B><center>Item deleted from your cart successfully!</B>\n";
print "<P><a href=viewcart.php>Return to Cart</a></center>\n";
exit;
}
if($submit == "change quantities")
{
if ($bpcart != "")
{
#print "$bpcart<P>\n";
$itemlist = preg_split("/\+/",$bpcart);
$counter = 1;
foreach($itemlist as $itemstring)
{
$counter++;
foreach ($HTTP_POST_VARS as $var => $value)
{
#print "$var - $value<P>\n";
$newquan = "newqty" . $counter;
if($var == $newquan)
{
$new_quantity = $value;
}
}
$itemdata = preg_split("/\|/",$itemstring);
$newitemstring = $itemdata[0]."|".$new_quantity."|".$itemdata[2]."|".$itemdata[3];
#print "$newitemstring<P>\n";
if($tosett == "")
{
$tosett = $newitemstring;
}
else
{
$tosett = $tosett."+".$newitemstring;
}
}
setcookie("bpcart",$tosett,time()+3600);
#print "New cookie value = $tosett<br>";
print "<B><center>Item quantities changed successfully!</B>\n";
print "<P><a href=viewcart.php>Return to Cart</a></center>\n";
exit;
}
}
$fp = fopen ("pricelist.txt","r");
while ($iteminfo = fscanf ($fp, "%s\t%s\n"))
{
list ($item, $price) = $iteminfo;
#print "Item Name = $item , price = $price <br>";
$priceinfo[$item] = $price;
}
fclose($fp);
$chklist = preg_split("/+/",$bpcart);
if($bpcart != "")
{
foreach($chklist as $acookie)
{
#print "$chklist<P>";
list($name,$qty,$size,$color) = preg_split("/|/",$acookie);
$total = $priceinfo[$name] * $qty;
$networth = $networth + $total;
$totalquantity = $totalquantity + $qty;
}
print "<body link=696969 alink=dcdcdc vlink=708090><h4><center>You have $totalquantity items in your cart with a subtotal of $networth</center></h4>\n";
}
else
{
print "<h3><center>No items in the cart</center></h3>\n";
exit;
}
?>
checkout
<? /
Programmed Jorge Sanchez
This file contains both a submit form and the code to process it.
/
// Define the file name
// Important: This file must be writable by the www server
$file_name = "results/result.txt";
?>
<?
/ ********************************************************************
This section contains the code for processing the form input (see below)
It will only be executed if $insert is not 0 (e.g. if it is set by
the submit button of the form below. Else the if clause is skipped.
/
if ($insert) {
// check existance of file (or try to create it)
// a better alternative to touch() would be is_file, is_writable and so on.
$try = touch($file_name);
if (!$try) {
echo "<p>Sorry I can't open a file, something is wrong";
exit;
}
// this is the stuff we get from the form, we insert it into an array
$input = array ($item, $totaluniqueitems, $totalitems, $totalcost,$f1,$m1,$l1,$c1,$r1,$s1,$r2,$c2,$s2,$z1,$p2,$p3,$fx,$e1,$sf1,$sm1,$sl1,$sc1,$sr1,$ss1,$sr2,$sc2,$ss2,$sz1,$sp2,$fx2,$ed,$ld,$X2,$CH,$X2,$X3,);
// so we can make a big string with tabs between the elements
// note that we add a \n (line break) to the end of the string.
$output_line = implode ($input, " ")."\n";
// Now open the file (get a file pointer)
// We will append to it and therefore use the "a" option
$output_stream = fopen($file_name, "a");
// and dump the string into the file
$result = fputs ($output_stream, $output_line);
// give feedback
if ($result) {
echo "<center><h2>Your order has successfully been registered.";
}
else {
echo "<p>Too bad, the db did not want your data.";
}
// close the file pointer
fclose($output_stream);
?>
<?
// EXIT here ... we don't want to see the form again. If you do, kill the exit
exit;
}
?>
<body link="696969" aLink="dcdcdc" vLink="708090">
<div align="center">
<center>
<table width="60%" border="0" cellspacing="0" cellpadding="3">
<tr height="75">
<td align=center height="75" valign="top"><font color="black"><b>
<Form action="checkout.php" method="post">
<?php
#print "$bpcart <br>";
$cklist = preg_split("/+/",$bpcart);
$fp = fopen ("pricelist.txt","r");
while ($iteminfo = fscanf ($fp, "%s\t%s\n"))
{
list ($item, $price) = $iteminfo;
#print "Item Name = $item , price = $price <br>";
$priceinfo[$item] = $price;
}
fclose($fp);
$total = 0;
$count = 0;
$totcount = 0;
$networth = 0;
$totalquantity = 0;
if($bpcart != "")
{
$count = 1;
foreach($cklist as $acookie)
{
$count++;
$totcount++;
list($name,$qty,$size) = preg_split("/|/",$acookie);
$total = $priceinfo[$name] * $qty;
$networth = $networth + $total;
$totalquantity = $totalquantity + $qty;
#print "<tr><td bgcolor=white align=center>".$name."</td><td bgcolor=white align=center>".$qty."</td><td bgcolor=white align=center>".$size."</td>";
#print "<td bgcolor=white align=center>$ ".$priceinfo[$name]."</td>";
#print "<td bgcolor=white align=center>$ ".$total."</td>";
print "<input type='hidden' name='item' value='.$acookie.'>\n";
}
print "<input type='hidden' name='totaluniqueitems' value='.$totcount.'>\n";
print "<input type='hidden' name='totalitems' value='.$totalquantity.'>\n";
if ($networth > 150)
{
print "<input type='hidden' name='totalcost' value='.$networth.'>\n";
print "<h4><center>You have $totalquantity items in your cart for a grand total of $networth</center></h4>\n";
}
else
{
$grand_total = $networth + 8.95;
print "<input type='hidden' name='totalcost' value='.$grand_total.'>\n";
print "<h4><center>You have $totalquantity items in your cart for a subtotal of $networth, plus 8.95 for shipping brings your grand total to $grand_total</center></h4>\n";
}
}
else
{
print "<h3><center>No items in your cart</center></h3>\n";
exit;
}
#print "$count - $networth";
?>