Jump to content

Recommended Posts

hi guys,

 

the below code generates this (see pic):

 

click http://www.dug.scieron.com/casablanca2/show_cart.php

 

my question is how to save the shoesize for each product one selects?  at the moment you can save the quanity for each product, but the shoe size is not.  this is been perplexing me for some days, if can help me please do.

 

show_cart.php

function display_cart($cart, $change = true, $images = 1) 
{ 
//Basic Flow:
/*
1:loop through each iteam and pass the product number of each item to get_book_details() 
so that we can summerize the info of each book.
2:provide an image for each book if one exits
3:if we call function with the change parameter set to true (or not it defaults to true), show box with the 
quantities in them as a form with the save changes button at the end.
*/


  // display items in shopping cart 
  // optionally allow changes (true or false) 
  // optionally include images (1 - yes, 0 - no) 

  global $HTTP_SESSION_VARS; 
   global $HTTP_GET_VARS;

  echo '<table border = 1 bordercolor = #000000 width = 100% cellspacing = 0> 
        <form action = show_cart.php method = post> 
        <tr> 
        <th colspan = '. (1+$images) .' bgcolor="#cccccc">Item</th> 
        <th bgcolor="#cccccc">Price</th> 
        <th bgcolor="#cccccc" align="left">Quantity</th>'; 
	echo'<th bgcolor="#cccccc" align="left">Shoesize</th>';
   echo'  <th bgcolor="#cccccc">Total</th></tr>'; 

  //display each item as a table row 
  foreach ($cart as $prdnumber => $qty) 
  { 
     
    $products = get_book_details($prdnumber); 
    echo '<tr>'; 
    if($images ==true) 
    { 
      echo '<td align = left width=5%>'; 
      if (file_exists("images/$prdnumber.jpg")) 
      { 
         $size = GetImageSize('images/'.$prdnumber.'.jpg');   
         if($size[0]>0 && $size[1]>0) 
         { 
           echo '<img src="images/'.$prdnumber.'.jpg" border=0 '; 
           echo 'width = '. $size[0]/3 .' height = ' .$size[1]/3 . '>'; 
         } 
      } 
     
 else 
        echo ' '; 
    	echo '</td>'; 
    } 
           
    echo '<td align = left>'.$products['title'].'</a></td>'; 
    echo '<td align = left>'.number_format($products['price'], 2).'</td>'; 
    echo '<td align = left>'; 

    // if we allow changes, quantities are in text boxes 
    if ($change == true) 
     echo "<input type = text name = \"$prdnumber\" value = \"$qty\" size = 3 align = left>"; 
    else 
    	  echo $qty.'</td>'; 



    // if we allow changes, quantities are in text boxes 
    if ($change == true) 
     echo "<td align = left>
	 <select name=\"$shoesize\">
		<option >--</option>
		<option name=41 value = \"41\">41</option>
		<option name=42 value = \"42\">42</option>
		<option name=43 value = \"43\">43</option>
		<option name=44 value = \"44\">44</option>
		<option name=45 value = \"45\">45</option>
		<option name=46 value = \"46\">46</option>
	</select></td>"; 
   	 
      echo '<td align = left>'.number_format($products['price']*$qty,2)."</td></tr>\n"; 

}

  // display total row 
  echo '<tr> 
          <th colspan = '. (2+$images) .' bgcolor=#cccccc> 
          <th align = left bgcolor=#cccccc> '; 
            echo  $HTTP_SESSION_VARS['items']; 
       		echo' </th>'; 
        
       		echo'<th align = left bgcolor=#cccccc>  '; 
           
       		echo' </th>'; 
       
         	echo'<th align = left bgcolor=#cccccc> 
            '; echo number_format($HTTP_SESSION_VARS['total_price'], 2); 
          	echo'</th> </tr>'; 

  // display save change button 
  if($change == true) 
  { 
    echo '<tr> 
            <td colspan = '. (2+$images) .'> </td> 
            <td align = center> 
              <input type = hidden name = save value = true>   
              <input type = image src = "images/save-changes.gif" 
                     border = 0 alt = "Save Changes"> 
            </td> 
            <td> </td> 
        </tr>'; 
  } 
  echo '</form></table>'; 
} 

 

save_cart.php

/*
if a user has come to show_cart.php page by clicking an Add to Cart button add the item tot he cart,
*/ 
  
@ $new = $HTTP_GET_VARS['new']; 

//if user did not have a cart, creat one, an empty one

  if($new) 
  { 
     //second, after we know that cart is set-up we can add the item to it
    if(!isset($HTTP_SESSION_VARS['cart'])) 
    { 
      $HTTP_SESSION_VARS['cart'] = array(); 
      $HTTP_SESSION_VARS['items'] = 0; 
     // $HTTP_SESSION_VARS['shoesize'] = ''; 
      $HTTP_SESSION_VARS['total_price'] ='0.00'; 
       
    } 
    //check whether the item is already in the cart, if it is increment the quanity of that item by one.
if(isset($HTTP_SESSION_VARS['cart'][$new])) 
      	$HTTP_SESSION_VARS['cart'][$new]++; 
    else 
//if not, you add the new item to the cart
      $HTTP_SESSION_VARS['cart'][$new] = 1; 
  //work out the total price and number of items in the cart.  for this we use function
//calculate_price() and calculate_iem()
      $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']); 
      $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']); 
     // $HTTP_SESSION_VARS['shoesize'] =  calculate_shoesize($HTTP_SESSION_VARS['cart']); 
     
  } 
  
   //user has come to show cart via save changes button.  in this case user has come via a
//form submission.  if the hidden variable on the form is set we know that the user
//has come from the save chnages button.  this also means that the user has edited the 
//quanity values in the cart and we need to update them.
  if(isset($HTTP_POST_VARS['save'])) 
  {    

   //for each prdnumber in the cart you check the post variable with that name
    foreach ($HTTP_SESSION_VARS['cart'] as $prdnumber => $qty) 
    { 
      //if any of the variables are set to zero, then remove all ietms from the cart    
      if($HTTP_POST_VARS[$prdnumber]=='0') 
  {
        unset($HTTP_SESSION_VARS['cart'][$prdnumber]); 
	}
  else
  { 
   //otherswise update the cart to match the form fields.
        $HTTP_SESSION_VARS['cart'][$prdnumber] = $HTTP_POST_VARS[$prdnumber]; 
	}

    } 
 //after that we again use the calculate_price() and calculate_items() to work 
			  //out the new values of the total_price and item sessions variables.
    $HTTP_SESSION_VARS['total_price'] = calculate_price($HTTP_SESSION_VARS['cart']); 
    $HTTP_SESSION_VARS['items'] = calculate_items($HTTP_SESSION_VARS['cart']); 
   
   
  } 

do_html_header('Your shopping cart'); 
//no matter which page the user has come from display the content of the cart
//if cart has content call display_cart()
  if($HTTP_SESSION_VARS['cart']&&array_count_values($HTTP_SESSION_VARS['cart'])) 
    display_cart($HTTP_SESSION_VARS['cart']); 
  else 
  { 
    echo '<p>There are no items in your cart</p>'; 
    echo '<hr />'; 
  }

 

thanks

 

dan

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/
Share on other sites

This block of code:

		 <select name=\"$shoesize\">
		<option >--</option>
		<option name=41 value = \"41\">41</option>
		<option name=42 value = \"42\">42</option>
		<option name=43 value = \"43\">43</option>
		<option name=44 value = \"44\">44</option>
		<option name=45 value = \"45\">45</option>
		<option name=46 value = \"46\">46</option>
	</select></td>"; 

 

should be:

		 <select name=\"shoesize\">
		<option >--</option>
		<option value=\"41\">41</option>
		<option value=\"42\">42</option>
		<option value=\"43\">43</option>
		<option value=\"44\">44</option>
		<option value=\"45\">45</option>
		<option value=\"46\">46</option>
	</select></td>";

 

You should define the name for the pull down menu within the select tag and not for the individual options. You'd then use $_POST['shoesize'] to get the selected size the customer selected from the list.

 

Also I noticed you are using the old superglobal variables $HTTP_*_VARS. These variables are now depreciated and have been replaced with the new shorter versions, eg: $HTTP_POST_VARS should be $_POST

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-333922
Share on other sites

Thank you.  My select list was a slip (sorry).  my real problem (rather a torture) is assosiated each shoesize posted for every product and saving it to my session, just like i am doing with quanity.  i've tried and tried yet always fail :(

 

could you please help me in trying to resolve that?

 

thx

 

dani

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-333928
Share on other sites

The way you are handling your sessions when saving products to the cart is not adaptable enough to easily integrate the shoe size into the session. The cart session handling is going to have to be modified or prehaps redone.

You are going to have to have a session like the following

 

$_SESSION['cart']['PRODUCT_ID_NUMBER_HERE']['qty']

$_SESSION['cart']['PRODUCT_ID_NUMBER_HERE']['shoesize']

 

Currently it is just:

$_SESSION['cart']['PRODUCT_ID_NUMBER_HERE]

 

which just holds the quantity for the ordered item.

 

In order to save the shoesize to the session you are going to have to change $_SESSION['cart']['PRODUCT_ID_NUMBER_HERE] from holding just the quantity to holding both the shoesize and the quantity. In order to do that $_SESSION['cart']['PRODUCT_ID_NUMBER_HERE] is going to have to store an array. In doing that it'll break your session handling. Due to how your script process the $_SESSION['cart']['PRODUCT_ID_NUMBER_HERE] variable.

 

From looking at your script I think I understand how it works. I'll provide you will the modifications in abit.

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-333952
Share on other sites

OK, I have modified your two functions:

 

Before using my code make sure you backup your orignials. All code below is untested so it may work or it may not.

function display_cart($cart, $change = true, $images = 1)
{
    //Basic Flow:
    /*
    1:loop through each iteam and pass the product number of each item to get_book_details()
    so that we can summerize the info of each book.
    2:provide an image for each book if one exits
    3:if we call function with the change parameter set to true (or not it defaults to true), show box with the
    quantities in them as a form with the save changes button at the end.
    */

    // display items in shopping cart
    // optionally allow changes (true or false)
    // optionally include images (1 - yes, 0 - no)

    echo '
<form action="show_cart.php" method="post>
  <table border="1" bordercolor="#000000" width="100%" cellspacing="0">
    <tr>
      <th colspan="' . $images+1 . '" bgcolor="#cccccc">Item</th>
      <th bgcolor="#cccccc">Price</th>
      <th bgcolor="#cccccc" align="left">Quantity</th>
      <th bgcolor="#cccccc" align="left">Shoesize</th>
      <th bgcolor="#cccccc">Total</th>
    </tr>';

    //display each item as a table row
    foreach ($cart as $prdnumber => $prddetails)
    {
        extract($prddetails);

        $products = get_book_details($prdnumber);

        echo "    <tr>\n";

        if($images == true)
        {
            echo '      <td align="left" width="5%">';

            if (file_exists("images/$prdnumber.jpg"))
            {
                $size = GetImageSize('images/'.$prdnumber.'.jpg');

                if($size[0] > 0 && $size[1] > 0)
                {
                    echo '<img src="images/'.$prdnumber.'.jpg" border=0 ';
                    echo 'width = '. $size[0]/3 .' height = ' .$size[1]/3 . '>';
                }
            }

    	    echo "</td>\n      ";
        }

        echo '<td align"left">'.$products['title']."</a></td>\n      ";
        echo '<td align="left">'.number_format($products['price'], 2)."</td>\n      ";
        echo '<td align="left">';

        // if we allow changes, quantities are in text boxes
        if ($change == true)
            echo "<input type=\"text\" name=\"{$prdnumber}[qty]\" value=\"$qty\" size=\"3\" align=\"left\">";
        else
            echo $qty;

        echo "</td>\n      ";

        // if we allow changes, quantities are in text boxes
        if ($change == true)
        {
        echo '<td align="left">
        <select name="' . $prdnumber . "[shoesize]\">
          <optgroup label=\"sizes\">\n";

            $shoe_sizes = range(40, 46);
            $selected = null;

            foreach($shoe_sizes as $shoe_size)
            {
                if($_SESSION['cart'][$prdnumber]['shoesize'] == $shoe_size)
                {
                    $selected = ' selected="selected"';
                }

                echo '            <option value="' . $shoe_size . '"' . $selected . '>' . $shoe_size . "</option>\n";
             }

             echo '        </optgroup>
        </select>
      </td>

      <td align="left">'.number_format($products['price']*$qty,2)."</td>\n    </tr>\n      ";
        }
}

  // display total row
  echo '<tr>
      <th colspan="'. (2+$images) .'" bgcolor="#cccccc">
      <th align="left" bgcolor="#cccccc">' . $_SESSION['items'] . '</th>
      <th align="left" bgcolor="#cccccc"> </th>
      <th align="left" bgcolor="#cccccc">' . number_format($_SESSION['total_price'], 2) . "</th>
    </tr>\n    ";

    // display save change button
    if($change == true)
    {
        echo '<tr>
      <td colspan="'. (2+$images) .'"></td>
      <td align="center">
        <input type="hidden" name="save" value="true">
        <input type="image" src="images/save-changes.gif" border="0" alt="Save Changes">
      </td>
      <td> </td>
    </tr>';
    }

    echo "\n  </table>\n</form>";
}

 

// if a user has come to show_cart.php page by clicking an Add to Cart button add the item tot he cart,

//if user did not have a cart, creat one, an empty one
if(isset($_GET['new']) && is_numeric($_GET['new']))
{
    $prd_id = $_GET['new'];

    //second, after we know that cart is set-up we can add the item to it
    if(!isset($_SESSION['cart']))
    {
        $_SESSION['cart']        = array();
        $_SESSION['items']       = 0;
        $_SESSION['total_price'] = 0.00;
    }

    //check whether the item is already in the cart, if it is increment the quanity of that item by one.
if(isset($_SESSION['cart'][$prd_id]))
    {
      	$_SESSION['cart'][$prd_id]['qty']++;
    }
    else
    {
        //if not, you add the new item to the cart
        $_SESSION['cart'][$prd_id] = 1;
    }

//work out the total price and number of items in the cart.  for this we use function
//calculate_price() and calculate_iem()
    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items']       = calculate_items($_SESSION['cart']);
}

//user has come to show cart via save changes button.  in this case user has come via a
//form submission.  if the hidden variable on the form is set we know that the user
//has come from the save chnages button.  this also means that the user has edited the
//quanity values in the cart and we need to update them.
if(isset($_POST['save']))
{
   //for each prdnumber in the cart you check the post variable with that name
    foreach ($_SESSION['cart'] as $prdnumber => $product_details)
    {
        extract($product_details);

        //if any of the variables are set to zero, then remove all ietms from the cart
        if($_POST[$prdnumber] == '0')
        {
            unset($_SESSION['cart'][$prdnumber]);
	}
        else
        {
            //otherswise update the cart to match the form fields.
            $_SESSION['cart'][$prdnumber]['qty'] = $_POST[$prdnumber]['qty'];
            $_SESSION['cart'][$prdnumber]['shoesize'] = $_POST[$prdnumber]['shoesize'];
	}

    }

    //after that we again use the calculate_price() and calculate_items() to work
//out the new values of the total_price and item sessions variables.
    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items']       = calculate_items($_SESSION['cart']);
}


do_html_header('Your shopping cart');

//no matter which page the user has come from display the content of the cart
//if cart has content call display_cart()
if($_SESSION['cart'] && array_count_values($_SESSION['cart']))
{
    display_cart($_SESSION['cart']);
}
else
{
    echo '<p>There are no items in your cart</p><hr />';
}

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334005
Share on other sites

OK, I have modified your two functions:

 

Before using my code make sure you backup your orignials. All code below is untested so it may work or it may not.

function display_cart($cart, $change = true, $images = 1)
{
    //Basic Flow:
    /*
    1:loop through each iteam and pass the product number of each item to get_book_details()
    so that we can summerize the info of each book.
    2:provide an image for each book if one exits
    3:if we call function with the change parameter set to true (or not it defaults to true), show box with the
    quantities in them as a form with the save changes button at the end.
    */

    // display items in shopping cart
    // optionally allow changes (true or false)
    // optionally include images (1 - yes, 0 - no)

    echo '
<form action="show_cart.php" method="post>
  <table border="1" bordercolor="#000000" width="100%" cellspacing="0">
    <tr>
      <th colspan="' . $images+1 . '" bgcolor="#cccccc">Item</th>
      <th bgcolor="#cccccc">Price</th>
      <th bgcolor="#cccccc" align="left">Quantity</th>
      <th bgcolor="#cccccc" align="left">Shoesize</th>
      <th bgcolor="#cccccc">Total</th>
    </tr>';

    //display each item as a table row
    foreach ($cart as $prdnumber => $prddetails)
    {
        extract($prddetails);

        $products = get_book_details($prdnumber);

        echo "    <tr>\n";

        if($images == true)
        {
            echo '      <td align="left" width="5%">';

            if (file_exists("images/$prdnumber.jpg"))
            {
                $size = GetImageSize('images/'.$prdnumber.'.jpg');

                if($size[0] > 0 && $size[1] > 0)
                {
                    echo '<img src="images/'.$prdnumber.'.jpg" border=0 ';
                    echo 'width = '. $size[0]/3 .' height = ' .$size[1]/3 . '>';
                }
            }

    	    echo "</td>\n      ";
        }

        echo '<td align"left">'.$products['title']."</a></td>\n      ";
        echo '<td align="left">'.number_format($products['price'], 2)."</td>\n      ";
        echo '<td align="left">';

        // if we allow changes, quantities are in text boxes
        if ($change == true)
            echo "<input type=\"text\" name=\"{$prdnumber}[qty]\" value=\"$qty\" size=\"3\" align=\"left\">";
        else
            echo $qty;

        echo "</td>\n      ";

        // if we allow changes, quantities are in text boxes
        if ($change == true)
        {
        echo '<td align="left">
        <select name="' . $prdnumber . "[shoesize]\">
          <optgroup label=\"sizes\">\n";

            $shoe_sizes = range(40, 46);
            $selected = null;

            foreach($shoe_sizes as $shoe_size)
            {
                if($_SESSION['cart'][$prdnumber]['shoesize'] == $shoe_size)
                {
                    $selected = ' selected="selected"';
                }

                echo '            <option value="' . $shoe_size . '"' . $selected . '>' . $shoe_size . "</option>\n";
             }

             echo '        </optgroup>
        </select>
      </td>

      <td align="left">'.number_format($products['price']*$qty,2)."</td>\n    </tr>\n      ";
        }
}

  // display total row
  echo '<tr>
      <th colspan="'. (2+$images) .'" bgcolor="#cccccc">
      <th align="left" bgcolor="#cccccc">' . $_SESSION['items'] . '</th>
      <th align="left" bgcolor="#cccccc"> </th>
      <th align="left" bgcolor="#cccccc">' . number_format($_SESSION['total_price'], 2) . "</th>
    </tr>\n    ";

    // display save change button
    if($change == true)
    {
        echo '<tr>
      <td colspan="'. (2+$images) .'"></td>
      <td align="center">
        <input type="hidden" name="save" value="true">
        <input type="image" src="images/save-changes.gif" border="0" alt="Save Changes">
      </td>
      <td> </td>
    </tr>';
    }

    echo "\n  </table>\n</form>";
}

 

// if a user has come to show_cart.php page by clicking an Add to Cart button add the item tot he cart,

//if user did not have a cart, creat one, an empty one
if(isset($_GET['new']) && is_numeric($_GET['new']))
{
    $prd_id = $_GET['new'];

    //second, after we know that cart is set-up we can add the item to it
    if(!isset($_SESSION['cart']))
    {
        $_SESSION['cart']        = array();
        $_SESSION['items']       = 0;
        $_SESSION['total_price'] = 0.00;
    }

    //check whether the item is already in the cart, if it is increment the quanity of that item by one.
if(isset($_SESSION['cart'][$prd_id]))
    {
      	$_SESSION['cart'][$prd_id]['qty']++;
    }
    else
    {
        //if not, you add the new item to the cart
        $_SESSION['cart'][$prd_id] = 1;
    }

//work out the total price and number of items in the cart.  for this we use function
//calculate_price() and calculate_iem()
    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items']       = calculate_items($_SESSION['cart']);
}

//user has come to show cart via save changes button.  in this case user has come via a
//form submission.  if the hidden variable on the form is set we know that the user
//has come from the save chnages button.  this also means that the user has edited the
//quanity values in the cart and we need to update them.
if(isset($_POST['save']))
{
   //for each prdnumber in the cart you check the post variable with that name
    foreach ($_SESSION['cart'] as $prdnumber => $product_details)
    {
        extract($product_details);

        //if any of the variables are set to zero, then remove all ietms from the cart
        if($_POST[$prdnumber] == '0')
        {
            unset($_SESSION['cart'][$prdnumber]);
	}
        else
        {
            //otherswise update the cart to match the form fields.
            $_SESSION['cart'][$prdnumber]['qty'] = $_POST[$prdnumber]['qty'];
            $_SESSION['cart'][$prdnumber]['shoesize'] = $_POST[$prdnumber]['shoesize'];
	}

    }

    //after that we again use the calculate_price() and calculate_items() to work
//out the new values of the total_price and item sessions variables.
    $_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    $_SESSION['items']       = calculate_items($_SESSION['cart']);
}


do_html_header('Your shopping cart');

//no matter which page the user has come from display the content of the cart
//if cart has content call display_cart()
if($_SESSION['cart'] && array_count_values($_SESSION['cart']))
{
    display_cart($_SESSION['cart']);
}
else
{
    echo '<p>There are no items in your cart</p><hr />';
}

 

i am now getting this error:

1" bgcolor="#cccccc">Item Price Quantity Shoesize Total 
Warning: extract() [function.extract]: First argument should be an array in C:\webs\phpfiles\casablanca2\output_fns.php on line 295


line 295 >>  extract($prddetails);

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334016
Share on other sites

Umm, looks like the session isn't being rebuilt properly. Change the following line in save_cart.php:

display_cart($_SESSION['cart']);

to this:

// display_cart($_SESSION['cart']);

echo '<h1>Session Data</h1>';
die( '<pre>' . print_r($_SESSION['cart'], true) . '</pre>'

 

This will break your code. But it's only temporary. Its just to see how the session is being populated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334022
Share on other sites

Hehe... I missed a line. The following line in save_cart.php:

        //if not, you add the new item to the cart
        $_SESSION['cart'][$prd_id] = 1;

Should of been:

        //if not, you add the new item to the cart
        $_SESSION['cart'][$prd_id]['qty'] = 1;

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334031
Share on other sites

i am now getting this error

Fatal error: Unsupported operand types in C:\webs\phpfiles\casablanca2\book_fns.php on line 87

 

this i pointing to my functions for calculate_price()

$_SESSION['total_price'] = calculate_price($_SESSION['cart']);

 

 

calculate_price()

function calculate_price($cart)
{
  // sum total price for all items in shopping cart
  $price = 0.0;
  if(is_array($cart))
  {
    $conn = db_connect();
    foreach($cart as $prdnumber => $qty)
    {  
      $query = "select price from products where prdnumber='$prdnumber'";
      $result = mysql_query($query);
      if ($result)
      {
        $item_price = mysql_result($result, 0, 'price');
line 87 >>        $price +=$item_price*$qty;
      }
    }
  }
  return $price;
}

 

 

i just hope we can resolve this i've been having nightmares  :-\

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334037
Share on other sites

Try:

function calculate_price($cart)
{
    // sum total price for all items in shopping cart
    $price = 0;

    if(is_array($cart))
    {
        $conn = db_connect();

        foreach($cart as $prdnumber => $prddetails)
        {
            extract($prddetails);

            $query = "select price from products where prdnumber='$prdnumber'";

            $result = mysql_query($query);

            if ($result)
            {
                $item_price = mysql_result($result, 0, 'price');
                $price += $item_price*$qty;
            }
        }
    }
    return $price;
}

You may get the same error for calculate_items too, if you do just change the foreach loop to how I have it above (including the extract function too). Also you'll want to change the bottom of save_cart.php (

if($_SESSION['cart'] && array_count_values($_SESSION['cart']))

)

to the following too:

if(isset($_SESSION['cart']) && (count($_SESSION['cart']) > 0))

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334040
Share on other sites

Ohh, looks like my codes now working!

 

However I have just noticed a bug for the shoe sizes drop downmenu where the last size in the in the menu gets selected no matter what value you select in the dropdown. This is because I didn't unset the $selected variable in the display_cart function.

 

Change this block:

                if($_SESSION['cart'][$prdnumber]['shoesize'] == $shoe_size)
                {
                    $selected = ' selected="selected"';
                }

to this:

$selected ($_SESSION['cart'][$prdnumber]['shoesize'] == $shoe_size) ? ' selected="selected"' : null;

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334047
Share on other sites

weee....it works...we almost there the only problem now is that when i click on check out

 

the shoesize is not being displayed and it's not inserting into the db

 

when i click on check out i call

checkout.php

echo'<div id="displayedproducts">';
//include our function set
 include ('book_sc_fns.php');



 do_html_header('Checkout');

 if(isset($_SESSION['cart']) && (count($_SESSION['cart']) > 0))
 {
   display_cart($HTTP_SESSION_VARS['cart'], false, 0);
   display_checkout_form();
 }
 else
   echo '<p>There are no items in your cart</p>';

 

to insert into the db, previously i had this code

foreach($HTTP_SESSION_VARS['cart'] as $prdnumber => $quantity)
 {

   //$orderid = mysql_insert_id();
   $detail = get_book_details($prdnumber);
   $query = "delete from order_items where  orderid = '$orderid' and prdnumber =  '$prdnumber'";
   $result = mysql_query($query);
$shoesize = $HTTP_SESSION_VARS['shoesize'];
$query = "insert into order_items values ('$orderid', '$prdnumber', '$shoesize', ".$detail['price'].", '$quantity')";
   $result = mysql_query($query);
   if(!$result)
     return false;

}
 return $orderid;
}

 

do i have to change the foreach loop?

 

goto this link www.dug.scieron.com/casablanca2/

 

to see what i mean when i say the shoesize is not being displayed, select a product then click on checkout

 

 

many many thanks

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334049
Share on other sites

Try the following:

foreach($_SESSION['cart'] as $prdnumber => $prddetails)
{
    extract($prddetails);

    //$orderid = mysql_insert_id();
    $detail = get_book_details($prdnumber);

    $query = "delete from order_items where  orderid = '$orderid' and prdnumber =  '$prdnumber'";

    $result = mysql_query($query);

$query = "insert into order_items values ('$orderid', '$prdnumber', '$shoesize', ".$detail['price'].", '$qty')";

    $result = mysql_query($query);
}
return $orderid;
}

 

Also make sure you have corrected the bug I mentioned in my post above, about the sticky shoesizes

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334051
Share on other sites

haha...great its now inserting into the db :)

 

but when i replaced this code:

if($_SESSION['cart'][$prdnumber]['shoesize'] == $shoe_size)
                {
                    $selected = ' selected="selected"';
                }

with this:

$selected ($_SESSION['cart'][$prdnumber]['shoesize'] == $shoe_size) ? ' selected="selected"' : null;

 

the list was empty and my buttons disappeared?

 

 

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334061
Share on other sites

Just another bug I found. Another line I forgot to change in save_cart.php is this line:

if($_POST[$prdnumber]=='0')

it should of been:

if($_POST[$prdnumber]['qty'] == '0' || is_empty($_POST[$prdnumber]['qty']))

If you update a product with a quantity of zero and update the cart, the product does not get removed from the list of ordered products.

 

Glad I helped.

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334068
Share on other sites

great that you mentioned that coz i think shoe sieze is not insertig according to what you select i dont know if what you mentioned now was the cause for that...let me have a go at it

Recopy the suggested code again. I didn't copy it correctly, just edited my post after you read it  :(

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334070
Share on other sites

great that you mentioned that coz i think shoe sieze is not insertig according to what you select i dont know if what you mentioned now was the cause for that...let me have a go at it

Recopy the suggested code again. I didn't copy it correctly, just edited my post after you read it  :(

 

i am now getting this error:

 

Fatal error: Call to undefined function is_empty()

Link to comment
https://forums.phpfreaks.com/topic/66648-solved-session/#findComment-334073
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.