Click to See Complete Forum and Search --> : Loop Problem,plz help


cty
12-18-2006, 09:58 AM
$totalCost=0;
while($row = $result->fetch_assoc())
{

$totalCost+=$row["price"];
--------------------------

why teh $totalcost =0?what happen?

NightShift58
12-18-2006, 10:09 AM
Not enough code to really tell you why.

Probably because $result is false (query didn't work as expected somewhere else in your code).

NogDog
12-18-2006, 10:16 AM
The $totalCost=0; serves two purposes: it ensures the count starts from zero (especially in case register_globals is on and a malicious user sends a "?totalCost=100" in the URL query string), plus it prevents a notice-level error being issued by PHP the first time the $totalCost+=$row["price"]; line is executed when $totalCost has yet to be set (and thus technically there is no value to be incremented, so PHP assumes it is to start at zero).

NightShift58
12-18-2006, 10:22 AM
I think that he really meant was:

"Why is $totalCost still 0 after the loop - where it was supposed to be incremented by the value of individual items in the shopping cart?"

cty
12-18-2006, 10:35 PM
NightShift understand my question well.
So,do i need to provide u a full code?

cty
12-18-2006, 10:56 PM
I think that he really meant was:

You understand my question well.TQ

below are my full code:
-------------------------------------
//products.php

<?php

include("db.php");

$db=new mysqli('localhost','root','','test');
$db->select_db('test');



$query="select * from book order by title asc";
$result = $db->query($query);
?>

<?php
while($row =$result->fetch_assoc())
{
?>

<table border=1 width=80% bgcolor="pink">
<tr>
<td width=20%>
<font face="verdana" size="2" color="black" >
ISBN :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["isbn"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
TITLE:
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["title"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Author :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["author"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Description :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["description"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Condition :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["condition"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Price(RM) :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["price"]; ?>
</font>
</td>
</tr>





<td >
<font face="verdana" size="2" color="black">
<a href="cart.php?action=add_item&id=<?php echo $row["bookid"];?>">Buy</a>
</font>
</td>
</tr>
<br />
</table>

<?php }?>

<tr>
<td >
<hr size="2" color="red" NOSHADE>
</td>
</tr>



<tr>
<td >
<font face="verdana" size="5" color="black">
<a href="cart.php"> View Your Shopping Cart &gt;&gt;</a>
</font>
</td>
</tr>
</table>
</body>
</html>
------------------------------------------------------
//kely.php

<?php


function AddItem($bookid){


$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}

$db->select_db('test')
or die ('Select_db failed:'.$db->error);


$query="select count(*) from cart where cookieId ='GetCartId()'
and bookid = '$bookid'";

$result=$db->query($query)
or die('Query line 17 failed:'.$db->error);

$row =$result->fetch_row();
$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, bookid) values('" . GetCartId() . "', '$bookid')";
$result=$db->query($query)
or die('Query line 29 failed:'.$db->error);
}
else
{
echo "The book already in your shopping cart.";

// This item already exists in the users cart,
}

}

function RemoveItem($bookid){
$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}

$db->select_db('test')
or die ('Select_db failed:'.$db->error);

$query="delete from cart where cookieId = '" . GetCartId() . "' and bookid = $bookid";
$result=$db->query($query)
or die('Query line 52 failed:'.$db->error);
}





function ShowCart(){

$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}

$db->select_db('test')
or die ('Select_db failed:'.$db->error);


$query="select * from cart inner join book on cart.bookid = book.bookid where cart.cookieId = '" . GetCartId() . "' order by book.title asc";
$result=$db->query($query)
or die('Query line 73 failed:'.$db->error);

$totalCost=0;
while($row = $result->fetch_assoc())
{

$totalCost +=$row["price"];
echo $totalCost;

?>




<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["title"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["price"], 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["bookid"]; ?>">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">&lt;&lt; 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 } ?>

---------------------------------------------------------
//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();
}
}

?>
-----------------------------------------------

//cart.php


<?php
include("kelly.php");
include("db.php");
?>


<?php

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["bookid"]);
ShowCart();
break;
}


case "remove_item":
{
RemoveItem($_GET["bookid"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}

?>

NightShift58
12-19-2006, 12:11 AM
Is there by chance a field named "price" in the cart table?

cty
12-19-2006, 07:40 AM
create table book
(
bookid int auto_increment not null,
isbn varchar,
author varchar,
title varchar,
Desc varchar,
Price decimal,
primary key(bookid),

);

create table cart
(
cartId int auto_increment not null,
cookieId varchar(50),
bookid int,
primary key(cartId),

);
------------------
The above is my 2 tables,
So,how am i going to edit my code???

NightShift58
12-19-2006, 08:09 AM
Is price and title displaying in your loop?

cty
12-19-2006, 08:27 AM
$query="delete from cart where cookieId = '" . GetCartId() . "' and bookid = $bookid";

Beside the total never increase,this query also have mistake,but i unable to make it to a correct form,can help?

NightShift58
12-19-2006, 08:37 AM
I think I see it now...

You field is called "Price" and not "price".

So your code should read:$totalCost += $row['Price'];

cty
12-19-2006, 08:46 AM
I hope NightShift can help me,i already stuck in this problem for days.

Please guide me in eidt my code.TQ

cty
12-19-2006, 09:07 PM
i already change to "price",but still the same problem.
Beside,the folowing query also not in correct form,how am i going to edit it?

$query="delete from cart where cookieId = '" . GetCartId() . "' and bookid = $bookid";

NightShift58
12-19-2006, 09:09 PM
You changed what to "price"?

cty
12-19-2006, 11:01 PM
Just to let you know,i already update my coding and my question as below:

Error shown:

Query (line 50) failed:You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the
right syntax to use near '' at line 1

-----------------------------------------
My first problem:

How am i going to eidt this query to a correct form?


$query="delete from cart where bookid =$bookid";


----------------------------------------
My second problem(main problem):


Why the book price and the total price always shown 0?(*When i click on "buy).

-------------------------------------------------

Below are my code:

//product.php


<?php


include("db.php");

$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed(line 7):'.mysqli_connect_error());
}


$db->select_db('test')
or die ('Select_db failed(line 14):'.$db->error);


$query="select * from book order by title asc";
$result = $db->query($query)
or die('Query (line 18) failed:'.$db->error);

?>

<?php
while($row =$result->fetch_assoc())
{
?>

<table border=1 width=80% bgcolor="pink">
<tr>
<td width=20%>
<font face="verdana" size="2" color="black" >
ISBN :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["isbn"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
TITLE:
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["title"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Author :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["author"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Description :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["description"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Condition :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["condition"]; ?>
</font>
</td>
</tr>

<tr>
<td width=20%>
<font face="verdana" size="2" color="black">
Price(RM) :
</font>
</td>
<td >
<font face="verdana" size="2" color="black">
<?php echo $row["price"]; ?>
</font>
</td>
</tr>





<td >
<font face="verdana" size="2" color="black">
<a href=cart.php?action=add_item&bookid=<?php echo $row["bookid"];?>>Buy</a>
</font>
</td>
</tr>
<br />
</table>

<?php }?>

<tr>
<td >
<hr size="2" color="red" NOSHADE>
</td>
</tr>



<tr>
<td >
<font face="verdana" size="5" color="black">
<a href="cart.php"> View Your Shopping Cart &gt;&gt;</a>
</font>
</td>
</tr>
</table>
</body>
</html>


-----------------------------------------------------
//kelly.php

<?php


function AddItem($bookid){

$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}
$db->select_db('test')
or die ('Select_db failed:'.$db->error);

$query="select count(*) from cart where bookid = '$bookid'";

$result=$db->query($query)
or die('Query line 17 failed:'.$db->error);

$row =$result->fetch_row();
$numRows = $row[0];
if($numRows == 0)
{


$query="insert into cart(cookieId, bookid) values('" . GetCartId() . "', '$bookid')";
$result=$db->query($query)
or die('Query line 29 failed:'.$db->error);
}

else
{
echo "The book already in your shopping cart.";
echo "<br />";

}

}


function RemoveItem($bookid){

$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}

$db->select_db('test')
or die ('Select_db failed(line 53):'.$db->error);

$query="delete from cart where bookid =$bookid";

$result=$db->query($query)
or die('Query (line 50) failed:'.$db->error);
}





function ShowCart( ){

$db=new mysqli('localhost','root','','test');
if(mysqli_connect_errno())
{
die('Connect failed:'.mysqli_connect_error());
}

$db->select_db('test')
or die ('Select_db failed:'.$db->error);


$query="select * from cart inner join book where cart.bookid = book.bookid and cart.cookieId = '" . GetCartId() . "'order by book.title asc";
$result=$db->query($query)
or die('Query line 78 failed:'.$db->error);

$totalCost=0;
while($result->fetch_assoc())
{

$totalCost +=$row["price"];


?>

<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["title"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["price"], 2); ?>

</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href=cart.php?action=remove_item&bookid=<?php echo $row["bookid"];?>>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">&lt;&lt; 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 } ?>

-------------------------------------------------

//db.php

<?php

function GetCartId()
{

if(isset($_COOKIE["cartId"]))
{
return $_COOKIE["cartId"];
}
else
{


session_start();
setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
return session_id();
}
}

?>
------------------------------------------------------
//cart.php

<?php
include("kelly.php");
include("db.php");
?>


<?php

switch($_GET["action"])
{
case "add_item":
{
AddItem($_GET["bookid"]);
ShowCart();
break;
}


case "remove_item":
{
RemoveItem($_GET["bookid"]);
ShowCart();
break;
}
default:
{
ShowCart();
}
}

?>


---------------------------------------------//end


I hope u able to help me find out my coding bugs.TQ

cty
12-19-2006, 11:05 PM
As i suspect,the main problem may occur in showcart() function and also remove() function.
Also,as i tested,i found that remove() function unable to get the value of $bookid.

This is what i think only.Hope you able to guide me.TQ