Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Problem with variable not working in if...else statement


hetecro

Recommended Posts

I am working on a custom POS based on phpPOS that uses the osCommerce database. I have a very simple problem, but I can't figure it out.

 

Basically I have three buttons, buy, sell, and trade, where each button puts in the the value associated with it. This is a snippet from one of the buttons.

 

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

	if(empty($_POST['items']))
	{
		echo "<b>$lang->youMustSelectAtLeastOneItem</b><br>";
		echo "<a href=javascript:history.go()>$lang->refreshAndTryAgain</a>";
		exit();

	}

	$items_to_add=array();
	$items_to_add=$_POST['items'];
	$quantity_to_add=$_POST['quantity'];
	global $sales_type;
	$sales_type=1;

	for($k=0;$k<count($items_to_add);$k++)
	{
		$_SESSION['items_in_sale'][]=$items_to_add[$k].' '.$quantity_to_add.' '.$sales_type;

	}


}	

 

Basically, each button has the $sales_type of 1, 2, or 3.

 

Then, at the value add section, it recalls this number.

 

		  while($row=mysql_fetch_assoc($item_result))
	  {

		$id=$row['products_id'];

		if ($sales_type==3)
		$unit_price=$row['products_price3'];

		elseif ($sales_type==2)
		$unit_price=$row['products_price2'];

		elseif ($sales_type==1)
		$unit_price=$row['products_price'];

	  	$tax_percent=$row['tax_rate'];
	  	$option_value=$id.' '.$unit_price.' '.$tax_percent;
	    $display_item="$row[products_name]";
	 	echo "<option value='$option_value'>$display_item</option>\n";

	  }

 

It works completely if I add $sales_type=1, 2, or 3 right before the $id=$row['products_id']; but it won't pick up the value from the variable in the earlier code. I tried setting the variable to global in each of the switches and inside the while statement, but it still didn't carry the value.

 

Please help, I could lose my job if I don't figure this out.

Link to comment
Share on other sites

I've had a lot better results using the osC session functions.

 

Try replacing this code:

 

  global $sales_type;
 $sales_type = 1;

With:

 

  $sales_type = 1;
 if ( ! tep_session_is_registered('sales_type') ) {
   tep_session_register('sales_type');
 }

Also this code:

 

  if ($sales_type==3)
   $unit_price=$row['products_price3'];
 elseif ($sales_type==2)
   $unit_price=$row['products_price2'];
 elseif ($sales_type==1)
   $unit_price=$row['products_price'];

Could be cleaned up a little like this:

 

  switch ( $sales_type ) {
     case 1:
         $unit_price=$row['products_price'];
         break;
     case 2:
         $unit_price=$row['products_price2'];
         break;
     case 3:
         $unit_price=$row['products_price3'];
         break;
 }

If I suggest you edit any file(s) make a backup first - I'm not perfect and neither are you.

 

"Given enough impetus a parallelogramatically shaped projectile can egress a circular orifice."

- Me -

 

"Headers already sent" - The definitive help

 

"Cannot redeclare ..." - How to find/fix it

 

SSL Implementation Help

 

Like this post? "Like" it again over there >

Link to comment
Share on other sites

I've had a lot better results using the osC session functions.

 

Try replacing this code:

 

  global $sales_type;
 $sales_type = 1;

With:

 

  $sales_type = 1;
 if ( ! tep_session_is_registered('sales_type') ) {
   tep_session_register('sales_type');
 }

Also this code:

 

  if ($sales_type==3)
   $unit_price=$row['products_price3'];
 elseif ($sales_type==2)
   $unit_price=$row['products_price2'];
 elseif ($sales_type==1)
   $unit_price=$row['products_price'];

Could be cleaned up a little like this:

 

  switch ( $sales_type ) {
     case 1:
         $unit_price=$row['products_price'];
         break;
     case 2:
         $unit_price=$row['products_price2'];
         break;
     case 3:
         $unit_price=$row['products_price3'];
         break;
 }

 

 

Unfortunately that isn't working, unless there is something else I am supposted to do to recall the session. I tried a line before the case switch $sales_type=$_SESSION['sales_type']; but I am not sure if I'm doing this right.

Link to comment
Share on other sites

Unfortunately that isn't working, unless there is something else I am supposted to do to recall the session. I tried a line before the case switch $sales_type=$_SESSION['sales_type']; but I am not sure if I'm doing this right.

The only way I can rationally explain that is you're losing the session altogether.

 

That would also explain why your original code wasn't working.

If I suggest you edit any file(s) make a backup first - I'm not perfect and neither are you.

 

"Given enough impetus a parallelogramatically shaped projectile can egress a circular orifice."

- Me -

 

"Headers already sent" - The definitive help

 

"Cannot redeclare ..." - How to find/fix it

 

SSL Implementation Help

 

Like this post? "Like" it again over there >

Link to comment
Share on other sites

The code doesn't make sense ..

 

global $sales_type;

$sales_type=1;

 

You are retrieving $sales_type from the global scope then are setting it manually via $sales_type=1

 

But the main issue in trying to help is that requirements of the code are unclear as you don't show where you retrieve and deal with the session variable $_SESSION['items_in_sale']

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...