Well I think I have what I need working, of course the only area I believe I need help with is the existing sessions script on our site. So I will post the code and ask a few questions and hopefully can get some help with.
<?php
// Don't want links with ampersands in them (XHTML).
ini_set ("arg_separator","&");
// Directory where common include files are stored
define("PHPINCDIR","../../phpinc/");
// This holds the classes used to build the page (Product, Family, Label and System)
require_once(PHPINCDIR.'class_page.php');
/* Set up PHP/mySQL stuff */
require_once(PHPINCDIR.'db.php');
// Functions for printing html
require_once(PHPINCDIR.'html_family.php');
// Misc Quote Functions
require_once(PHPINCDIR."quotefunctions.php");
// Common Page Generation Functions
require_once(PHPINCDIR."html_common.php");
/* End Setup PHP/mySQL stuff */
// Debug
//error_reporting(E_ALL);
//list($susec, $ssec) = explode(" ", microtime());
// Create Session if we are not a known search engine bot
// Avoid passing bots session ids
if (!stristr($HTTP_SERVER_VARS['HTTP_USER_AGENT'] ,'Googlebot') && !stristr($HTTP_SERVER_VARS['HTTP_USER_AGENT'] ,'YahooSeeker') && !stristr($HTTP_SERVER_VARS['HTTP_USER_AGENT'] ,'msnbot') && !stristr($HTTP_SERVER_VARS['HTTP_USER_AGENT'] ,'Yahoo-VerticalCrawler') && !stristr($HTTP_SERVER_VARS['HTTP_USER_AGENT'] ,'Scooter') && !stristr($HTTP_SERVER_VARS['HTTP_USER_AGENT'] ,'slurp')) {
session_cache_limiter('nocache');
//Cart stuff is stored in session variables, sessions used in section_builder code
session_start();
}
/* Check for GET data */
if(!isset($_GET["name"]))
{
// Name is not passed in, create error page
message_page("Error!<br><br>No page name provided.", "Error");
return;
}
/* End GET data check*/
/* Open Winsted DB */
$db_connect = new db();
/* End DB open */
if(!isset($_GET['page'])) {
// No Page number passed in so on page 1
$pagenum = 1;
}
else {
$pagenum = intval($_GET['page']);
if($pagenum < 1) {
$pagenum = 1;
}
}
/* Create page, if doesn't exist, print error */
$current_page = new prod_family($_GET['name']);
$etype = $current_page->get_error();
if($etype !== false) {
// 404 redirect on error
if($etype === 2) {
header("HTTP/1.0 404 Not Found");
message_page($current_page->get_error_msg(), "Error");
return;
}
else {
message_page($current_page->get_error_msg(), "Error");
return;
}
}
// Create links for clearing cart, previous page
$_SESSION['prev_page'] = "family_sess.php?name=".$current_page->get_familyID();
if(isset($_GET["page"])) {
$_SESSION['prev_page'] .= '&page='.$_GET["page"];
}
// Clear Cart link
$clearcart = $_SESSION['prev_page'];
if(isset($_GET["action"])) {
$_SESSION['prev_page'] .= '&action=update_product';
$clearcart .= '&action=clear';
}
// Update session details here. We need to do this stuff above any html writes, going to use header
// to redirect to page we came from.
// Process cart
if(isset($_GET['action'])) {
// Replace ifs with switch
switch($_GET['action']) {
case "update_product":
if(isset($_POST)) {
// Figure out which form was updated. Either the one at bottom of page or top.
// Each has different prefix for array name
if(isset($_POST['Change_x']) || isset($_POST['getchange_x'])) {
if(isset($_POST['cart_quantity']) && is_array($_POST['cart_quantity'])) {
$numrows = sizeof($_POST['cart_quantity']);
}
else {
$numrows = 0;
}
$cart_array = 'cart';
}
else {
if(isset($_POST['prod_quantity']) && is_array($_POST['prod_quantity'])) {
$numrows = sizeof($_POST['prod_quantity']);
}
else {
$numrows = 0;
}
$cart_array = 'prod';
}
$cart_id = $cart_array.'_id';
$cart_quan = $cart_array.'_quantity';
// If cart is not in session add it
if(!isset($_SESSION['prod_quantity'])) {
$_SESSION['prod_quantity'] = array();
}
// Process POST vatiables, look for dupes.
$cart_ids = array();
for($i=0;$i < $numrows;$i++) {
// Scrub post values
if(isset($_POST[$cart_id][$i])) {
$temp_id = intval($_POST[$cart_id][$i]);
if(isset($_POST[$cart_quan][$i])) {
$temp_quan = intval($_POST[$cart_quan][$i]);
if(isset($_SESSION['prod_quantity'][$temp_id])) {
if($_SESSION['prod_quantity'][$temp_id] != $temp_quan) {
if(isset($cart_ids[$temp_id])) {
// Dupe detected, add quantity?
$cart_ids[$temp_id] += $temp_quan;
}
else {
$cart_ids[$temp_id] = $temp_quan;
}
}
}
else {
if(isset($cart_ids[$temp_id])) {
// Dupe detected, add quantity?
$cart_ids[$temp_id] += $temp_quan;
}
else {
$cart_ids[$temp_id] = $temp_quan;
}
}
}
}
}
$numrows = sizeof($cart_ids);
foreach($cart_ids as $product_id => $quantity) {
if(isset($_SESSION['prod_quantity'][$product_id])) {
// Change quantity to model quantity
// If zero, remove from array
if($_SESSION['prod_quantity'][$product_id] != $quantity) {
if($quantity > 0) {
$_SESSION['prod_quantity'][$product_id] = $quantity;
}
// If quantity = 0 remove from array
else {
unset($_SESSION['prod_quantity'][$product_id]);
}
}
}
else {
// New model, add to end of array if it has quantity
if($quantity > 0) {
// Check legality of model number, avoid injection attack
$_SESSION['prod_quantity'][$product_id] = $quantity;
}
}
}
unset($cart_ids);
// Redirect to quote page if we are getting quote, end script
// We are checking which button was used to submit
if(isset($_POST['getchange_x'])) {
session_write_close();
// For 1.0 http clients send out default 302 status
if(0 == strcmp($_SERVER['SERVER_PROTOCOL'],'HTTP/1.1')) {
header("HTTP/1.1 303 See Other");
}
header('Location: http://'.$_SERVER['HTTP_HOST'].'/winstedSQL/quote_sess.php'); /* Redirect browser */
exit;
}
unset($cart_ids);
}
break;
// Nuke cart
case "clear":
unset($_SESSION['prod_quantity']);
break;
}
}
else {
// No action added yet so add one
$_SESSION['prev_page'] .= "&action=update_product";
$clearcart .= "&action=clear";
}
// Target
$_SESSION['prev_page'] .= "#quote";
$clearcart .= "#quote";
// Create cart
if(!isset($_SESSION['prod_quantity'])) {
$_SESSION['prod_quantity'] = array();
}
// Now we build the page, this uses cart variables for quantities
$retval = $current_page->build_page($pagenum);
if($retval === false) {
message_page($current_page->get_error_msg(), "Error");
return;
}
$cart = array();
$i = 0;
// Fill out cart
foreach ($_SESSION['prod_quantity'] as $id => $quantity) {
// Get information for prod
$query = 'SELECT model,little_desc FROM product WHERE id = '.intval($id);
$prod = mysql_query($query) or die("Query failed");
$desc = mysql_fetch_array($prod, MYSQL_ASSOC);
$cart[$i][0] = intval($id);
$cart[$i][1] = intval($quantity);
$cart[$i][2] = $desc['model'];
$cart[$i][3] = $desc['little_desc'];
$i++;
}
// Print html for page
/* Gzip, caching and search engine robot stuff */
// Gzip up stuff, MSN doesn't seem to handle so don't
if(!stristr($HTTP_SERVER_VARS['HTTP_USER_AGENT'] ,'msnbot')) {
ob_start("ob_gzhandler");
}
print_html($current_page, $cart, $_SESSION['prev_page'], $clearcart);
// End Script
// Flush cache, MSN isn't so smart it seems
if(!stristr($HTTP_SERVER_VARS['HTTP_USER_AGENT'] ,'msnbot')) {
ob_end_flush();
}
// DEBUG
// list($fusec, $fsec) = explode(" ", microtime());
// $ttime = (float)(((float)$fsec + (float)$fusec)) - (float)(((float)$ssec + (float)$susec));
// system('echo "Total time: '.$ttime.'" >> /tmp/results.txt');
?>
1) I have two text fields called prod_quantity and another called model. Now since I realy know nothing about sessions (keep in mind the existing work wasnt done by me)
1) How do I incorporate those 2 text fields into this script so customers can keep adding items to their cart?
2) One problem I seem to have with incorporating my form to use this script is that this script looks for a page name. How do I set up this script (or my page if thats the case) to get around that?
3) I believe there are things in this script I may not need.But for any of you who cal help ill tell you what I do need. All I need from this script as far as my form goes is to handle the "prod_quantity" and the "model" numbers that the customer enters and for it to handle those.
Thank you to anyone who can help me out with this it will be greatly appreciated.