Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

my first shopping cart

Status
Not open for further replies.

ThomasJSmart

Programmer
Joined
Sep 16, 2002
Messages
634
hi ppl iv made this very simple shooping cart prototype, its my first cart :) remarks and comments are welcome.

Code:
<?
session_start();
if (!isset($_SESSION['products'])){$_SESSION['products'] = &quot;&quot;;}

if (isset($_GET['add'])){$_SESSION['products'] = $_SESSION['products'].&quot;.&quot;.$_GET['add']; echo &quot;<body onLoad=\&quot;window.location='index.php'\&quot;>&quot;;}


// Database Connect function
function connectdb()
{
global $db;


$db = mysql_connect(&quot;localhost&quot;, &quot;******&quot;,&quot;*******&quot;);
mysql_select_db(&quot;webshop&quot;,$db);
}

// get items from database
    connectdb();
    $result = mysql_query(&quot;SELECT * from items&quot;,$db);

    echo &quot;Items in database:<br><br>&quot;;
			
    while ($myrow = mysql_fetch_array($result))
	{
	$id 	= $myrow['id'];
	$cat 	= $myrow['cat'];
	$title 	= $myrow['title'];
	$info 	= $myrow['info'];
	$cost 	= $myrow['cost'];

	 echo &quot;ID: $id<br>Cat: $cat<br><a href='index.php?add=$id'>Title: $title</a><br>Info: $info<br>Cost: $cost<br><br>&quot;;

	} // end get results loop
	
    mysql_close();
	
    $itemarr = explode(&quot;.&quot;,$_SESSION['products']);

    $itemarr = array_slice ($itemarr, 1);

    $acv   = array_count_values ($itemarr);
    $ak	   = array_keys ($acv);
    $totp  = count($ak);

    $totsub = 0;

if ($totp > 0)
	{
	echo &quot;Items u have selected:<br><br><table width='400'>&quot;;
	for ($n = 0; $n < $totp; $n++)
	    {
		$thisid = $ak[$n];
		
		connectdb();
		$result = mysql_query(&quot;SELECT * from items WHERE id='$thisid'&quot;,$db);
		while ($myrow = mysql_fetch_array($result))
		     {
			$title 	= $myrow['title'];
			$cost 	= $myrow['cost'];
			
                        $thiscost = $cost*($acv[$n+1]);
			$totsub += $thiscost;
						
		        echo &quot;<tr><td>$title</td><td>x&quot;.$acv[$n+1].&quot;</td><td> E $cost</td></tr>&quot;;
						
		     } // end get results loop
		mysql_close();
    }
		
    echo &quot;<tr><td></td><td>Subtotal:</td><td> E $totsub&quot;;
    $btw = round(($totsub / 100)*19);
    echo &quot;<tr><td></td><td>BTW:</td><td> E $btw&quot;;
    $totcos = $totsub + $btw;
    echo &quot;<tr><td></td><td>BTW:</td><td> E $totcos</td></tr></table>&quot;;
}

?>

there is an online working version here:



comments are welcome,
Thomas



I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
one fairly large error or flaw in the script.

scenario:
user adds a item :
user clicks the back button :
HEY! What @!^%$ is going on.

solution:
well, I haven't gone through it yet to see why the URL is clearing the querystring out yet, but thats the problem. everytime you hit the back button it adds the users last event being the add and then your stuck adding, and adding....

I actually saw this on a cart once (live) and heard it's a common error, in not catching the back button (history) actions that occur by the user.

On the first cart! good job. better then I could have done on my first go. [smile]

____________________________________________________
The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811

onpnt2.gif
 
So how can i solve the back problem? thing is if i dont use the window.location trick the product will be added again if the user refreshes the page.

thanks,


I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top