Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

Shopping Cart Problem
Welcome, Guest. Please login or register.
December 02, 2008, 06:42:41 AM
11304 Posts in 1248 Topics by 498 Members
Latest Member: katCheeme
Experts Round Table Network  |  Databases  |  MySQL  |  Shopping Cart Problem « previous next »
Pages: 1 [2]
Author Topic: Shopping Cart Problem  (Read 1155 times)
paul_mellon36

Offline Offline

Posts: 17


WWW
« Reply #15 on: February 22, 2006, 12:03:17 PM »

OK, having another bit of trouble however some progress has been made.

We are getting the item id and checking it against the item_id in the colour table, this is fine, but when we actually choose the colour id we are wrong.

We choose the colour id which matched the item id and also matches with the colours itemid. I think this has explained it better?? if not i'll post some code below..
Code:

mysql_query("insert into tblstore_items values ('1', '1', 'Easter Egg Selection',5.99, 'Which chocolate most tickles your fancy this easter?','images/easter/newsletter_easter.jpg')");
mysql_query("insert into tblstore_items values ('2', '1', 'Chocolate Bunny',4.99, 'Give this gorgeous bunny to a loved one this easter.','images/easter/EasterBunny.jpg')");
mysql_query("insert into tblstore_items values ('3', '1', 'Easter Candle Pack',10.99, 'Light up your life this easter with some scented candles.','images/easter/eastdisp.jpg')");
mysql_query("insert into tblstore_items values ('4', '1', 'Chocolate Surprise Egg',3.99, 'Open wide for more chocolate this easter','images/easter/surpriseegg.jpg')");

mysql_query("insert into tblstore_items values ('5', '2', 'Red Rose',4.99,'Single Red Rose', 'images/valentines/bunches.jpg')");
mysql_query("insert into tblstore_items values ('6', '2', 'Love Heart Necklace',25.99,'Gorgeous Necklace any girl would be delighted with.','images/valentines/diamondnecklace.jpg')");
mysql_query("insert into tblstore_items values ('7', '2', 'Rose Petals',7.99,'Make someone feel special with this bag of Rose Petals.', 'images/valentines/rosepetals.jpg')");
mysql_query("insert into tblstore_items values ('8', '2', 'Champagne & Truffles',40.99,'Moet & Chandon Champagne & Chocolate Truffles', 'images/valentines/champagne&truffles.jpg')");

mysql_query("insert into tblstore_colour values ('1', '3', 'Yellow')");
mysql_query("insert into tblstore_colour values ('2', '3', 'White')");
mysql_query("insert into tblstore_colour values ('3', '3', 'Mixed')");
mysql_query("insert into tblstore_colour values ('4', '2', 'Gold')");
mysql_query("insert into tblstore_colour values ('5', '2', 'Silver')");
mysql_query("insert into tblstore_colour values ('6', '6', 'Gold')");
mysql_query("insert into tblstore_colour values ('7', '6', 'Silver')");

Code:


for($i = 0; $i < count($_SESSION['basket']); $i++)
{
$product = $_SESSION['basket'][$i];
$myarray = explode('|', $product);

$get_cart = "SELECT c.id AS cat_id, c.cat_title, si.id, si.item_title, si.item_price, si.item_desc, si.item_image FROM tblstore_items AS si LEFT JOIN tblstore_categories AS c ON c.id = si.cat_id WHERE si.id = $myarray[0]";
$get_cart_res = mysql_query($get_cart,$conn);

while($row = mysql_fetch_array($get_cart_res))
{
$item_id = $row['id'];
$item_title = $row['item_title'];
$item_price = $row['item_price'];
}
$unit_price = sprintf("%.02f", $item_price * $myarray[1]);
$total_price += $unit_price;

$get_color = "SELECT * FROM tblstore_colour, tblstore_items WHERE tblstore_colour.item_id = tblstore_items.id AND tblstore_colour.id = {$myarray[0]}";

$colour="";
$get_color_results = mysql_query($get_color,$conn);


if ($get_color_results!==FALSE)
{ // or else, add an " or die("failed to execute '$get_color' : ".mysql_error());" to the mysql_query() call above...

   if ($row_colour=mysql_fetch_array($get_color_results)) {
   $colour = $row_colour['item_colour'];
   $colours = ($colour=="") ? "&nbsp;" : $colour;
 }
 else {
 $colours='&nbsp'; // if failed to execute query on colours
}
}
$display_block2 .= "
<tr>
<td class=border align=center>$item_title</td>
<td class=border align = center>$size</td>
<td class=border align = center>$colours</td>
<td class=border align = center>£ $item_price</td>
<td class=border align = center>$myarray[1]</td>
<td class=border align = center>£ $unit_price</td>
<td class=border align = center><a href='removefromcart.php?id=$item_id'>Remove</a></td>
</tr>";

}

$display_block2 .= "</table>
<input name = 'total_prices' type = 'hidden' value = '$total_price'>
<p align = 'center'>Basket Total:  £". $total_price. "<br><br>
<input type = 'submit' name = 'checkout' value = 'Go to Checkout'></p>
</form>";
}

?>
Logged

Paul
seandelaney
Moderator
*
Offline Offline

Posts: 119



WWW
« Reply #16 on: February 24, 2006, 01:11:39 PM »

still stuck?  :scratch:
Logged

paul_mellon36

Offline Offline

Posts: 17


WWW
yep
« Reply #17 on: February 24, 2006, 01:12:52 PM »

i'm afraid so :notworthy:
Logged

Paul
seandelaney
Moderator
*
Offline Offline

Posts: 119



WWW
« Reply #18 on: February 24, 2006, 01:19:20 PM »

try this:

Code:
for($i = 0; $i < count($_SESSION['basket']); $i++)
{
$product = $_SESSION['basket'][$i];
    $myarray = explode('|', $product);

    $get_cart = "SELECT c.id AS cat_id, c.cat_title, si.id, si.item_title, si.item_price, si.item_desc, si.item_image FROM tblstore_items AS si LEFT JOIN tblstore_categories AS c ON c.id = si.cat_id WHERE si.id = $myarray[0]";
    $get_cart_res = mysql_query($get_cart,$conn);

if (mysql_num_rows($get_cart_res) == 1)
{
while($row = mysql_fetch_array($get_cart_res))
{
$item_id = $row['id'];
$item_title = $row['item_title'];
$item_price = $row['item_price'];
}
$unit_price = sprintf("%.02f", $item_price * $myarray[1]);
$total_price += $unit_price;


$get_color = "SELECT tblstore_colour.item_colour FROM tblstore_colour, tblstore_items WHERE tblstore_colour.item_id = tblstore_items.id AND tblstore_colour.item_id = {$myarray[0]}";

$$item_colour = "";
$get_color_results = mysql_query($get_color,$conn);

if (mysql_num_rows($get_color_results) == 1)
{
while ($colour_row = mysql_fetch_array($get_color_results))
{
$colours = $row_colour['item_colour'];
$item_colour = ($colours == "") ? "&nbsp;" : $colours;
}
}
else
{
$item_colour = '&nbsp'; // if failed to execute query on colours
}
}
else
{
// item id in tblstore_items not found
}
 
    $display_block2 .= "
    <tr>
<td class=border align=center>$item_title</td>
        <td class=border align = center>$size</td>
        <td class=border align = center>$item_colour</td>
        <td class=border align = center>£ $item_price</td>
        <td class=border align = center>$myarray[1]</td>
        <td class=border align = center>£ $unit_price</td>
        <td class=border align = center><a href='removefromcart.php?id=$item_id'>Remove</a></td>
    </tr>";
}

$display_block2 .= "</table>
<input name = 'total_prices' type = 'hidden' value = '$total_price'>
<p align = 'center'>Basket Total:  £". $total_price. "<br><br>
<input type = 'submit' name = 'checkout' value = 'Go to Checkout'></p>
</form>";
Logged

VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #19 on: February 24, 2006, 03:10:02 PM »

excuse-me, but if you look at the code I produced, you'll notice that I got rid of silly loops like those two. perhaps you can explain to me what you're trying to achieve ?

Code:
while($row = mysql_fetch_array($get_cart_res))
      {
         $item_id = $row['id'];
         $item_title = $row['item_title'];
         $item_price = $row['item_price'];
      }

(you don't do anything with each iteration's values retrived. Basically, you lose your time retrieving all the rows only to keep the last's values. Tip : reverse the ORDER BY clause, and take the first one's values, whithout a while() loop :D.

This is even more true now that someone added the test "if (mysql_num_rows($get_cart_res) == 1)" to control the loop :D

you know there is only one row, and still you do a while() loop ? and if the num_rows is not 1 (it could be 0 or more than 1, by the way) you just assume there is "// item id in tblstore_items not found "

I don't think this is properly coded.

If you know there can only be zero or ONE line, then get rid of the while loop . By security, if num_rows<>0 (inverse the test), take the first row like I suggested. This can save you in case of doublons.

can you also explain to me why you still append to the $display2 string even if you fell into the case "// item id in tblstore_items not found " ?

Code:
if (mysql_num_rows($get_color_results) == 1)
      {
         while ($colour_row = mysql_fetch_array($get_color_results))
         {
            $colours = $row_colour['item_colour'];
            $item_colour = ($colours == "") ? "&nbsp;" : $colours;
         }
      }

same remark

By the way, you wrote above :
Code:
$$item_colour = "";


this is probably a very embarassing typo, unless you really want a variable variable. This will produce strange effects.

Also, please turn NOTICES on like I explained. Your code will become clearer to you.

regards
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Pages: 1 [2]
« previous next »
    Jump to: