I have two simple questions. I have created a basic shopping cart for my site and having a couple minor issues. The cart so far functions properly, removes, adds items etc.
1. I would like the cart to display (0) items in cart, while right now it is only displaying () when the cart is empty. I tried a few different methods and clearly I cannot get the cart to display the "(0)".
2. I have adapted the cart to be visible throughout the site so when the user is searching the site (shopping), it will always show the cart total running tally, etc. in the "right container div". I also have a link to the cart in my "navigation bar". Both files are retrieved externally through the index page.
When I am in cart.php, the right container displays the cart information correctly. The problem is when I navigate throughout the site the cart shows its empty with no price total, etc. I know it has something to do with sessions and retrieving the data from the cart page/database and simply displaying it in the "right container". Do I also have to add something to the index and navigation?
I am not sure if I need to show any other code because from what I understand it is just a session code I need to apply somewhere..
Thanks again and have a great Sunday!
(1.) This is the code for the first question. (I hate tables but for the cart its okay? right!?!?
Hello, well I guess I can say I have spent most of the day reading and to say the least, I have made some progress. I have adjusted the code to show the quantity throughout the site. "Almost" every page displays the current quantity in the cart. The one page does not show the quantity without throwing errors at me is the product page.
If I change session_register('final'); it displays errors, so I have commented it out until I figure out a solution. I also think I did the coding wrong because all I did was copy/paste my code from my cart.php to my product.php file. That being said I believe amongst other things I am not properly retrieving the correct information in a "legal" manner.
This is the product page code with issues of either;
Always showing a 0
or Showing the proper quantity but with errors thrown at the top on the lines for session_register.
<?php if (!isset($_SESSION['final']) )
{
//session_register('final');
$_SESSION['final'] = 0;}
?>
<?php
// Check to see the URL variable is set and that it exists in the database
if (isset($_GET['id'])) {
// Connect to the MySQL database
include_once"store/storescripts/connect_to_mysql.php";
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);
// Use this var to check to see if this ID exists, if yes then get the product
// details, if no then exit this script and give message why
$sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)){
$product_name = $row["product_name"];
$partnumber = $row["partnumber"];
$subpartnumber = $row["subpartnumber"];
$yearavail = $row["yearavail"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$details = $row["details"];
$grams = $row["grams"];
$sealed = $row["sealed"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
}
} else {
echo "That item does not exist.";
exit();
}
} else {
echo "Data to render this page is missing.";
exit();
}
$_SESSION['final'] = 0;
$show = ""; $display = ""; $count2= ""; // NEW test again for the zeroes.
$cartOutput = "";
$cartTotal = "";
$cartWeight = "";
$cartQty = "";
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h3>Your shopping cart is empty</h3>";
} else {
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id= $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$subpartnumber = $row["subpartnumber"];
$grams = $row["grams"];
I don't see anywhere in that script where it displays "( something )".
There's a line that says "Your shopping cart is empty" ??
I'm guessing there are other scripts involved. Perhaps we need to see more?
Yes you are correct. The "your shopping cart is empty" is there so if you goto the shopping cart page it will show the words too. Maybe if I post a link to the site that might help also just incase someone wants to physically test/look at it. I also noticed when I browse the site with my iPhone, the quantity is always showing (0) unless I am in the cart page- there it will show the correct quantity. Next I have to add the total $ and shipping. Yikes.
I have tried many different combinations to get the quantity to show up on all pages but I am having lots of trouble calculating the logic of this code. I can get the quantity in the right container to show (0). If I adjust the code to get the correct quantity on all the pages, when the cart is emptied it will still show the amount that was in the cart. I have been trying this for so long that I honestly forgot how to get it work some what correct again.
I have been messing with the index, right container, cart and product page and it is just turning into a giant mess. I started all of this about a month ago so my knowledge on coding is limited (hence why I joined here.) I will also include a link to the site because perhaps someone would like to see the live version in progress.
Create a single small PHP script that echoes the cart count.
That is all the script does.
Then, when you get that working, include it in the HTML of
anywhere you wish to display it.
Example.
The cart contains <?php include("count.php");?> items.
That way, you only code it one time and use it everywhere.
This idea worked well! I am using it right now and every page works. Well almost every page, my product page still shows the cart is empty when a user views a product, also the Total $ Cart amount does not load.
Could this be because in the product script is has something to do with - only run if the "product" is 0 or greater? I cannot think of any other reason why I cant get the product page to display items in the cart...
It doesn't matter where I add it to the script, it will always show up zero and the total does not show like the other pages. I tried also making the product page a unique include.php and that also does not work. You can see about 7 lines down on the product page I added the "count.php". It works like a charm on all the other pages, but only the product page it resets, or does not acknowledge it is there. I believe it has something to with the "if ($productCount > 0) {" but if I tamper with that, it will not display properly.
I am so close I can taste it! I just can't get past this part! Here is a link to the site again to see the live version if interested.
PRODUCT PAGE SCRIPT
PHP Code:
<?php ob_start(); // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php include("php/count.php");?> <?php // Check to see the URL variable is set and that it exists in the database if (isset($_GET['id'])) { // Connect to the MySQL database include_once"store/storescripts/connect_to_mysql.php"; $id = preg_replace('#[^0-9]#i', '', $_GET['id']); // Use this var to check to see if this ID exists, if yes then get the product // details, if no then exit this script and give message why $sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { // get all the product details while($row = mysql_fetch_array($sql)){ $product_name = $row["product_name"]; $partnumber = $row["partnumber"]; $subpartnumber = $row["subpartnumber"]; $yearavail = $row["yearavail"]; $category = $row["category"]; $subcategory = $row["subcategory"]; $details = $row["details"]; $grams = $row["grams"]; $sealed = $row["sealed"]; $price = $row["price"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); } } else { echo "That item does not exist."; exit(); } } else { echo "Data to render this page is missing."; exit(); } mysql_close(); ob_end_flush(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- THIS ENDS INSERT CENTER CONTAINER DYNAMIC INFORMATION ABOVE HERE ONLY. DO NOT CHANGE ANYTHING BELOW.--> </div> <!-- End CENTER container--> <?php include('php/r_container.php'); ?> <!-- Right Container --> <?php include('php/footer.php'); ?> </div><!-- End wrapcontent--> </div><!-- End pagecontainer--> </body> </html>
Those variables would be echoed on the cart page. I suppose I am "building" those variables on this script for absolutely no reason. I obviously have to spend a day or two (maybe more) to completely go over all the code and pages to avoid errors like this.
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 1 (if user attempts to add something to the cart from the product page)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
// If the cart session variable is not set or cart array is empty
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
// RUN IF THE CART IS EMPTY OR NOT SET
$_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1));
} else {
// RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $pid) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
$wasFound = true;
} // close if condition
} // close while loop
} // close foreach loop
if ($wasFound == false) {
array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
}
}
header("location: cart.php");
exit();
}
?>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 2 (if user chooses to empty their shopping cart)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") {
unset($_SESSION["cart_array"]);
}
?>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 3 (if user chooses to adjust item quantity)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['quantity_to_adjust']) && $_POST['quantity_to_adjust'] != "") {
//Execute some code
$quantity_to_adjust = $_POST['quantity_to_adjust'];
$quantity = $_POST['quantity'];
$quantity = preg_replace('#[^A-Za-z0-9]#i', '', $quantity); // filter everything but numbers and letters
if($quantity >=10) {$quantity=9;}
if($quantity <1) {$quantity=1;}
if($quantity =="") {$quantity=1;}
$i=0;
foreach ($_SESSION["cart_array"] as $each_item) {
$i++;
while (list($key, $value) = each($each_item)) {
if ($key == "item_id" && $value == $quantity_to_adjust) {
// That item is in cart already so let's adjust its quantity using array_splice()
array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $quantity_to_adjust, "quantity" => $quantity)));
} // close if condition
} // close while loop
} // close foreach loop
header("location: cart.php");
exit();
}
?>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 4 (if user chooses to remove an item from the cart)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") {
// Access the array and run code to remove that array index
$key_to_remove = $_POST['index_to_remove'];
if (count($_SESSION["cart_array"]) <= 1) {
unset($_SESSION["cart_array"]);
} else {
unset($_SESSION["cart_array"]["$key_to_remove"]);
sort($_SESSION["cart_array"]);
}
header("location: cart.php");
exit();
}
?>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 5 (Render the cart for the user to view on the page)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$show = ""; $display = ""; $count2= "";
$cartOutput = "";
$cartTotal = "";
$cartWeight = "";
$cartQty = "";
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h3>Your shopping cart is currently empty!</h3>";
} else {
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item) {
$item_id= $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while ($row = mysql_fetch_array($sql)) {
$product_name = $row["product_name"];
$price = $row["price"];
$subpartnumber = $row["subpartnumber"];
$grams = $row["grams"];
<!-- THIS ENDS INSERT CENTER CONTAINER DYNAMIC INFORMATION ABOVE HERE ONLY. DO NOT CHANGE ANYTHING BELOW.-->
</div> <!-- End CENTER container-->
<?php include('php/r_container.php'); ?> <!-- Right Container -->
<?php include('php/footer.php'); ?> </div><!-- End wrapcontent-->
</div><!-- End pagecontainer-->
</body>
</html>
When it goes to "product.php" I wonder if it's missing a session_start();
That's why the cart shows empty.
If you put this at the top of each script: <?php include("php/count.php");?>
put your session_start(); in that script, and remove it elsewhere.
But I'm thinking "product.php" doesn't ever see a session_start();
EDIT:
I would be cool if a zoomed image appeared when you mouse over a lego figure.
Something JQuery-ish so I could see visual details without clicking on it.
I tried that and it doesn't work. I also tried eliminating the display SESSION_Final and using the cartQty output, that also does not work and eliminates the (0) when the cart is empty. When I tried putting the session start in the count.php file and adding that to all my scripts, my index page fills up with lines and lines of errors.
The other thing is, the cart total, quantity etc is displayed in the r_container, so technically it has nothing do with the cart page, product page, index page. The right container always loads on its own, doesn't matter which page is loading. You would think the product page and r_container do not interact with each other when loaded, but "technically" they are loading and interacting with each other.
I have tried so many combinations now, I loose count. The closest I came was when the product page loaded, the cart displayed errors pertaining to the r_container script. So they can load together and also interact separately.
As for the enlarge image as you suggested, yes it is a wonderful idea and when you are on the product page, you may have noticed a giant ugly button under the image. That is to remind me that I want to do a mouse over - enlarge image with perhaps another image or two under in little thumbnails. That in my opinion will be tricky to get each "id" to load two or three pictures each and then using JS or Jquery allowing them to double size/enlarge on mouse over.
I have learned in this last month that if I jump all over the place and try to work on too many things at once that my head explodes so I have to one thing at a time now. (search bar script,payment system,getting the buttons to change also on mouse over[JS form issues are keeping me back!], shipping total set to minimum rate and also displayed in cart, shipping calculations for total weight added onto total). -I have some work cut out for me and I made it this far, no turning back now!!