By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

Shopping Cart

ak1dnar
Expert 100+
P: 1,584
Hi,
I am Building small shopping cart that enables customers to select books from the list and Add it to the Cart.

When i click Add Button the Cart Should Update with these things.

No of Books in the Cart.
Book Name And Quantity

I'll Submit the Entire Coding that i have currently.

My Problem is I cant Update the No of Books and Book name along with Qty.
Can anyone provide some help.
Is there any other way to Do this.

products.php
[PHP]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<p><?php include('cart.php'); ?></p>
<p>
<?php include('result.php'); ?>

</p>
</body>
</html>
[/PHP]
result.php
[PHP]<?php
if ($_GET['cat'])
{$cat = $_GET['cat'];}
else
{$cat = "kids";}
require 'dbcon.php';
$sql="SELECT * FROM products GROUP BY p_id ORDER BY p_id asc";
$result=mysql_query($sql) or die("Error Occured while Searching Records : " . mysql_error());
$num=mysql_num_rows($result);
if (mysql_num_rows($result) == 0)
{
echo '<table width="700" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top" class="text">No Maching Result Found<br></td>

</tr></table>';
}
else
{

echo '<table width="550" border="0" cellspacing="0" cellpadding="0">';
$colsPerRow = 3;
// width of each column in percent
$colWidth = (int)(100/$colsPerRow);
$i = 0;
while ($row = mysql_fetch_assoc($result))
{
if ($i % $colsPerRow == 0)
{
// start a new row
echo '<tr>';
}
echo
'<td align="center">

<img src="images_products/'.$row['p_image'].'" width="154" height="154" border="0" alt="'.$row['p_image'].'"/><br>
<form action="'.$PHP_SELF .'" method="post">
<input name="pid" type="submit" value="ADD"/>
<input name="pid" type="hidden" value="'.$row['p_id'].'"/>
</form>
</td>';

if ($i % $colsPerRow == $colsPerRow - 1)
{
echo '</tr><td align="center" valign="top">&nbsp;</td>';
}
$i += 1;
}
// print blank columns
if ($i % $colsPerRow != 0)
{
while ($i++ % $colsPerRow != 0)
{
echo '<td width="' . $colWidth . '%">&nbsp;</td>';
}
echo '</tr>';
}

echo '</table>';
}

?>[/PHP]
dbcon.php
[PHP]<?
$con = mysql_connect('localhost', 'root', 'dba') or die ("Could not connect to the Database");
mysql_select_db('test', $con) or die (mysql_error());
?>[/PHP]
cart.php
[PHP]<?php $pid = $_POST['pid'];

if($pid){

$Items = $pid;

}else{
$Items = "No Items";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<a href="view_cart.php">Your Cart<br />
</a>
<?php echo $Items;?>
</body>
</html>[/PHP]

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `products` (
  2.   `p_id` int(10) NOT NULL auto_increment,
  3.   `p_name` varchar(25) NOT NULL,
  4.   `p_image` varchar(15) NOT NULL,
  5.   PRIMARY KEY  (`p_id`)
  6. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1005 ;
  7.  
  8. INSERT INTO `products` VALUES (1001, 'PHP MySQL Starter Kit', '1001.jpg');
  9. INSERT INTO `products` VALUES (1002, 'ASP.Net Beginers Guide', '1002.jpg');
  10. INSERT INTO `products` VALUES (1003, 'Learn Ajax', '1003.jpg');
  11. INSERT INTO `products` VALUES (1004, 'Ajax Bible', '1004.jpg');
  12.  
  13.  
Mar 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: 99
result.php
[PHP]<?php

<form action="'.$PHP_SELF .'" method="post">
<input name="pid" type="submit" value="ADD"/>
<input name="pid" type="hidden" value="'.$row['p_id'].'"/>
</form>
[/PHP]
You have two inputs called pid, i think that is the problem. The easy way to do a tablebased item submit is using the button tag

[HTML]<button type="submit" name="pid" value="'.$row['p_id'].'">ADD</button>[/HTML]

This way you don't have to use hidden fields and you can put the table in one form instead of as many forms as there are items.
Mar 2 '07 #2

ak1dnar
Expert 100+
P: 1,584
You have two inputs called pid, i think that is the problem. The easy way to do a tablebased item submit is using the button tag

[HTML]<button type="submit" name="pid" value="'.$row['p_id'].'">ADD</button>[/HTML]

This way you don't have to use hidden fields and you can put the table in one form instead of as many forms as there are items.
In my Original Application I am using a Image Button.Thats what I am using a Hidden field. By removing it doesn't make a Answer to my Question.

All i need to Update my Shopping Cart for each and Every Add.

Currently I can send Just only the Item that I am selecting from the List.
But what i need I want to Display,

1001 x 1 items
1003 x 3 items

Like this as a Shopping Cart.

Thanks for reading my POST.
Mar 4 '07 #3

P: 99
In my Original Application I am using a Image Button.Thats what I am using a Hidden field. By removing it doesn't make a Answer to my Question.

All i need to Update my Shopping Cart for each and Every Add.

Currently I can send Just only the Item that I am selecting from the List.
But what i need I want to Display,

1001 x 1 items
1003 x 3 items

Like this as a Shopping Cart.

Thanks for reading my POST.
I understand now but then you need to have only one form for that to work

[PHP]
<?php
if(isset($_POST['send'])){
foreach($_POST['send'] as $buttonvalue){
if(is_numeric($_POST['quantity_'.$buttonvalue]) && $_POST['quantity_'.$buttonvalue] > 0){
echo $_POST['quantity_'.$buttonvalue].' x '.$buttonvalue.'<br>';
// database code and count subtotals and total
}
}
}
?>

<form action="'.$PHP_SELF .'" method="post">
<!-- build the items table -->
<!-- this is an example how to output a row -->
<td><input type="text" name="quantity_<?php echo $row['p_id']; ?>" value="0"></td>
<td><button type="submit" name="send[]" value="<?php echo $row['p_id']; ?>">image if needed</button></td>
</form>
[/PHP]

this should do the trick.
Mar 4 '07 #4

ak1dnar
Expert 100+
P: 1,584
Please Run this Script After Creating MySQL Table in my first post.
I cant update my CART for each and every click.

If I am selecting 1001.jpg in the cart I can display
1 x 1001
But when i click it again I want to Display
2 x 1001

Like wise if there is Multiple selection That also should Update.

Eg:

2 x 1001
1 x 1004
2 x 1003

I am stucked with this pls help.

[PHP]<?php
// CART VALUES
if(isset($_POST['send']))
{

foreach($_POST['send'] as $buttonvalue)
{

if(is_numeric($_POST['quantity_'.$buttonvalue]))
{

echo $_POST['quantity_'.$buttonvalue].' x '.$buttonvalue.'<br>';

// database code and count subtotals and total

}

}

}
//END CART

// DISPLAY THE TABLE DATA
if ($_GET['cat'])
{$cat = $_GET['cat'];}
else
{$cat = "kids";}
require 'dbcon.php';
$sql="SELECT * FROM products GROUP BY p_id ORDER BY p_id asc";
$result=mysql_query($sql) or die("Error Occured while Searching Records : " . mysql_error());
$num=mysql_num_rows($result);
if (mysql_num_rows($result) == 0)
{
echo '<table width="700" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top" class="text">No Maching Result Found<br></td>

</tr></table>';
}
else
{

echo '<table width="550" border="0" cellspacing="0" cellpadding="0">';
$colsPerRow = 3;
// width of each column in percent
$colWidth = (int)(100/$colsPerRow);
$i = 0;
while ($row = mysql_fetch_assoc($result))
{
if ($i % $colsPerRow == 0)
{
// start a new row
echo '<tr>';
}
echo
'<td align="center">

<img src="images_products/'.$row['p_image'].'" width="154" height="154" border="0" alt="'.$row['p_image'].'"/><br>
<form action="'.$PHP_SELF .'" method="post">
<input type="hidden" name="quantity_'.$row['p_id'].'" value="1">
<input type="submit" name="send[]" value="ADD"/>
<input type="hidden" name="send[]" value="'.$row['p_id'].'"/>
</form>
</td>';

if ($i % $colsPerRow == $colsPerRow - 1)
{
echo '</tr><td align="center" valign="top">&nbsp;</td>';
}
$i += 1;
}
// print blank columns
if ($i % $colsPerRow != 0)
{
while ($i++ % $colsPerRow != 0)
{
echo '<td width="' . $colWidth . '%">&nbsp;</td>';
}
echo '</tr>';
}

echo '</table>';
}

?>[/PHP]
Mar 4 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.