View Single Post
Old 11-25-2012, 04:10 PM   PM User | #3
tonygot
New Coder

 
Join Date: Nov 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
tonygot is an unknown quantity at this point
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 Code:
<?php 
ob_start
();
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors''1');
?>

<?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"];
    
}
$pricetotal$price $each_item['quantity'];
$weighttotal$grams $each_item['quantity'];
$cartWeight$weighttotal $cartWeight;
$cartQty$each_item['quantity'] + $cartQty;
$cartTotal $pricetotal $cartTotal;

setlocale(LC_MONETARY"en_US");
$pricetotal money_format("%10.2n",$pricetotal);

//Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= "<td style=\"color:#206691;\" align=\"center\">".$product_name'<br/><img src="../store/inventory_images/' $item_id '.jpg" width="58" height="59" </td>';  
$cartOutput .= "<td style=\"color:#666;\" align=\"center\">".$subpartnumber."</td>";
$cartOutput .= '<td style=\"color:#666;\" align=\"center\"><form action="cart.php" method="post">
<input name="quantity" type="text" value="'
.$each_item['quantity'].'" size="1" maxlength="1" />
<input name="adjustBtn' 
$item_id '" type="submit" value="Change" />
<input name="quantity_to_adjust" type="hidden" value="' 
$item_id '" /></form></td>';  
//$cartOutput .= "<td style=\"color:#666;\" align=\"center\">".$each_item['quantity']."</td>";  
$cartOutput .= "<td style=\"color:#666;\" align=\"center\">".$weighttotal."(g)</td>";  
$cartOutput .= "<td style=\"color:#666;\" align=\"center\">$".$price."</td>";  
$cartOutput .= "<td style=\"color:#d87921; \" align=\"center\">".$pricetotal."</td>";  
$cartOutput .= '<td style=\"color:#666;\" align=\"center\"><form action="cart.php" method="post"><input name="deleteBtn' $item_id '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' .$i'" /></form></td>';  
$cartOutput .= '</tr>'
    
$i++;    
$loop $i ;// New code to test the zero function STARTS here.
$count $each_item['quantity'];
//session_register('final');  
 
if ($loop == )
 {
 
$_SESSION['final'] = $count
 }
 else if (
$loop )
 {
 
$display $_SESSION['final'];
 
$count2 $display;
 
$display $count2 $count;
 
$_SESSION['final'] = $display;
 }
 
$show $_SESSION['final'];// New code to test the zero function ENDS here.    
    
}
    
setlocale(LC_MONETARY"en_US");
    
$cartTotal money_format("%10.2n",$cartTotal);
    
$cartTotal="".$cartTotal." USD";
// Added to avoid the "Cart" showing errors
mysql_close();
ob_end_flush();
?>

This is the cart.php code which is not having any current issues.
PHP Code:
<?php
ob_start
(); 
session_start();
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors''1');
include_once
"store/storescripts/connect_to_mysql.php";
?>
<?php 
$_SESSION
['final'] = 0;
?>
<?php

[B]I REMOVED LINES 13-104 to show less code that is not relative to the issue.[/B]

$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"];
    
}
$pricetotal$price $each_item['quantity'];
$weighttotal$grams $each_item['quantity'];
$cartWeight$weighttotal $cartWeight;
$cartQty$each_item['quantity'] + $cartQty;
$cartTotal $pricetotal $cartTotal;

setlocale(LC_MONETARY"en_US");
$pricetotal money_format("%10.2n",$pricetotal);

//Dynamic table row assembly
$cartOutput .= "<tr>";
$cartOutput .= "<td style=\"color:#206691;\" align=\"center\">".$product_name'<br/><img src="../store/inventory_images/' $item_id '.jpg" width="58" height="59" </td>';  
$cartOutput .= "<td style=\"color:#666;\" align=\"center\">".$subpartnumber."</td>";
$cartOutput .= '<td style=\"color:#666;\" align=\"center\"><form action="cart.php" method="post">
<input name="quantity" type="text" value="'
.$each_item['quantity'].'" size="1" maxlength="1" />
<input name="adjustBtn' 
$item_id '" type="submit" value="Change" />
<input name="quantity_to_adjust" type="hidden" value="' 
$item_id '" /></form></td>';  
//$cartOutput .= "<td style=\"color:#666;\" align=\"center\">".$each_item['quantity']."</td>";  
$cartOutput .= "<td style=\"color:#666;\" align=\"center\">".$weighttotal."(g)</td>";  
$cartOutput .= "<td style=\"color:#666;\" align=\"center\">$".$price."</td>";  
$cartOutput .= "<td style=\"color:#d87921; \" align=\"center\">".$pricetotal."</td>";  
$cartOutput .= '<td style=\"color:#666;\" align=\"center\"><form action="cart.php" method="post"><input name="deleteBtn' $item_id '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' .$i'" /></form></td>';  
$cartOutput .= '</tr>'
    
$i++;    
$loop $i ;// New code to test the zero function ENDS here.
$count $each_item['quantity'];
//session_register('final');  
 
if ($loop == )
 {
 
$_SESSION['final'] = $count
 }
 else if (
$loop )
 {
 
$display $_SESSION['final'];
 
$count2 $display;
 
$display $count2 $count;
 
$_SESSION['final'] = $display;
 }
 
$show $_SESSION['final'];// New code to test the zero function ENDS here.    
    
}
    
setlocale(LC_MONETARY"en_US");
    
$cartTotal money_format("%10.2n",$cartTotal);
    
$cartTotal="".$cartTotal." USD";
}
ob_end_flush();
?>
tonygot is offline   Reply With Quote