Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP Shopping Cart - Immediate Help needed!
Old 12-08-2008, 11:02 PM PHP Shopping Cart - Immediate Help needed!
Junior Talker

Posts: 1
Name: Dylan
Trades: 0
Hey all, just joined the site because I'm having some serious issues with my final project for my PHP programming class. We were given a basic shopping cart to work off of for a bookstore project, and I just can't figure out how to work the thing. I echo'd the $bookArray array and the result was "Array."

Here's the code:

Code:
<?php

//Shopping cart uses cookies to store cart items.
//PHP script uses an array for adding, removing and displaying the cart items.
//Cookies can contain only string data so array must be serialized.
 
$cookieName = "myCart2";
// retrieve cookie and unserialize into $bookArray
if (isset($_COOKIE[$cookieName])) {
    $bookArray = unserialize($_COOKIE[$cookieName]);
}
// Add items to cart
$addISBN = CleanISBN($_GET['addISBN']);
if (strlen($addISBN) > 0) {
    if (isset($addISBN, $bookArray)) {
        // Increment by +1
        $bookArray[$addISBN] += 1;
    } else {
        // Add new item to cart
        $bookArray[$addISBN] = 1;
    }
}
// Remove items from cart
$deleteISBN = CleanISBN($_GET['deleteISBN']);
if (strlen($deleteISBN) > 0) {
    if (isset($bookArray[$deleteISBN])) {
        // Deincrement by 1
        $bookArray[$deleteISBN] -= 1;
        // remove ISBN from array if qty==0
        if ($bookArray[$deleteISBN] == 0) {
            unset($bookArray[$deleteISBN]);
        }
    }
}
if (isset($bookArray)) {
   // Write cookie
   setcookie($cookieName, serialize($bookArray), time() + 60 * 60 * 24 * 180);

//Count total books in cart
$totalbooks = 0;
   foreach($bookArray as $isbn => $qty) {
       $totalbooks += $qty;
   }
   setCookie('BookCount', $totalbooks,  time() + 60 * 60 * 24 * 180);
}
//***************************************************
//You do not need to modify any code above this point
//***************************************************

?>
<html>
<head>
<title>Shopping Cart -- NerdWords.com</title>
</head>
<body>
<div align="center">
  <center>
<p>        
<? echo $totalbooks . " item";
   if ($totalbooks != 1) {
      echo 's';
   }
   if ($totalbooks = 0) {
          echo 's';
   }
   echo ' in your cart'
?> 
</p>

<table border="0" cellpadding="5" width="300">
<?
  if (count($bookArray)) {
    echo "<tr><td>ISBN</td><td>Title</td><td>Qty</td><td>Price</td><td>Add/Remove</td></tr>";
    $count = 1;
        foreach($bookArray as $isbn => $qty) {
    $strSQL = "SELECT ISBN, title, price FROM bookdescriptions WHERE ISBN = '$isbn'";
}
?>
       <tr>
          <td><a href="ProductPage.php?isbn=<? echo $isbn; ?>"></td>
          <td><? echo $row['title']; ?></a></td>
          <td><? echo $count ?></td>
          <td><? echo $row['price']; ?></td>
          <td>
               <a href="?addISBN=<? echo $isbn; ?>">Add</a><br>
               <a href="?deleteISBN=<? echo $isbn; ?>">Remove</a>
          </td>

       </tr>
<?
       $count += 1;
}
?>
</table>
</center>
</div>

<?php 
// ********************************************
// Utilities **********************************
function CleanISBN($isbn)
{
    return preg_replace("/[^0-9X]{1,}/", "" , substr($isbn, 0, 10));
}
?>
rondead is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-09-2008, 04:43 AM Re: PHP Shopping Cart - Immediate Help needed!
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Use the "print_r()" function to examine the contents of an array
stoot98 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP Shopping Cart - Immediate Help needed!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.09539 seconds with 12 queries