Jump to content

[SOLVED] Coding help is needed


tauchai83

Recommended Posts

I have the following code:

<?php

if(isset($_SESSION['items_in_sale'])){
    	$num_items=count($_SESSION['items_in_sale']);

	$_POST['item'] = strtoupper($_POST['item']);



		if (count($_SESSION['items_in_sale'])>0){
		   
		  for($k=0;$k<$num_items;$k++)
			{
				$item_info=explode(' ',$_SESSION['items_in_sale'][$k]);
				$item_id=$item_info[0];
				  

				if (($item_id == $_POST['item'])) {
					$AllowUpdate = False;
					echo 'The item' . ' ' . strtoupper($_POST['item']) . ' ' . 'is already on this order' . ' - ' . 'the system will not allow the same item on the list more than once' . '. ' . 'However you can change the quantity by selecting it from the list';
				} //item id==posted item id
			  
			} //end for loop
		}//count
	}//isset





?>

 

I would like to disallow the user from entering the same ITEM on the sales he is making. If the ITEM entered is the same, then they can only update the quantity from the current row of ITEM shown on the screen. I don want the same item appear twice. But my problem is when i enter the different item, it will show the error message, stating I already enter the item twice. Could someone please help me on this coding?

 

I need different approach. I'm lost!

Link to comment
Share on other sites

Try printing out the values you are comparing inside the for loop.  You may find something you don't expect, which will lead you in the right direction to fixing it.

 

You might also want to print_r($_SESSION) or var_dump($_SESSION)

 

Based on only the code you gave, I can't give any specific advice, because there is not enough information.

Link to comment
Share on other sites

the VALUE of the item is the same.

 

for eg, 00001 00001. I did echo it to see whether they are the same. Basically the array value all pass well. Only the method of programming that might wrong.

 

<?php

echo "<center>New Sales</center>";



if(isset($_POST['item']))
{
	$item=$_POST['item'];
	$AllowUpdate = True; /*always assume the best */
	$discount='0%';

	$sql		="select * from product where pd_barcode='$item'";
	$result		=mysql_query($sql);
	$row 		=mysql_fetch_assoc($result);
	$itemPrice	= $row['pd_price'];
	$pd_qty		=$row['pd_qty'];




			if(mysql_num_rows($result)>0)

			{

				if($pd_qty==0){
				echo '<br><br><center><b>Out of Stock! Please Reorder this item</b></center>';
				exit();
				}
				$_SESSION['items_in_sale'][]=$item.' '.$itemPrice.' '.'1';


/*$item_info=explode(' ',$_SESSION['items_in_sale'][$k]);
$item_id=$item_info[0];
foreach ($item_info as $OrderItem) { /*now test for the worst */
				/* do a loop round the items on the order to see that the item
				is not already on this order */
                 


			}
			else
			{
			echo "<center><b>Please enter a valid Item ID!</b></center>";
			}

}

	if(isset($_SESSION['items_in_sale'])){
    	$num_items=count($_SESSION['items_in_sale']);
 	//if ($_POST['item']!='' AND $AllowUpdate==True){  //A stock item has been entered - skip if inputs crook

			$_POST['item'] = strtoupper($_POST['item']);



		if (count($_SESSION['items_in_sale'])>0){
		   
		  for($k=1;$k<$num_items;$k++)
			{
				$item_info=explode(' ',$_SESSION['items_in_sale'][$k]);
				$item_id=$item_info[0];
				  

				if (($item_id == $_POST['item'])) {
					$AllowUpdate = False;
					echo 'The item' . ' ' . strtoupper($_POST['item']) . ' ' . 'is already on this order' . ' - ' . 'the system will not allow the same item on the order more than once' . '. ' . 'However you can change the quantity by selecting it from the order summary';
				} //item id==post item id
			  
			} //end for loop
		}//count
	}//post item and allow update?

	echo $_POST['item']; //try check the value whether it was matched. YES.it matched.
	echo $item_id;



if(isset($_SESSION['items_in_sale']))
{
	$num_items=count($_SESSION['items_in_sale']);

}
else
{
	$num_items=0;
}	
$temp_item_name='';
$temp_item_id='';
$temp_quantity='';
$temp_price='';
$finalSubTotal=0;
$finalTotal=0;
$totalItemsPurchased=0;

$item_info=array();



echo "<hr><center><a href=delete.php?action=all>Clear Sale</a></center>";


echo "<center><form name='select_item' action='sale_ui.php' method='POST'>
	<table border='0' bgcolor='#FFCCFF' align='center'>
<table><tr><td>
<font color='black'><b>Item Barcode: </b></font><input type='text' name='item' size=20'>
<input type='submit'></form></td></tr>


</table>";



echo "<h3 align='center'>List of Items</h3>

<form name='add_sale' action='addsale.php' method='POST'>";
echo "<table border='0' bgcolor='#FFCCFF' cellspacing='0' cellpadding='2' align='center'>
<tr><th>Remove</font></th>
<th>Item Name</font></th>
<th>Unit Price</font></th>
<th>Quantity</font></th>
<th>Total Price</font></th>
<th>Update</font></th>	

</tr>";


for($k=0;$k<$num_items;$k++)
{
	$item_info=explode(' ',$_SESSION['items_in_sale'][$k]);
	$temp_item_id=$item_info[0];

	$sql2="SELECT pd_name FROM product WHERE pd_barcode='$temp_item_id'";
	$result2=mysql_query($sql2);
	$row2=mysql_fetch_assoc($result2);
	$name= $row2['pd_name'];
	$temp_item_name= $name;

	$temp_price=$item_info[1];
	$temp_quantity=$item_info[2];
	$temp_discount=$item_info[3];

	$subTotal=$temp_price*$temp_quantity;
	$rowTotal=$subTotal;
	$rowTotal=number_format($rowTotal,2,'.', '');

	$finalSubTotal+=$subTotal;

	$totalItemsPurchased+=$temp_quantity;

	echo "<tr><td align='center'><a href=delete.php?action=item&pos=$k><font color=black>Delete</font></a></td>
			  <td align='center'><font color='black'><b>$temp_item_name</b></font></td>
			  <td align='center'><input type=text name='price$k' value='$temp_price' size='8'></td>
			  <td align='center'><input type=text name='quantity$k' value='$temp_quantity' size='3'></td>

			  <td align='center'><font color='black'><b>RM $rowTotal</b></font></td>
			  <td align='center'><input type='button' name='updateQuantity$k' value='update' onclick=\"document.add_sale.action='sale_ui.php?update_item=$k';document.add_sale.submit();\"></td>

			  <input type='hidden' name='item_id$k' value='$temp_item_id'>
			  </tr>";
}


$finalSubTotal=number_format($finalSubTotal,2,'.', '');


echo '</table>';


echo "<table align='center' ><br>
<tr><td align='left'><b>Sale SubTotal: RM $finalSubTotal</b></td></tr>";





echo "<br><table border='0' bgcolor='#FFCCFF' align='center'>
<tr>
<td>
<font color='black'>Paid With:</font> 
</td>
<td>
<select name='paid_with'>
<option value='cash'>cash</option>
<option value='check'>check</option>
<option value='credit'>credit</option>

</select>
<font color='black'>Aount Tendered:<input type='text' name='amt_tendered'></font>
</td>
</tr>
<tr>
<td>
<font color='black'>Sale Comment:</font>
</td>
<td>
<input type=text name=comment size=25>
</td>
</tr>

</table>
  <br>
  	  <input type=hidden name='totalItemsPurchased' value='$totalItemsPurchased'>
  	  <input type=hidden name='finalSubTotal' value='$finalSubTotal'>
  <input type=hidden name='confirm' value='1'>
  <center><input type='submit' value='Add Sale'></center></form>";		



  








?>

 

 

Here is the information that you need. Please advise and correct where was my mistake. Thanks.

Link to comment
Share on other sites

I believe the problem may be that you add the item to $_SESSION['items_in_sale'] before you check for duplicates.

 

Also, $k should start at 0 (as in the first code you posted), not 1.  You can check this by using var_dump($_SESSION['items_in_sale']).  That will show you that the first array index is 0.

Link to comment
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.