根据PHP中的GET id显示不同的SESSION购物车结果

时间:2013-12-03 01:50:07

标签: php mysql get cart

我有一个网站,其中登录的用户可以查看他们从'sales_list'表中进行的销售。 'sales_id'是主键。的(showsales.php)

从此列表中,用户可以向特定销售添加附件,这将从数据库上的“附件”表中检索附件列表,并保留sales_id(sid),'accessories_id'是主要密钥。这张桌子。 (add_accessories_sales.php)添加/查看配件,(add_accessories_cart.php)添加到购物车

从此列表中,附件将添加到购物车(cart_a)中,仍然位于原始sid下方,他们可以在 view_accessories_cart.php

下查看购物车

我想要做的是不工作,那就是将购物车商品限制为特定销售下的配件,或者'sales_id'/ sid

例如,如果从sales_list表中检索到的销售号码id为“17”,那么我只希望能够看到限制在sales_id值为“17”的购物车项目,如果他们点击“view_accessories_cart.php? = sid“链接

从下面的代码中,我可以让购物车运行(并打印sales_id),但它会显示所有配件,而不是sales_id = '17'下的配件。

有人可以帮忙吗?或者有更好的方法吗?所有代码栏view_accessories_cart.php工作正常......

view_accessories_cart.php

<?php session_start();//view_accessories_cart.php

$page_title = 'View Accessories Cart';
include ('../includes/header-sc.html');

if (!isset($_SESSION['users_id'])) {

   $url = 'http://' . $_SERVER['HTTP_HOST']
    . dirname($_SERVER['PHP_SELF']);
   if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
        $url = substr ($url, 0, -1); // Chop off the slash.
   }
   $url .= '../../login.php'; 

ob_end_clean(); 
header("Location: $url"); 
exit(); 
}

$sid = $_GET['sid'];

$problem = FALSE; 

if (isset($_POST['submitted']))
   { 
foreach ($_POST['qty'] as $k => $v) {

$aid = (int) $k;
$qty = (int) $v;

if ( $qty == 0 ) { 
unset ($_SESSION['cart_a'][$aid]);
} elseif ( $qty > 0 ) { 
$_SESSION['cart_a'][$aid] ['quantity'] = $qty;
}
} 
} 

$empty = TRUE;
if (isset ($_SESSION['cart_a'])) {
foreach ($_SESSION['cart_a'] as $key =>$value) {
if (isset($value)) {
$empty = FALSE;
break; 
}
}
}

if (!$empty) {
  require_once ('database.php'); 

$query = "SELECT sales_id FROM sales_list
            WHERE sales_id=$sid";
$result = mysql_query($query);  

$query = "SELECT accessories.accessories_id, accessories.accessories_name, accessories_description FROM accessories
   WHERE accessories.accessories_id 
   IN (";foreach ($_SESSION['cart_a'] as $aid =>$value) {
$query .= $aid . ',';
}
$query = substr ($query, 0, -1) . ') ORDER BY accessories.accessories_name ASC';
$result = mysql_query($query);
?>

<h1>Performance </h1>

<?php
echo '
<table border="0" width="100%" cellspacing="1" cellpadding="5"
   align="center">
<tr class="top">
<td align="left"><b>ID</b></td>
<td align="left"><b>Accessory</b></td>
<td align="right"><b>Price</b></td>
<td align="center"><b>Qty</b></td>
<td align="right"><b>Sub Total</b></td>
</tr>
<form action="view_accessories_cart.php" method="post">
';

$total = 0; 

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$subtotal = $_SESSION['cart_a'][$row
   ['accessories_id']]['quantity'] *
   $_SESSION['cart_a'][$row ['accessories_id']]['accessories_price'];
$total += $subtotal;

echo " <tr><td align=\"left\"> " . $sid . " </td>
<td align=\"left\">{$row['accessories_name']}</td>
<td align=\"right\">{$_SESSION['cart_a'][$row['accessories_id']] ['accessories_price']} pts</td>
<td align=\"center\"><input type=\"text\" size=\"3\"
   name=\"qty[{$row['accessories_id']}]\"
   value=\"{$_SESSION['cart_a'][$row['accessories_id']]['quantity']}\" /></td>
<td align=\"right\">" . number_format ($subtotal) . " pts</td>
</tr>\n";
} 

mysql_close($dbc); 

$str = '<tr class="even">
<td colspan="3" align="right"><b> TOTAL:<b></td>
<td align="right"><b>' . number_format ($total) . ' pts </b></td>
</tr>
</table>
<br />
<div align="center"><input type="submit" name="submit" value="Update" />
<input type="hidden" name="submitted"value="TRUE" />
</form><br /><br /></div>
<p><a href="add_accessories_sales.php?sid='. $sid . '">Add more accessories from this sale</a></p>
<p><a href="submit_cart_a.php">Submit</a></p>
'; 

} else {
echo '<div id="maincontent_left"><h1>Accessory Sales</h1><p>Your accessory sales list is currently empty.</p>
<p><a href="sc-sales.php">Go to sales page</a></p>
 ';
}

include ('../includes/footer-sc.html');
?>

add_accessories_cart.php

<?php //add_accessories_cart.php

$page_title = 'Add to Accessories Cart';
include ('../includes/header-sc.html');

if (isset ($_GET['aid'])) { 

$sid = (int) $_GET['sid'];
$aid = (int) $_GET['aid'];
$rwp = $_SESSION['reward_user_points'];

if (isset($_SESSION['cart_a'][$aid])) {

$_SESSION['cart_a'][$aid]['quantity']++; 

echo '<div id="maincontent_left"><h1>Added to Accessories Sales List</h1><p>This product is already in your shopping cart, another quantity has been added to it.</p>
<p><a href="view_accessories_cart.php">View your accessory sales list</a></p>
<p><a href="sc-sales.php">Go back to the sales page</a></p>';

} else { 

  require_once ('database.php');// Connect to the db.

$query = "SELECT accessories_price FROM accessories WHERE accessories.accessories_id = $aid";
$result = mysql_query ($query);

if (mysql_num_rows($result) == 1){ 

list($accessories_price) = mysql_fetch_array ($result, MYSQL_NUM);

$_SESSION['cart_a'][$aid] = array ('quantity' => 1, 'accessories_price' => $accessories_price);

echo '<div id="maincontent_left"><h1>Added to Accessories Sales List</h1><p> ' . $sid . ' und  ' . $aid . ' The product has been added to your shopping cart.</p><p><a href="view_accessories_cart.php?sid=' . $sid . '  ">View your accessory sales list</a></p>
<p><a href="sc-sales.php"><img src="images/but_continue.png" /></a></p>';

} else { 
echo '<div align="center">This page has been accessed in error! </div>';
}

mysql_close($dbc);

} 

} else { 
echo '<div id="maincontent_left"><h1>Error</h1><p>This page has been accessed in error!</p>';
}

include ('../includes/footer-sc.html');
?>

add_accessories_sales.php

<?php // add_accessories_sales.php
require_once ('../includes/config.inc.php');

$page_title = 'Add Accessory Sales';
include ('../includes/header-sc.html');

if (!isset($_SESSION['users_id'])) {

$url = 'http://' . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); 
}
$url .= '../../login.php'; 

ob_end_clean(); 
header("Location: $url"); 
exit();
}
if ( (isset($_GET['sid'])) && (is_numeric($_GET['sid'])) )
{  
$sid = $_GET['sid'];

} elseif ( (isset($_POST['sid'])) && (is_numeric($_POST['sid'])) )
{    
} else { 
echo '<h1>Page Error</h1><p class="error">This page has been accessed in error.</p>';
include ('../includes/footer-sc.html'); 
exit();}
?>

<h1>Add Accessories</h1>

<?php echo "<a href=\"view_accessories_cart.php?sid=$sid\">View accessory sales list for $sid</a>"; ?>

<?php require_once ('database.php'); // Connect to the database.

$query = "SELECT accessories.accessories_id, accessories.accessories_name, accessories.accessories_description , accessories.accessories_price FROM accessories";

$result = @mysql_query ($query); 

echo '<p>Add to Sales VIN Number 
<table width="610" cellspacing="1" cellpadding="5" style="font-size:11px;">
<tr class="orangehead">
<td align="center">Part No.</td>
<td align="center">Description</td>
<td align="center">RRP</td>
<td align="center"></td>
</tr>';

$bg = '#ffffff'; 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#e1e3e6' ? '#cdcdcf' : '#e1e3e6');  
echo '<tr bgcolor="' . $bg . '">';
echo  '<td align="center">' . $row['accessories_name'] . '</td>';
echo  '<td align="center">' . $row['accessories_description'] . ' </td>';
echo  '<td align="center">' . $row['accessories_price'] . '</td>';
echo  '<td align="center"><a href="add_accessories_cart.php?sid=' . $sid . '&aid=' . $row['accessories_id'] . '">Add</a></td>';}
echo '</table>';

mysql_free_result ($result);

mysql_close(); 

include ('../includes/footer-sc.html'); ?>

showsales.php (我已经摆脱了分页并登录了用户会话,因为代码有点长)

<?php  require_once ('database.php'); // Connect to the database.

$display = 100;

$query = "SELECT `sales_id`, `users_sales_guild_id` , `users_dealer_code_id` , `sales_customer_firstname` , `sales_customer_surname` , `sales_customer_company` , `sales_vin` , `sales_commission_no` , `sales_model` ,  `sales_vehicle_class` , `sales_vehicle_reg` , `sales_rda` , `sales_active` , `sales_rrp` , `sales_quantity` , `sales_points` ,  DATE_FORMAT(`sales_entry_date`, '%d-%m-%Y') AS `dr` FROM `sales_list` WHERE `users_sales_guild_id` = '".$_SESSION['users_sales_guild_id']."' 
ORDER BY ".$order_by." LIMIT ".$start.", ".$display;

$result = @mysql_query ($query);

echo '<table width="610" cellspacing="1" cellpadding="5" style="font-size:11px;"><tr>
<td align="center">Model Description</td>
<td align="center">Customer Name</td>
<td align="center">VIN</td>
<td align="center">RDA</td>
<td align="center">RRP</td>
<td align="center">Status</td>
</tr>
';

$bg = '#ffffff'; 
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$bg = ($bg=='#e1e3e6' ? '#cdcdcf' : '#e1e3e6'); 
echo '<tr bgcolor="' . $bg . '">';
echo  '<td align="center">' . $row['sales_model'] . '</td>';
echo  '<td align="center">' . $row['sales_customer_firstname'] . ' ' . $row['sales_customer_surname'] . '</td>';
echo  '<td align="center">' . $row['sales_vin'] . '</td>';
echo  '<td align="center">' . $row['sales_rda'] . '</td>';
echo  '<td align="center">' . $row['sales_rrp'] . '</td>';
echo  '<td align="center"><a href="add_accessories_sales.php?sid=' . $row['sales_id'] . '">Add</td>';   
}

echo '</table>';

mysql_free_result ($result); 

mysql_close(); 

include ('../includes/footer-sc.html'); ?>

0 个答案:

没有答案