Jump to content

php cart with size and unit code


subhrajit

Recommended Posts

Hi friends,

 

I am new to php and and i have design a web site.Now Everything is ready but only one error in shopping cart for which i really need ur help.

 

Actuallt in my cart.php page there are 3 text box for a Product name. Size, Qunatity and Unit.Now if i eneter only size and update the cart then its working but if i will enter the Quantity and unit then its shows the errorr "Warning: extract() [function.extract]: First argument should be an array in C:\www\Parmadar\inc\functions.inc.php on line 61"

 

Can u pls pls pls  help me in this matter.

 

Here my cart.php page

###################

<?php

// Include MySQL class

require_once('inc/mysql.class.php');

// Include database connection

require_once('inc/global.inc.php');

// Include functions

require_once('inc/functions.inc.php');

// Start the session

session_start();

// Process actions

$cart = $_SESSION['cart'];

$action = $_GET['action'];

$id = $_GET['pname'];

$sizecategories = $_GET['sizecategories'];

$sizecategories = $_SESSION['sizecategories'];

$unitmeasurement = $_GET['unitmeasurement'];

$unitmeasurement = $_SESSION['unitmeasurement'];

 

$_SESSION['select']="$select";

session_register("$select");

$select = $_SESSION['select'];

    echo "$sizecategories";

 

//$cat_ID = $_GET['cat_ID'];

//$select = $_POST['select'];

//$_SESSION['select']="$select";

//session_register("$select");

//$select11 = $_SESSION['select'];

//$mySize['select']=$select;

// $_SESSION['select1'] = $_SESSION['select1']+ 1;

//$_SESSION['cat_ID']="$cat_ID";

//session_register("$cat_ID");

 

switch ($action) {

case 'add':

if ($cart) {

$cart .= ','.$_GET['id'];

} else {

$cart = $_GET['id'];

}

break;

case 'delete':

if ($cart) {

$items = explode(',',$cart);

$newcart = '';

foreach ($items as $item) {

if ($_GET['id'] != $item) {

if ($newcart != '') {

$newcart .= ','.$item;

} else {

$newcart = $item;

}

}

}

$cart = $newcart;

}

break;

case 'update':

if ($cart) {

$newcart = '';

foreach ($_POST as $key=>$value)  {

if (stristr($key,'qty'))  {

$id = str_replace('qty','',$key);

$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);

$newcart = '';

foreach ($items as $item) {

if ($id != $item ) {

if ($newcart != '') {

$newcart .= ','.$item;

} else {

$newcart = $item;

}

}

}

for ($i=1;$i<=$value;$i++) {

if ($newcart != '') {

$newcart .= ','.$id;

 

} else {

$newcart = $id;

}

}

}

}

}

$cart = $newcart;

break;

}

$_SESSION['cart'] = $cart;

//$_SESSION['cat_ID'] = $cat_ID;

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>www.parmadar.com</title>

<link rel="shortcut icon" href="images/favicon.ico" >

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<SCRIPT language="JavaScript">

function OnSubmitForm(val)

{

 

if(val == 'Quote')

{

document.cart.method="POST";

document.cart.action ="checkout.php?action=Quote&id=cart";

document.cart.submit();

 

}

else

if(document.pressed == 'Back')

{

document.cart.action ="product.php";

}

return true;

}

</SCRIPT>

<script language="javascript">

var message="Copyright@2008, Parmadar Pte Ltd. All rights reserved";

function clickIE4(){

if (event.button==2){

alert(message);

return false;

}

}

function clickNS4(e){

if (document.layers||document.getElementById&&!document.all){

if (e.which==2||e.which==3){

alert(message);

return false;

}

}

}

if (document.layers){

document.captureEvents(Event.MOUSEDOWN);

document.onmousedown=clickNS4;

}

else if (document.all&&!document.getElementById){

document.onmousedown=clickIE4;

}

document.oncontextmenu=new Function("alert(message);return false;")

</script>

<style type="text/css">

<!--

td {

text-decoration: none;

border: none;

}

img {

text-decoration: none;

border: none;

}

font {

font-family: Verdana;

font-size: 9px;

color: #FFFFFF;

text-decoration: none;

border: none;

}

a {

font-family: Verdana;

color: #0000FF;

text-decoration: none;

border: none;

}

a.four:link {color: #ffffff}

a.four:visited {color:#ffffff}

a.four:hover {color: #7FFFD4}

a{font:"Times New Roman", Times, serif

}

a{font-size:10px}

a{text-decoration: none}

.bodytxt

{

font-family: Verdana;

font-size: 11px;

color: #666666;

text-decoration: none;

}

.style14 {

color: #FFAC00;

font-weight: bold;

}

 

-->

 

</style>

</head>

 

<body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0" >

<table width="782" height="18" border="0" align="center" cellpadding="0" cellspacing="0">

 

<tr>

<td width="782" height="18"><?php include "include/header.php" ?> </td>

</tr>

</table>

<div align="center">

<table width="781" border="0" cellspacing="0" cellpadding="0">

<tr>

<td>

 

<table cellSpacing="0" cellPadding="0" width="780" border="0">

<tr>

<td class="blue18b" align="left" height="32" background="images/products.jpg"></td>

</tr>

<tr>

<td align="left"> <div align="center">

<table cellSpacing="1" cellPadding="1" width="100%" border="1" bordercolor="#84a2c6">

<tr>

<td width="1%" height="424" align="middle" bgColor="#ffffff" bordercolor="#FFFFFF" class="bodytxt"> <div align="justify"><br>

<br>

</div></td>

<td width="99%" align="middle" valign="top" bgColor="#ffffff" class="bodytxt"><div align="justify">

<br><br>

 

<table width="397" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#84a2c6" >

 

 

<tr bgcolor="#FFFFFF">

<td width="393" height="54" bordercolor="#FFFFFF" >

 

<?php

echo showCart();

?>

 

</td >

</tr>

 

<tr bgcolor="#FFFFFF">

<td bgcolor="#FFFFFF" colspan="3" align="center" height="23">

<FORM name="myform" onSubmit="return OnSubmitForm()">

 

<INPUT TYPE="button" name="Operation" onClick="OnSubmitForm(this.value);" VALUE="Quote">

<INPUT TYPE="SUBMIT" name="Operation" onClick="document.pressed=this.value" VALUE="Back">

</form>

<!--</FORM>-->

</td>

</tr>

 

<tr bgcolor="#FFFFFF">

<td height="13" bordercolor="#FFFFFF" > </td >

</tr>

</table>

 

 

 

</div></td>

<td width="0%" align="middle" bgColor="#ffffff" class="bodytxt"> </td>

</tr>

</table>

</div>

</td>

</tr>

</table>

 

 

</td>

</tr>

</table>

</div>

<table width="780" height="18" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>

<td height="18"><?php include "include/footer.php" ?>

 

</td>

</tr>

</table>

 

 

</body>

</html>

##################################

Functions.inc.php

#################

<?php

function writeShoppingCart() {

$cart = $_SESSION['cart'];

$cat_ID = $_SESSION['cat_ID'];

 

if (!$cart) {

return '<p>Sorry You Have No Items In Your Cart</p>';

} else {

// Parse the cart session variable

$items = explode(',',$cart);

$s = (count($items) > 1) ? 's':'';

return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';

}

}

 

function showCart() {

global $db;

$cart = $_SESSION['cart'];

$cat_ID = $_SESSION['cat_ID'];

 

$qty1 = $_POST["qty1"];

$_SESSION['qty1']="$qty1";

session_register("$qty1");

$qty1 = $_SESSION['qty1'];

 

$qty2 = $_POST["qty2"];

$_SESSION['qty2']="$qty2";

session_register("$qty2");

$qty2 = $_SESSION['qty2'];

 

 

if ($cart) {

$items = explode(',',$cart);

$contents = array();

foreach ($items as $item) {

$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;

}

$output[] = '<form action="cart.php?action=update" name="cart" method="post" id="cart"&qty1="qty1"&qty2="qty2">';

$output[] = '<table width="501" >';

$output[] = '<tr bgcolor="#00659C" height="25">';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>Remove</b></font></td>';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>Product Name</b></font></td>';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>Quantity</b></font></td>';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>Size</b></font></td>';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>Unit</b>   (mt/kg/unit)</font></td>';

$output[] = '</tr>';

$output[] = '<tr height="30" >';

$output[] = '<td></td>';

$output[] = '<td></td>';

$output[] = '<td></td>';

$output[] = '<td></td>';

$output[] = '<td></td>';

$output[] = '</tr>';

 

 

foreach ($contents as $id=>$qty)

{

$sql = 'SELECT * FROM products WHERE id ='.$id ;

$result = $db->query($sql);

$row = $result->fetch();

extract($row);

 

$output[] = '<tr>';

$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';

$output[] = '<td><p>'.$product_NAME.'</p></td>';

$output[] = '<td><p><input type="text" name="qty'.$id.'" value="'.$qty.'" size="10"/> </p></td>';

$output[] = '<td><p><input type="text" name="qty1" value="'.$qty1.'"  size="10"/> </p></td>';

$output[] = '<td><p><input type="text" name="qty2"  value="'.$qty2.'" size="10"/> </p></td>';

$output[] = '<tr>';

}

$output[] = '</table>';

$output[] = '<div><button type="submit">Update cart</button></div>';

//added

$output[] = '</form>';

}else {

$output[] = '<p>Sorry You Have No Items In Your Cart.</p>';

}

return join('',$output);

}

/////////////////////////////////////////////////////////////////////////////////////////////

function showCart1() {

global $db;

 

$cart = $_SESSION['cart'];

if ($cart) {

$items = explode(',',$cart);

$contents = array();

foreach ($items as $item) {

$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;

}

$output[] = '<table>';

$output[] = '<table width="501" >';

$output[] = '<tr bgcolor="#00659C" height="25">';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>SINo</b></font></td>';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>Product Name</b></font></td>';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>Quantity</b></font></td>';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>Size</b></font></td>';

$output[] = '<td><font face=\"Arial\" color=\"#FF0000\"><b>Unit</b></font></td>';

$output[] = '</tr>';

$output[] = '<tr height="5" >';

$output[] = '<td></td>';

$output[] = '<td></td>';

$output[] = '<td></td>';

$output[] = '<td></td>';

$output[] = '<td></td>';

$output[] = '</tr>';

$cnt=1;

$cnt1=1;

foreach ($contents as $id=>$qty)

{

global $db;

$sql = 'SELECT * FROM products WHERE id = '.$id;

$result = $db->query($sql);

$row = $result->fetch();

extract($row);

 

$qty123 = $_POST["qty1"];

$qty234 = $_POST["qty2"];

 

$output[] = '<tr>';

$output[] = '<td>'.$cnt.'</td>';

$output[] = '<td>'.$product_NAME.'</td>';

$output[] = '<td>'.$qty.'</td>';

$output[] = '<td>'.$qty123[cnt1].'</td>';

$output[] = '<td>'.$qty234[cnt1].'</td>';

$output[] = '</tr>';

$cnt1++;

$cnt++;

}

$output[] = '</table>';

 

 

} else {

$output[] = '<p>Sorry You Have No Items In Your Cart.</p>';

}

return join('',$output);

}

?>

 

 

 

 

Thnx in Advance

Jeet

 

Link to comment
Share on other sites

Actually it is there GingerRobot. Lol. Just hard to spot because of the way the code was pasted.

 

##################################

Functions.inc.php

#################

<?php
function writeShoppingCart() {
$cart = $_SESSION['cart'];
$cat_ID = $_SESSION['cat_ID'];

if (!$cart) {
return '<p>Sorry You Have No Items In Your Cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart[/url]</p>';
}
}

function showCart() {
global $db;
$cart = $_SESSION['cart'];
$cat_ID = $_SESSION['cat_ID'];

$qty1 = $_POST["qty1"];
$_SESSION['qty1']="$qty1";
session_register("$qty1");
$qty1 = $_SESSION['qty1'];

$qty2 = $_POST["qty2"];
$_SESSION['qty2']="$qty2";
session_register("$qty2");
$qty2 = $_SESSION['qty2'];


if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" name="cart" method="post" id="cart"&qty1="qty1"&qty2="qty2">';
$output[] = '<table width="501" >';
$output[] = '<tr bgcolor="#00659C" height="25">';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">Remove</font></td>';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">Product Name</font></td>';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">Quantity</font></td>';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">Size</font></td>';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">Unit   (mt/kg/unit)</font></td>';
$output[] = '</tr>';
$output[] = '<tr height="30" >';
$output[] = '<td></td>';
$output[] = '<td></td>';
$output[] = '<td></td>';
$output[] = '<td></td>';
$output[] = '<td></td>';
$output[] = '</tr>';


foreach ($contents as $id=>$qty)
{
$sql = 'SELECT * FROM products WHERE id ='.$id ;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);

$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove[/url]</td>';
$output[] = '<td><p>'.$product_NAME.'</p></td>';
$output[] = '<td><p><input type="text" name="qty'.$id.'" value="'.$qty.'" size="10"/> </p></td>';
$output[] = '<td><p><input type="text" name="qty1" value="'.$qty1.'"  size="10"/> </p></td>';
$output[] = '<td><p><input type="text" name="qty2"  value="'.$qty2.'" size="10"/> </p></td>';
$output[] = '<tr>';
}
$output[] = '</table>';
$output[] = '<div><button type="submit">Update cart</button></div>';
//added
$output[] = '</form>';
}else {
$output[] = '<p>Sorry You Have No Items In Your Cart.</p>';
}
return join('',$output);
}
/////////////////////////////////////////////////////////////////////////////////////////////
function showCart1() {
global $db;

$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<table>';
$output[] = '<table width="501" >';
$output[] = '<tr bgcolor="#00659C" height="25">';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">SINo</font></td>';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">Product Name</font></td>';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">Quantity</font></td>';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">Size</font></td>';
$output[] = '<td><font face=\"Arial\" color=\"#FF0000\">Unit</font></td>';
$output[] = '</tr>';
$output[] = '<tr height="5" >';
$output[] = '<td></td>';
$output[] = '<td></td>';
$output[] = '<td></td>';
$output[] = '<td></td>';
$output[] = '<td></td>';
$output[] = '</tr>';
$cnt=1;
$cnt1=1;
foreach ($contents as $id=>$qty)
{
global $db;
$sql = 'SELECT * FROM products WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);

$qty123 = $_POST["qty1"];
$qty234 = $_POST["qty2"];

$output[] = '<tr>';
$output[] = '<td>'.$cnt.'</td>';
$output[] = '<td>'.$product_NAME.'</td>';
$output[] = '<td>'.$qty.'</td>';
$output[] = '<td>'.$qty123[cnt1].'</td>';
$output[] = '<td>'.$qty234[cnt1].'</td>';
$output[] = '</tr>';
$cnt1++;
$cnt++;
}
$output[] = '</table>';


} else {
$output[] = '<p>Sorry You Have No Items In Your Cart.</p>';
}
return join('',$output);
}
?>

 

And as I said, check the $row is actually being returned. If not, you have an error in your query.

Link to comment
Share on other sites

this is visible in the page if i put the code

echo "<pre>", print_r($row), "</pre>";
exit; 

Array

(

    [id] => 2

    [cat_ID] => 1

    [product_NAME] => Garnet

    [image_URL] => images/Garnet.jpg

    [product_NOTES1] => totally natural almandite XXXXXXXXXXXXXX

)

1

Link to comment
Share on other sites

Having read the first few lines of the OP's post I noted that he mentioned a failure in the extract() function.

I don't recommend the usage of this function, as it extracts arrays into variables in the current symbol table. This could cause variables you already have of the same name to be overwritten. USE WITH CAUTION!!!

 

Incidently the error implies that whatever variable you're giving it to extract is NOT an array. Hence your problem.

Link to comment
Share on other sites

aschk,

 

Thnx ur Suggesion.

 

But can u pls suggest which function should i use in place of extract() function.I have already post my code ie cart.php and function.inc.php.and one more php page i have where we declare all the variables of mysql classes.If u want then i can  send u that Page.

 

thnx

jeet

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.