Jump to content

mr_jim

New Members
  • Posts

    9
  • Joined

  • Last visited

    Never

Posts posted by mr_jim

  1. Hi everyone,

     

    Got a problem involving looping around 2 sets of records at the same time and comparing them. Basically I have 2 tables:

    size_ref

    size_id

    size

     

    garment_sizes

    product_id

    size_id

     

    At the moment I loop around 'size_ref' to create a number of checkboxes. I want to loop compare the two recordsets to "check" the checkbox if the value in 'garment_sizes' matches the value in 'size_ref' (they are linked on 'size_id'). I have some code below but it is does not currently work, as I have been playing around with it to try and get this to work: -

     

    <?php
    $querysize = "SELECT * FROM size_ref ORDER BY size_id";
    $resultsize = mysql_query($querysize);
    f ($resultsize){
    } else {
    echo "Error connecting to database. Please contact the administrator of the site.";
    exit();
    }
    
    $querygs = "SELECT DISTINCT size_id FROM garment_sizes WHERE product_id = $pid ORDER BY size_id";
    echo $querygs;
    $resultgs = mysql_query($querygs);
    if ($resultgs){
    } else {
    echo "Error connecting to database. Please contact the administrator of the site.";
    exit();
    }
    
    $i =1;
    echo "<i>(More than one can be selected)</i><table border=\"0\"><tr>";
    
    while ($size = mysql_fetch_array ($resultsize, MYSQL_ASSOC)) {
    $sizeid = $size['size_id'];
    $sizename = $size['size'];
    $check = "";
    
    //I had another loop that went around garment_sizes and checked here, but deleted it as it didnt work. It simply repeated every record. 
    
    if ($i == 2) {
    echo "<td><input type=\"checkbox\" value=\"$sizeid\" name=\"size[]\" $check> $sizename</td></tr>\n";
    $i =1;
    } else {
    echo "<td><input type=\"checkbox\" value=\"$sizeid\" name=\"size[]\" $check> $sizename</td>\n";
    $i = $i + 1;
    }
    
    //The loop ended here
    
    }
    echo "<td> </td></tr></table>";
    mysql_free_result($resultsize);
    mysql_free_result($resultgs);		  
    ?>
    

     

    Please disregard the fact I have no connection etc as this is all done further up the page. Help would be much appreciated.

     

    Jim

  2. Hey, posted something a few days, thought I had fixed it but how little I know. Anyway I am storing information about a shopping basket in a session array. It seemed to work fine on my local machine on apache, but as soon as it was loaded to the server they seem to behave very irratically i.e disappear/reappear when deleted or quantites change. Has anything similar happened to anyone else. Help would be appreciated. Code is below: -

     

    Cheers

     

    Jim

     

    <?php
    
    ob_start();
    session_start();
    
    if(isset($pagetitle)) {
    $pagetitle = 'Tam & Rob';
    }
    
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3c//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3c.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-
    transitional.dtd">
    <html xmlns="http://www.wc3.org/1999/xhtml" xml:lang="en">
    <head>
    <meta http-equiv="content-type" content="text/html ; charset=iso-8859-1" />
    <title>Tam & Rob</title>
    <link href="includes/styles.css" rel="stylesheet" type="text/css">
    </head>
    <!-- End of header -->
    
    <?php
    
    echo '<body>';
    
    include ('includes/menu.html');
    
    //check adding to cart
    if (is_numeric ($_POST['pid'])) {
    $pid= $_POST['pid'];
    $colour = $_POST['colour'];
    $size = $_POST['size'];
    
    	//Create cartitem ID
    	if (isset ($_SESSION['cart'])) {
    		foreach ($_SESSION['cart'] as $key => $value) {
    		if (isset($value)) {
    
    		//new code to make sure key isnt blank
    		if ($key == 0){
    			$key = 1;
    		}
    
    			$i = $key;
    		}
    		              $cart_item = $i + 1;
    		}
    	} else {
    		$cart_item = 1;
    	}
    
    //Check if product exists
    if (isset ($_SESSION['cart'])) {
    foreach ($_SESSION['cart'] as $key => $value) {
     if ($_SESSION['cart'][$key]['pid'] == $pid){
    	if ($_SESSION['cart'][$key]['size'] == $size){
    	      if ($_SESSION['cart'][$key]['colour'] == $colour){
    	      //product does exist so add to quantity
    		$cart_item = $key;
    		     if (isset ($_SESSION['cart'][$cart_item]['qty'])){
    			$qty = $_SESSION['cart'][$cart_item]['qty'] + 1;
    		      }else{
    			$qty = 1;
    		      }
    	      }
    }	
              }
    }
    
    } else {
    $qty = 1;
    }
    
    	if (!isset ($qty)){
    		$qty = 1;
    	}
    if (!isset($cart_item)){
    	$cart_item = 1;
    }
    
    $_SESSION['cart'][$cart_item]['qty']=$qty;
    $_SESSION['cart'][$cart_item]['pid']=$pid;
    $_SESSION['cart'][$cart_item]['size']=$size;
    $_SESSION['cart'][$cart_item]['colour']=$colour;	
    }
    
    //post script which changes value
    if (isset ($_POST['submit'])) {
    
    foreach ($_SESSION['cart'] as $upkey => $upvalue) {
    $qtyup = $_POST['qty'][$upkey];
    if (($qtyup == 0) AND (is_numeric ($qtyup))) {
    	unset ($_SESSION['cart'][$upkey]);
    } elseif (is_numeric($qtyup) AND ($qtyup > 0) ) {
    $_SESSION['cart'][$upkey]['qty'] = $qtyup;
    }
    
    }
    }
    
    //post empty script which empty's cart
    if (isset ($_POST['empty'])) {
    unset ($_SESSION['cart']);
    }
    
    //Check if empty
    $empty = TRUE;
    if (isset ($_SESSION['cart'])) {
    foreach ($_SESSION['cart'] as $key => $value) {
    	if (isset($value)) {
    		$empty = FALSE;
    	}
    }
    }
    
    
    //Cart not empty
    if (!$empty) {
    
    require_once ('includes/config.inc');
    
    require_once('includes/mysql_connect.php');
    
    //Retrieve information
    $query = 'SELECT * FROM garments WHERE product_id IN (';
    foreach ($_SESSION['cart'] as $key => $value) {
    	$query .= $_SESSION['cart'][$key]['pid'] . ',';
    }
    
    $query = substr ($query, 0, -1) . ') ORDER BY style';
    $result = mysql_query($query);
    
    //Create table & form
    echo '<table border="0" width="700px" cellspacing="3" cellpadding="3">
    <tr>
    	<td align="left" width="30%"><b><u>Product Name</u></b></td>
    	<td align="left" width="30%"><b><u>Colour</u></b></td>
    	<td align="left" width="10%"><b><u>Size</u></b></td>
    	<td align="left" width="10%"><b><u>Price</u></b></td>
    	<td align="left" width="10%"><b><u>Qty</u></b></td>
    	<td align="left" width="10%"><b><u>Total Price</u></b></td>
    </tr>
    <form action="view_cart.php" method="POST">
    ';
    
    //Print each item
    $total = 0; //Total Cost of the order
    while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
    
    	//Calculate the total and subtotals.
    	foreach ($_SESSION['cart'] as $key => $value) {
    	if ($_SESSION['cart'][$key]['pid'] == $row['product_id']){
    
    	$subtotal = $_SESSION['cart'][$key]['qty'] * $row['price'];
    	$total += $subtotal;
    
    	echo " <tr>
    	<td align=\"left\">{$row['style']}</td>
    	<td align=\"left\">{$_SESSION['cart'][$key]['colour']}</td>
    	<td align=\"left\">{$_SESSION['cart'][$key]['size']}</td>
    	<td align=\"left\">{$row['price']}</td>
    	<td align=\"left\"><input type=\"text\" size=\"3\" name=\"qty[$key] \" 
    	value=\"{$_SESSION['cart'][$key]['qty']}\" /></td>
    	<td align=\"left\">£" . number_format($subtotal, 2) . "</td></tr>\n";
    		}
    	}
    
    
    } //end while
    
    //Close table
    echo " <tr>
    <td colspan=\"5\" align=\"right\"><b>Total:</b></td>
    <td align=\"left\">£" . number_format($total, 2) . "</td>\n
    </tr>
    </table><div><input type=\"submit\" name=\"submit\" value=\"Update My Cart\"/>\n
    <input type=\"submit\" name=\"empty\" value=\"Empty Cart\"/></form>\n
    <form action=\"checkout.php\" method=\"POST\">
    <input type=\"hidden\" name=\"total\" value=" . number_format($total, 2) .">
    <input type=\"submit\" name=\"submit\" value=\"Checkout\"></form></div>";
    mysql_free_result($result);
    mysql_close();
    ?>
    
    <?php
    } else {
    echo '<p>Your cart is currently empty.</p>';
    }
    
    include_once ('includes/footer.html');
    
    ?>
    

  3. Sorry forgot to include that I am starting the session. The variables work. But it is just my understanding of how to place them in an array that will allow me to loop around each product and display the qty, size and colour. At present I am storing by the Product_id, but each prodduct can appear more than once example:

     

    prodocut_id  product      qty      colour

      1                t-shirt        1        red

      1                t-shirt        1        blue

     

    At the moment the colour is replaced evertime as I am sotring it against the product_id. Is there a better way to store it? Forgive me if I am rambling have been trying to solve this for a good few hours. Thanks for your time.

  4. Hi,

     

    I will post the code below. I wanted to store product_id, colour, size and qty dynamically so I could loop around and populate the shopping basket. Is this possible? Or should I create a mysql table to store the shopping basket information temporarily.

     

    
    <?php
    if (is_numeric ($_POST['pid'])) {
    $pid= $_POST['pid'];
    $colour = $_POST['colour'];
    $size = $_POST['size'];
    
    //Check cart already contains one of these products
    if (isset ($_SESSION['cart'][$pid]['qty'])) {
    	$qty = $_SESSION['cart'][$pid]['qty'] + 1;
    } else {
    	$qty = 1;
    }
    
    //Add to the cart session variable
    $_SESSION['cart'][$pid]['qty'] = $qty;
    $_SESSION['cart'][$pid]['colour'] = $colour;
    $_SESSION['cart'][$pid]['size'] = $size;	
    
    }
    
    //Check if the form has been submitted (to update cart).
    if (isset ($_POST['submit'])) {
    foreach ($_POST['qty'] as $key => $value) {
    	if (($value == 0) AND (is_numeric ($value))) {
    		unset ($_SESSION['cart'][$key]);
    	} elseif (is_numeric($value) AND ($value > 0) ) {
    		$_SESSION['cart'][$key]['qty'] = $value;
    	}
    }
    }
    
    //Check if empty
    $empty = TRUE;
    if (isset ($_SESSION['cart'])) {
    foreach ($_SESSION['cart'] as $key => $value) {
    	if (isset($value)) {
    		$empty = FALSE;
    	}
    }
    }
    
    
    //Cart not empty
    if (!$empty) {
    
    require_once ('includes/config.inc');
    
    require_once('includes/mysql_connect.php');
    
    //Retrieve information
    $query = 'SELECT * FROM garments WHERE product_id IN (';
    foreach ($_SESSION['cart'] as $key => $value) {
    	$query .= $key . ',';
    }
    
    $query = substr ($query, 0, -1) . ') ORDER BY style';
    
    $result = mysql_query($query);
    
    //Create table & form
    echo '<table border="0" width="100%" cellspacing="3" cellpadding="3">
    <tr>
    	<td align="left" width="30%"><b><u>Product Name</u></b></td>
    	<td align="left" width="30%"><b><u>Colour</u></b></td>
    	<td align="left" width="10%"><b><u>Size</u></b></td>
    	<td align="left" width="10%"><b><u>Price</u></b></td>
    	<td align="left" width="10%"><b><u>Qty</u></b></td>
    	<td align="left" width="10%"><b><u>Total Price</u></b></td>
    </tr>
    <form action="view_cart.php" method="POST">
    ';
    
    //Print each item
    $total = 0; //Total Cost of the order
    while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
    
    	//Calculate the total and subtotals.
    	$subtotal = $_SESSION['cart'][$row['product_id']]['qty'] * $row['price'];
    	$total += $subtotal;
    
    	//Print the row
    	echo " <tr>
    	<td align=\"left\">{$row['style']}</td>
    	<td align=\"left\">{$_SESSION['cart'][$row['product_id']]['colour']}</td>
    	<td align=\"left\">{$_SESSION['cart'][$row['product_id']]['size']}</td>
    	<td align=\"left\">{$row['price']}</td>
    	<td align=\"left\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}] \" 
    	value=\"{$_SESSION['cart'][$row['product_id']]['qty']}\" /></td>
    	<td align=\"left\">£" . number_format($subtotal, 2) . "</td></tr>\n";
    
    } //end while
    
    //Close table
    echo " <tr>
    	<td colspan=\"3\" align=\"right\"><b>Total:</b></td>
    	<td align=\"left\">£" . number_format($total, 2) . "</td>\n
    	</tr>
    	</table><div><input type=\"submit\" name=\"submit\" value=\"Update My Cart\"/></form>\n
    	<form action=\"checkout.php\" method=\"POST\">
    	<input type=\"hidden\" name=\"total\" value=" . number_format($total, 2) .">
    	<input type=\"submit\" name=\"submit\" value=\"Checkout\"></form></div>";
    	mysql_close();
    ?>
    
    <?php
    } else {
    echo '<p>Your cart is currently empty.</p>';
    }
    
    include_once ('includes/footer.html');
    
    ?>
    
    

     

    Thanks again for the help.

  5. Hi really struggling to get my head round session variables probably very easy for many of you out there.

     

    I am try to store four values against shopping cart. I want to store the product_id, the size, the colour and the qty ordered.

     

    Are session variables the best option?

     

    currently I have a dynamic loop that stores the product_id ($_SESSION[$product_id]['qty']) as the name of the variable and then stores the quantity against it, but this is no good as the colour and size get replaced everytime.

     

    Help would be greatly appreciated.

  6. Hi I am new to this forum wondered if anyone could help. I am creating a shopping basket and want to store three session variables (quantity, colour, size) against one product ID ($pid). I am trying to modify I page that I already had working that simply stored the quantity and product ID. Help please???

     

    <?php

     

    include_once ('includes/header.html');

     

    echo '<body>';

     

    include ('includes/menu.html');

     

    //check adding to cart

    if (is_numeric ($_POST['pid'])) {

                //These are the variables I am posting to the page and need to store against the SESSION[cart] array

    $pid= $_POST['pid'];

    $colour = $_POST['pid'];

    $size = $_POST['size'];

    echo "{$_SESSION['cart'][$pid]}";

     

    //Check cart already contains one of these products

    if (isset ($_SESSION['cart'][$pid])) {

    $qty = $_SESSION['cart'][$pid] + 1;

    } else {

    $qty = 1;

    }

     

    //Add to the cart session variable

    $_SESSION['cart'][$pid] = $qty;

     

    }

     

    //Check if the form has been submitted (to update cart).

    if (isset ($_POST['submit'])) {

    foreach ($_POST['qty'] as $key => $value) {

    if (($value == 0) AND (is_numeric ($value))) {

    unset ($_SESSION['cart'][$key]);

    } elseif (is_numeric($value) AND ($value > 0) ) {

    $_SESSION['cart'][$key] = $value;

    }

    }

    }

     

    //Check if empty

    $empty = TRUE;

    if (isset ($_SESSION['cart'])) {

    foreach ($_SESSION['cart'] as $key => $value) {

    if (isset($value)) {

    $empty = FALSE;

    }

    }

    }

     

     

    //Cart not empty

    if (!$empty) {

     

    require_once ('includes/config.inc');

     

    require_once('includes/mysql_connect.php');

     

    //Retrieve information

    $query = 'SELECT * FROM garments WHERE product_id IN (';

    foreach ($_SESSION['cart'] as $key => $value) {

    $query .= $key . ',';

    }

    $query = substr ($query, 0, -1) . ') ORDER BY style';

    echo $query;

    $result = mysql_query($query);

     

    //Create table & form

    echo '<table border="0" width="60%" cellspacing="3" cellpadding="3">

    <tr>

    <td align="left" width="30%"><b><u>Product Name</u></b></td>

    <td align="left" width="10%"><b><u>Price</u></b></td>

    <td align="left" width="10%"><b><u>Qty</u></b></td>

    <td align="left" width="10%"><b><u>Total Price</u></b></td>

    </tr>

    <form action="view_cart.php" method="POST">

    ';

     

    //Print each item

    $total = 0; //Total Cost of the order

    while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {

     

    //Calculate the total and subtotals.

    $subtotal = $_SESSION['cart'][$row['product_id']] * $row['price'];

    $total += $subtotal;

     

    //Print the row

    echo " <tr>

    <td align=\"left\">{$row['style']}</td>

    <td align=\"left\">{$row['price']}</td>

    <td align=\"left\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}] \"

    value=\"{$_SESSION['cart'][$row['product_id']]}\" /></td>

    <td align=\"left\">£" . number_format($subtotal, 2) . "</td>\n

    <td><td>{$row['product_id']}</tr>";

     

    } //end while

     

    //Close table

    echo " <tr>

    <td colspan=\"3\" align=\"right\"><b>Total:</b></td>

    <td align=\"left\">£" . number_format($total, 2) . "</td>\n

    </tr>

    </table><div><input type=\"submit\" name=\"submit\" value=\"Update My Cart\"/></form>\n

    <form action=\"checkout.php\" method=\"POST\">

    <input type=\"hidden\" name=\"total\" value=" . number_format($total, 2) .">

    <input type=\"submit\" name=\"submit\" value=\"Checkout\"></form></div>";

    mysql_close();

    ?>

     

    <?php

    } else {

    echo '<p>Your cart is currently empty.</p>';

    }

     

    include_once ('includes/footer.html');

     

    ?>

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.