473,498 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Shopping Cart

ak1dnar
1,584 Recognized Expert Top Contributor
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
4 1569
xwero
99 New Member
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
1,584 Recognized Expert Top Contributor
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
xwero
99 New Member
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
1,584 Recognized Expert Top Contributor
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

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

Similar topics

2
2842
by: Don Grover | last post by:
I am retrieving costs and product id's from a sql db. and need to build a shopping cart around it. How do I store the selected items and qty req so I can move into another catalog and total up as...
2
3283
by: Paul Bruneau | last post by:
Hi, I hope someone can help me make a working shopping cart, as a learning tool. If I have a "Product Demo" html page with a "Buy Me" button, there must be a simple javascript method of...
1
3561
by: madison | last post by:
Hi, I am trying to start a website using paypals shopping cart function. If i have 10 items and they sell out, how do I make it so the item is then listed as sold out. The next person would not...
1
1792
by: Jia Sun | last post by:
hello , everybody , i need a similar program , just like fancyimport.com if possible, pls contact me ,thank you very much . inchina@gmail.com
1
3197
by: Adil Akram | last post by:
I have created a site shopping cart in ASP.net. I am using ASP session object's SessionID on non SSL connection to track session. While adding products to cart DB I insert product and SessionID...
2
2281
by: G.E.M.P | last post by:
High Level Session Handling Design for a Shopping cart 0) What am I missing? 1) How does OSCommerce do it? I'm thinking about building a shopping cart from scratch, using a library of dynamic...
7
2617
by: isaac2004 | last post by:
hi i have a basic asp page that acts as an online bookstore. on my cart page i am having trouble generating 3 numbers; a subtotal, a shipping total, and a final price. here is my code i would...
1
7259
by: jecha | last post by:
I'm implementing a shopping cart but am having a problem in checking out a person who has added item in his/her shopping busket.The code for the checkout.php script is given below <?...
15
4257
gregerly
by: gregerly | last post by:
Hello, I once again turn to this community of genius' for some help with a problem. I've got a shopping cart (which is working well so far) that I will be implementing all kinds of AJAX...
3
2878
by: Paulo | last post by:
Hi, beginner on asp.net 2.0 C# VS 2005, how can I use the shopping cart concept on my application? When the user clicks add item, it will be stored on some storage format, I dont know what is the...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.