Forum Moderators: coopster

Message Too Old, No Replies

Dynamic LInks

Trying to do shopping cart

         

geo039

10:25 pm on Apr 11, 2006 (gmt 0)

10+ Year Member



Okay I'm pretty new to PHP and I am trying to do a simple shopping cart. I have an add to cart link on a products page, that I need to go to a different page depending on what product is selected but for the life of me I cannot figure out how to do that. I know I can do something like <a href='addtocart$c.php'> then do a loop to send user to a page called addtocart1.php if $c=1 but I cannot figure out how to get it to work. Any advice?

<?php

print "<html>
<head><title>Database Web Application</title></head>
<body bgcolor='white'>
<center><h1>Shop at DVD R US Now</h1></center>
<hr>";

//Establish Connection with MySQL Database
$username="XX";
$password="XX";
$database="products";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("unable to select database");

$query="SELECT * FROM inventory";
$result=mysql_query($query);

$num=mysql_numrows($result);

//Display database records in HTML table
print "<table border='0' width='100%' cellpadding='0' cellspacing='0'>

<tr>
<td>

<table width='400' border='1'>";

$i=0;
while ($i < $num) {

$item_name=mysql_result($result, $i, "item_name");
$price=mysql_result($result, $i, "price");
$image_fn=mysql_result($result, $i, "image_file");

print "<tr>";
print "<td><img src='images/$image_fn'></td>";
print "<td><b><font face='arial'>DVD: $item_name</b>
<br>Cost:$ $price</font>";
print "<td><a href='addcart.php'>Add to Cart</a></td></tr>";

$i++;
}

print "</table>
</td>
</tr>
<br>
<p align='center'><font size='2'>
<a href='#'>Home</a> ¦ <a href='#'>Order Form</a> ¦ <a href='#'>Contact Us</a>
</font></p>
<hr>
</body></html>";
mysql_close();
?>

eelixduppy

10:39 pm on Apr 11, 2006 (gmt 0)



Hello....

You can create a separate .php page for the content after then select the link for the product they want to buy. Then, for each of the links for the product, put the product name at the end like this...

print "<a href='addcart.php?$prod=item_name'>Add to Cart</a>";

...then on the other page do something like this...

$selected = $_GET['prod'];

if($selected == "DVD") {
print "You selected DVD!"; }
else if($selected == "Toy") {
print "You still play with toys?"; }

You can do something like this or choose one of the other ways that this can be done using forms and such...hope this kindof helps

adni18

11:51 pm on Apr 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think eelixduppy meant something like:

print "<a href='addcart.php?prod=$itemname'>Add to Cart</a>";

...

$selected = $_GET['prod'];

:)