General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 30th, 2003, 01:30 AM
Alicia Alicia is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 116 Alicia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 49 sec
Reputation Power: 6
Unhappy creating shopping cart - doubt

Hi guys,

I have some problem in creating a shopping cart.. actually i tried learn how to create a shopping cart by following one of the article published in DevArticle... but encoutered a lot of prob

My questions:
by the time i press the showcart link in the product.php page, it shows a blank page... a page with some output should be displayed right ?? did i insert the javascript at the wrong place or what ?

when press the add item link, it says that no database selected.. why is that so ??? the include file do works in the product page then why it doesn't work in cart page ???

i did try to put all the functions into a function.php and include it in the file after include(db.php) so i dun have to put all the functions after the switch case and i can used it for some other page... but it shows a lot of errors when i use include("function.php") function .. thats y i didn't make an include file for all function.. my friend said my showcart function contains error.. is that so ??

Please advise.. TQ


These are my coding..
my db.php file:
PHP Code:
<?

$dbServer 
"localhost";
$dbUser "user";
$dbPass "pass";
$dbName "name_db";

function 
ConnectToDb($server$user$pass$database)
{
$s mysql_connect($server$user$pass) or die("Query failed: $query<br><br>" mysql_error());
$d mysql_select_db($database$s) or die("Query failed: $query<br><br>" mysql_error());
/*
if (!$s || !$d)
return false ; 
else
return true ;
*/

}



function 
GetCartId()
{
//generate an encrypted string and set it as cookie
if (isset($_cookie["cartId"]))
{
return 
$_cookie["cartId"];
}
else
{
session_start();
setcookie("cartId"session_Id(), time() + ((3600 24) * 30));
return 
session_id();
}
}

?>


my products.php file:
<?

include ("db.php");

$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName);
//$result = mysql_query("select * from items order by itemName asc");
//show the error with mysql_error() function
$result mysql_query("select * from items order by itemName asc") or die("Query failed: $query<br><br>"mysql_error());

?>

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

<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>

<table border="0">
<?php
while($row mysql_fetch_array($result))
{
?>
<tr>
<td width="10%" height="25">

<font face="Arial" size="2" color="black"><?php echo $row["itemName"]; ?>
</font>
</td>

<td width="10%" height="25">
<font face="arial" size="2" color="black">
RM<? echo $row["itemPrice"]; ?>
</font>
</td>

<td width="20%" height="25">
<font face="arial" size="2" 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>
<td></td>
</tr>


<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<? ?>
<tr>
<td width="100%" colspan="4">
<font face="arial" size="1" color="black"><a href="cart.php">Your shopping cart >> 
</a></font>
</td>
</tr>

</table>
</BODY>



my cart.php file:
<?

include("db.php");
Switch(
$_GET["action"])
{
case 
"add_item":
{
AddItem($_GET["id"], $_GET["qty"]);
//ShowCart();
include("showcart.php");
break;
}

case 
"update_item":
{
UpdateItem($_GET["id"], $_GET["qty"]);
//ShowCart();
include("showcart.php");
break;
}

case 
"remove_item":
{
RemoveItem($_GET["id"]);
//ShowCart();
include("showcart.php");
break;
}

default:
{
//ShowCart();
include("showcart.php");
}

}

function 
AddItem($itemId$qty)

$result mysql_query("Select count(*) from cart where cookieId = '" GetCartId() . "' and itemId = $itemId")or die("Query failed: $query<br><br>" mysql_error());

$row mysql_fetch_row($result);
$numRows $row[0];

if(
$numRows == 0)
{
mysql_query("insert into cart(cookieId, itemId, qty) values('".GetCartId()."', $itemId, $qty)") or die("query failed".mysql_error());
}

else

{

UpdateItem($itemId$qty);

}
}


function 
UpdateItem($itemId$qty)
{

mysql_query("update cart set qty = $qty where cookieId = '".GetCartId()."' and itemId = $itemId");

}


function 
RemoveItem($itemId)
{

mysql_query("delete from cart where cookieId = '" .GetCartId()."' and itemId = $itemId");
}

?>


my showcart.php file:
<?
function ShowCart()
{
?>
<script language="JavaScript">
function UpdateQty(item)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;

document.location.href = 'cart.php?update_item&id='+itemId+'&qty='+newQty;
}

</script>

<?
$result 
mysql_query("select* from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '".GetCartId()."' order by items.itemName asc");

while(
$row mysql_fetch_array($result))
{
$totalCost += ($row["qty"] * $row["itemPrice"]);
?>

<tr>
<td width = "15%" height="25">
<font face="arial" size="2" color="black">
<select name="<? echo $row["itemId"]; ?>" onChange="UpdateQty(this)">

<?
for ($i 1$i <=20$i++)
{
echo 
"<option";
if(
$row["qty"] == $i)
{
echo 
"Selected";
}

echo 
">".$i."</option>";

}
?>
</select>
</font>
</td>

<td width="55%" height="25">
<font face="arial" size="2" color="black">
<? echo $row["itemName"]; ?>
</font>
</td>

<td width="20%" height="25">
<font face="arial" size="2" color="black">
RM<? echo number_format($row["itemPrice"], 2,".""."); ?>
</font>
</td>

<td width="10%" height="25">
<font face="arial" size="2" color="black">
<a href="cart.php?action=remove_item&id=<? echo $row["itemId"]; ?>">Remove</a>
</font>
</td>

<?
// close for while loop

//$totalCost += ($row["qty"] * $$row["itemPrice"]);
//$totalCost = $totalCost + ($row["qty"] * $row["itemPrice"]); // same by using +=
?>


<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>

<tr>
<td width="70%" colspan="2">
<font face="arial" size="2" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>

<td width="30%" colspan="2">
<font face="arial" size="3" color="black">
<b>Total: RM<? echo number_format($totalCost2".","."); ?> </b>
</font>
</td>
</tr>
<? //close function?>

Reply With Quote
  #2  
Old September 30th, 2003, 06:26 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Hi Alicia,

Try add this to your code:

db.php
PHP Code:
 $dbServer "localhost"
$dbUser "user"
$dbPass "pass"
$dbName "name_db"

global 
$dbServer;
global 
$dbUser;
global 
$dbPass;
global 
$dbName;

function 
ConnectToDb($server$user$pass$database

... 


That should allow correct your "no db selected" error.

Btw, it makes it easier if you include your code within [ php ] [ /php ] tags.
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old September 30th, 2003, 08:38 PM
Alicia Alicia is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 116 Alicia User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 27 m 49 sec
Reputation Power: 6
I have tried to insert global to these variables.. but it still display the same output...

Reply With Quote
  #4  
Old October 1st, 2003, 04:11 AM
Relax Relax is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 43 Relax User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
correct me if i'm wrong coz i havent been doing much php for quite some time now..

but arent you supposed to connect in ur cart.php too? just including the db doesnt mean it's connected (unless you run the connect method in the db.php ..

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > creating shopping cart - doubt


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway