•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 427,668 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,283 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1020 | Replies: 0
![]() |
•
•
Join Date: Sep 2006
Posts: 10
Reputation:
Rep Power: 3
Solved Threads: 0
1)Error display in cart.php page:
PHP Notice: Undefined offset: 0 in C:\test\kelly.php on line 16 PHP Notice: Undefined variable: totalCost in C:\test\kelly.php on line 61
2)Error show when i try to remove item form cart:
PHP Notice: Undefined variable: db in C:\test\kelly.php on line 43 PHP Fatal error: Call to a member function query() on a non-object in C:\test\kelly.php on line 43
--------------------------------------------------------
//This is "db.php"
<?php
function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID
session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}
?>
-------------------------------------------------------------------
//This is "product.php"
<?php
include("db.php");
$db=new mysqli('localhost','root','','test');
$db->select_db('test');
$query="select * from items order by itemName asc";
$result = $db->query($query);
?>
<?php
while($row =$result->fetch_assoc())
{
?>
<table border=2>
<tr>
<td width="30%" height="25">
<font face="verdana" size="2" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="2" color="black">
<?php echo $row["itemPrice"]; ?>
</font>
</td>
<td width="50%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemDesc"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="2" color="black">
<a href="cart.php?action=add_item&id=<?php echo $row["itemId"]; ?>&qty=1">Add Item</a>
</font>
</td>
</tr>
<br />
<?php }?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="100%" colspan="4">
<font face="verdana" size="1" color="black">
<a href="cart.php"> View Your Shopping Cart >></a>
</font>
</td>
</tr>
</table>
</body>
</html>
---------------------------------------------------------
//This is "cart.php"
<?php
include("kelly.php");
include("db.php");
?>
<?php
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
?>
-----------------------------------------------
//This is "kelly.php"
<?php
function AddItem($itemId, $qty){
$db=new mysqli('localhost','root','','test');
$db->select_db('test');
$query="select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId";
$result=$db->query($query);
$row =$result->fetch_assoc();
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
$query="insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)";
$result=$db->query($query);
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId, $qty);
}
}
function UpdateItem($itemId, $qty){
$query="update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId";
$result=$db->query($query);
}
function RemoveItem($itemId){
$query="delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId";
$result=$db->query($query);
}
function ShowCart(){
$db=new mysqli('localhost','root','','test');
$db->select_db('test');
$query="select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc";
$result=$db->query($query);
while($row = $result->fetch_assoc())
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a>
</font>
</td>
</tr>
<br />
<?php } ?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font>
</td>
</tr>
<?php } ?>
--------------------------------------------------end//
PHP Notice: Undefined offset: 0 in C:\test\kelly.php on line 16 PHP Notice: Undefined variable: totalCost in C:\test\kelly.php on line 61
2)Error show when i try to remove item form cart:
PHP Notice: Undefined variable: db in C:\test\kelly.php on line 43 PHP Fatal error: Call to a member function query() on a non-object in C:\test\kelly.php on line 43
--------------------------------------------------------
//This is "db.php"
<?php
function GetCartId()
{
// This function will generate an encrypted string and
// will set it as a cookie using set_cookie. This will
// also be used as the cookieId field in the cart table
if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{
// There is no cookie set. We will set the cookie
// and return the value of the users session ID
session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}
?>
-------------------------------------------------------------------
//This is "product.php"
<?php
include("db.php");
$db=new mysqli('localhost','root','','test');
$db->select_db('test');
$query="select * from items order by itemName asc";
$result = $db->query($query);
?>
<?php
while($row =$result->fetch_assoc())
{
?>
<table border=2>
<tr>
<td width="30%" height="25">
<font face="verdana" size="2" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="2" color="black">
<?php echo $row["itemPrice"]; ?>
</font>
</td>
<td width="50%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemDesc"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="2" color="black">
<a href="cart.php?action=add_item&id=<?php echo $row["itemId"]; ?>&qty=1">Add Item</a>
</font>
</td>
</tr>
<br />
<?php }?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="100%" colspan="4">
<font face="verdana" size="1" color="black">
<a href="cart.php"> View Your Shopping Cart >></a>
</font>
</td>
</tr>
</table>
</body>
</html>
---------------------------------------------------------
//This is "cart.php"
<?php
include("kelly.php");
include("db.php");
?>
<?php
switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
ShowCart();
break;
}
case "remove_item":
{
RemoveItem($_GET["id"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}
?>
-----------------------------------------------
//This is "kelly.php"
<?php
function AddItem($itemId, $qty){
$db=new mysqli('localhost','root','','test');
$db->select_db('test');
$query="select count(*) from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId";
$result=$db->query($query);
$row =$result->fetch_assoc();
$numRows = $row[0];
if($numRows == 0)
{
// This item doesn't exist in the users cart,
// we will add it with an insert query
$query="insert into cart(cookieId, itemId, qty) values('" . GetCartId() . "', $itemId, $qty)";
$result=$db->query($query);
}
else
{
// This item already exists in the users cart,
// we will update it instead
UpdateItem($itemId, $qty);
}
}
function UpdateItem($itemId, $qty){
$query="update cart set qty = $qty where cookieId = '" . GetCartId() . "' and itemId = $itemId";
$result=$db->query($query);
}
function RemoveItem($itemId){
$query="delete from cart where cookieId = '" . GetCartId() . "' and itemId = $itemId";
$result=$db->query($query);
}
function ShowCart(){
$db=new mysqli('localhost','root','','test');
$db->select_db('test');
$query="select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" . GetCartId() . "' order by items.itemName asc";
$result=$db->query($query);
while($row = $result->fetch_assoc())
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a>
</font>
</td>
</tr>
<br />
<?php } ?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font>
</td>
</tr>
<?php } ?>
--------------------------------------------------end//
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- syntax error? .. operator overload method problemo (C)
- syntax error that I just can't seem to find! (Visual Basic 4 / 5 / 6)
- UPDATE syntax error (MySQL)
- Subshell Problem, syntax error...Help please! (Shell Scripting)
- DECLARATION SYNTAX ERROR (for bc 31 user) (C++)
- Parse error, syntax error, Forbids declaration (C++)
Other Threads in the PHP Forum
- Previous Thread: Coding help - Is this possible? I think it is.
- Next Thread: CAn anyone provide me a very simple code for shopping cart?


Linear Mode