[56k WARN] Forms and arrays when re-calculating form data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

[56k WARN] Forms and arrays when re-calculating form data

Post by robster »

Hi all,

I have a site/application that I'm making for my wife's hair salon. I didn't want to purchase an app as I thought it'd be a fun project (and it is!)

This part of the app currently takes info from forms on a previous screen and presents it on the screen shown in this image:

Image

I wil post the code below. Currently it works fine, it shows all that needs to be shown, and in particular works out the GST (VAT/Tax/Etc). What happens though, if in this example, the receptionist decided to give the product to the client for $10 off, they would change the 'adjusted price' input, and the colour may have been $120, not $90 as shown, so they would adjust that also. After doing so they would press the 'Re-Calculate' button.

This is all good exept, how the heck do I do that?! I need to re-load the page I know, and I am guessing I need to stick the form data into an array (you think?). Then I need to process the array like I did in the last form and re-present it.

Perhaps looking at my code you might get some clues.

I'd really appreciate some help on what to do here. I'm having a true mental block.

Thanks so much.

Rob

Code: Select all

<? 
include "header.php";
include "menu.php";
include "config.php";


		//Get the passed variables
		$Submit = $_GET['Submit'];
		$client = $_GET['client'];
		$date = $_GET['date'];
		$employee = $_GET['employee'];
		$notes = $_GET['notes'];
		$avail = $_GET['Avail'];
		$select = $_GET['Sel'];
		$avail2 = $_GET['Avail2'];
		$select2 = $_GET['Sel2'];


		$total_price = "0";  //for working out the total price
		$total_gst = "0";  //for working out the total tax

		//Remove the commas and stick the numbers into an array		
		$services = (explode(',', $select, -1));
		$products = (explode(',', $select2, -1));
		
		//Connect to server
		$connection = mysql_connect($dbhost, $dbusername, $dbpassword);
		mysql_select_db($dbname);




		//Get Client Details
		$sql = "SELECT * FROM clients WHERE id = $client";
			$content = mysql_query($sql);
			$Xcontent = mysql_fetch_array($content);	
		
				$client_id = $Xcontent["id"];
				$client_name_first = $Xcontent["name_first"];
				$client_name_last = $Xcontent["name_last"];


				
		
		//Get Employee Details
		$sql = "SELECT * FROM operators WHERE id = $employee";
			$content = mysql_query($sql);
			$Xcontent = mysql_fetch_array($content);	
		
				$employee_id = $Xcontent["id"];
				$employee_name = $Xcontent["name"];



?>
<!-- ##### Main Copy ##### -->
<link href="prosimii-screen.css" rel="stylesheet" type="text/css">

<form action="retail_finalise.php" method="get" name="refill_stock_Add" target="_self">
	
<!-- CUSTOMER DETAILS -->
  <table width="500" border="0" cellspacing="1" cellpadding="1" align="center">
    <tr> 
      <td align="center" class="headerSubTitle">Purchase info for customer <b><? echo "$client_name_first $client_name_last"; ?></b>.</td>
    </tr>
	<tr> 
      <td align="center" class="headerSubTitle">Service supplied by <b><? echo "$employee_name"; ?></b>.</td>
    </tr>
	<?
	if ($notes != "")
	{
		echo "<tr> <td align=\"center\" class=\"clientview\"><b>Notes:</b><? echo \"$notes\"; ?>.</td></tr>";
	}
	?>
	<tr> 
      <td align="center" class="clientview"><b>Date:</b><? echo "$date"; ?></td>
    </tr>	
<?
/* Playing with showing the passed values after editing the $ value */

if ($Submit == "Re-Calculate")
{
	echo "<tr><td class=\"clientviewTitle\"><div align=\"center\">Total Price has been recalculated below</td></tr>";
}
?>	
  </table>
			
<br>
<br>
	
	
	<table width="500" border="0" cellspacing="1" cellpadding="1" align="center">
		<tr> 
			<td colspan="3" align="center" class="clientviewHeader">Please verify SERVICE details before finalising</td>
		</tr>

		<tr>
			<td class="clientviewTitle"><div align="center">Service</div></td>
			<td class="clientviewTitle"><div align="center">RRP</div></td>
			<td class="clientviewTitle"><div align="center">Adjusted Price</div></td>
		</tr>

<?
		// Define the services array
		$services_array = array(array(0,0));

		foreach($services as $val) 
		{
  			
			$sql = "SELECT * FROM services WHERE id = $val";
			$content = mysql_query($sql);
			$Xcontent = mysql_fetch_array($content);	
		
				$service_id = $Xcontent["id"];
				$service_name = $Xcontent["name"];
				$service_description = $Xcontent["description"];
				$service_price = $Xcontent["price"];

				if ($val == $service_id)
				{	
					echo "
					<tr>
					<td class=\"clientview\"><div align=\"center\">$service_name</div></td>
					<td class=\"clientview\"><div align=\"center\">$$service_price</div></td>
					<td class=\"clientview\"><div align=\"center\"><input name=\"service_price".$service_id."\" type=\"text\" value=\"$service_price\" size=\"6\" maxlength=\"7\"></div></td>
					";
					
				//Add up the Totals
				if ($service_price != "")
				{
					$total_price = ($total_price + $service_price);
				}

			}
		}
?>        

		
</table>
<br><br>
	<table width="500" border="0" cellspacing="1" cellpadding="1" align="center">
		<tr> 
			<td colspan="3" align="center" class="clientviewHeader">Please verify PRODUCT details before finalising</td>
		</tr>

		<tr>
			<td class="clientviewTitle"><div align="center">Product</div></td>
			<td class="clientviewTitle"><div align="center">RRP</div></td>
			<td class="clientviewTitle"><div align="center">Adjusted Price</div></td>
		</tr>


<?

		
		foreach($products as $val) 
		{
  			
			$sql = "SELECT * FROM stock_retail WHERE id = $val";
			$content = mysql_query($sql);
			$Xcontent = mysql_fetch_array($content);	
		
				$stock_id = $Xcontent["id"];
				$stock_name = $Xcontent["product_name"];
				$stock_size = $Xcontent["size"];
				$stock_price = $Xcontent["rrp_firstbuy"];

				if ($val == $stock_id)
				{
					echo "
					<tr>
					<td class=\"clientview\"><div align=\"center\">$stock_name - $stock_size</div></td>
					<td class=\"clientview\"><div align=\"center\">$$stock_price</div></td>
					<td class=\"clientview\"><div align=\"center\"><input name=\"stock_price".$stock_id."\" type=\"text\" value=\"$stock_price\" size=\"6\" maxlength=\"7\"></div></td></tr>
					";
					
				//Add up the Totals
				if ($stock_price != "")
				{
					$total_price = ($total_price + $stock_price);
				}
			}
		}
		
				//Show the total price / final price
				$total_gst = ($total_price * $cfg_gst_rate);
				echo "
				<tr>
				<td class=\"clientview\"><div align=\"center\"></div></td>
				<td class=\"clientview\"><div align=\"center\">GST</div></td>
				<td class=\"clientviewHeader\"><div align=\"center\">$$total_gst</div></td></tr>
				<tr>
				<td class=\"clientview\"><div align=\"center\"></div></td>
				<td class=\"clientview\"><div align=\"center\">TOTAL</div></td>
				<td class=\"clientviewHeader\"><div align=\"center\"><strong>$$total_price</strong></div></td></tr>";		

?>        




		
  <tr> 
      <td class="clientviewTitle"><div align="right"></div></td>
    <td align="center">
	<input type="hidden" name="client_name" value="<? echo "$client"; ?>">
	<input type="hidden" name="date" value="<? echo "$date"; ?>">
	<input type="hidden" name="employee_name" value="<? echo "$employee"; ?>">
	<input type="hidden" name="notes" value="<? echo "$notes"; ?>">
	<input type="submit" name="Submit" value="FINALISE">
	</form>
	
	
	<?
	/* ************************************************************************************************************** */	
	/* This form is for the re-calculate, it's really just me mucking around, and I think it's not on the right track */
	/* ************************************************************************************************************** */
	?>
	<form action="" method="get" name="refill_stock_total" target="_self">
	<input type="hidden" name="client" value="<? echo "$client"; ?>">
	<input type="hidden" name="date" value="<? echo "$date"; ?>">
	<input type="hidden" name="employee" value="<? echo "$employee"; ?>">
	<input type="hidden" name="notes" value="<? echo "$notes"; ?>">
	<input type="hidden" name="Avail" value="<? echo "$avail"; ?>">
	<input type="hidden" name="Sel" value="<? echo "$select"; ?>">
	<input type="hidden" name="Avail2" value="<? echo "$avail2"; ?>">
	<input type="hidden" name="Sel2" value="<? echo "$select2"; ?>">
			
	<input type="submit" name="Submit" value="Re-Calculate">
	</form>
	</td>
  </tr>
</table>



<?

include "footer.php";

?>
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

I thought this might help, it shows the previous screen to the one above and has the code.

The code is based on some javascript that I found on the net and really do not fully undertand (but it works ;)).

Basically you select an object in left list box and hit the right arrow and it MOVES it to the right list box. Hit the left arrow and it will move whatever is selected in the right list box back to the left list box.


Image

Code: Select all

<? 
include "header.php";
include "menu.php";
include "config.php";

$today = date("Y-m-d"); 

?>        
		
<SCRIPT LANGUAGE="JavaScript">
<!--
function makeStringFromSelect(selectCtrl) {
	var i;
	var j = 0;
	var outlist = "";

	for (i = 0; i < selectCtrl.options.length; i++) {
		if (j > 0) {
			outlist = outlist + ", ";
		}
		outlist = outlist + selectCtrl.options[i].value;
		j++;
	}
	return outlist;
}

function addItems(fromCtrl, toCtrl) {
	var i;
	var j;
	var itemexists;
	var nextitem;

	// step through all items in fromCtrl
	for (i = 0; i < fromCtrl.options.length; i++) {
		if (fromCtrl.options[i].selected) {
			// search toCtrl to see if duplicate
			j = 0;
			itemexists = false;
			while ((j < toCtrl.options.length) && (!(itemexists))) {
				if (toCtrl.options[j].value == fromCtrl.options[i].value) {
					itemexists = true;
					alert(fromCtrl.options[i].value + " found!");
				}
				j++;
			}
			if (!(itemexists)) {
				// add the item
				nextitem = toCtrl.options.length;
				toCtrl.options[nextitem] = new Option(fromCtrl.options[i].text);
				toCtrl.options[nextitem].value = fromCtrl.options[i].value;
			}
		}
	}
}

function removeItems(fromCtrl) {
	var i = 0;
	var j;
	var k = 0;

	while (i < (fromCtrl.options.length - k)) {
		if (fromCtrl.options[i].selected) {
			// remove the item
			for (j = i; j < (fromCtrl.options.length - 1); j++) {
				fromCtrl.options[j].text = fromCtrl.options[j+1].text;
				fromCtrl.options[j].value = fromCtrl.options[j+1].value;
				fromCtrl.options[j].selected = fromCtrl.options[j+1].selected;
			}
			k++;
		} else {
			i++;
		}
	}
	for (i = 0; i < k; i++) {
		fromCtrl.options[fromCtrl.options.length - 1] = null;
	}
}
//-->
</SCRIPT>

<!-- ##### Main Copy ##### -->
<link href="prosimii-screen.css" rel="stylesheet" type="text/css">

<br>

<FORM NAME="main" ACTION="retail_verify.php" METHOD="GET">
<INPUT TYPE="HIDDEN" NAME="Avail">
<INPUT TYPE="HIDDEN" NAME="Sel">
<INPUT TYPE="HIDDEN" NAME="Avail2">
<INPUT TYPE="HIDDEN" NAME="Sel2">


<table width="500" border="0" cellspacing="2" cellpadding="2" align="center">
  <tr> 
    <td align="center" class="clientviewHeader" colspan="3">Record Retail Sales below...</td>
  </tr>
  <tr> 
    <td align="center" class="clientviewTitle" colspan="3">
		<select name="client">
        <option value="">Choose a Client</option>
        <?
	include "config.php";
	$connection = mysql_connect($dbhost, $dbusername, $dbpassword);

	$dir = "ASC";
	mysql_select_db($dbname);
	$sql = "SELECT * FROM clients ORDER BY name_first $dir";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$cShowMax = mysql_num_rows($content);

	for ($y=1; $y<=$cShowMax; $y++)
	{ 
		//Get the info from the database and smack it in variables for user later 
		$name_id = $Xcontent["id"];
		$name_first = $Xcontent["name_first"];				
		$name_last = $Xcontent["name_last"];
	
	
	echo "<option value=\"$name_id\">$name_first $name_last</option>";
	
	
		$Xcontent = mysql_fetch_array($content);

	//mysql_free_result($content);
	}


?>
      </select></td>
  </tr>
  
  
  
  <tr> 
    <td align="center" class="clientviewTitle" colspan="3">
		<select name="employee">
        <option value="">Choose an Employee</option>
        <?
	include "config.php";
	$connection = mysql_connect($dbhost, $dbusername, $dbpassword);

	$dir = "ASC";
	mysql_select_db($dbname);
	$sql = "SELECT * FROM operators WHERE active = 1 ORDER BY name $dir";
	$content = mysql_query($sql);
	$Xcontent = mysql_fetch_array($content);	
	$cShowMax = mysql_num_rows($content);

	for ($y=1; $y<=$cShowMax; $y++)
	{ 
		//Get the info from the database and smack it in variables for user later 
		$operator_id = $Xcontent["id"];
		$operator_name = $Xcontent["name"];				
	
	
	echo "<option value=\"$operator_id\">$operator_name</option>";
	
	
		$Xcontent = mysql_fetch_array($content);

	//mysql_free_result($content);
	}


?>
      </select></td>
  </tr>  
 
	<tr>
      	<td align="center" class="clientviewTitle" colspan="3">
			<input type="text" name="date" value="<? echo "$today"; ?>" size="10" maxlength="12" align="absmiddle">
		</td>
  	</tr>
   	<tr>
  		<td align="center" class="clientviewTitle" colspan="3">(year-month-day)</td>
	</tr> 
  
  
  <tr> 
    <td colspan="3" class="clientviewTitle" align="center">
	<img src="images/divider.gif" width="100%" height="1"><br>
	Choose service/s:
	</td>
  </tr>
  <tr> 
    <td align="center" class="clientview">
	Services Available<BR>
		<SELECT MULTIPLE NAME="AvailItems" SIZE="5" style="width: 200px;">
        <?
		include "config.php";
		$connection = mysql_connect($dbhost, $dbusername, $dbpassword);
	
		$dir = "ASC";
		mysql_select_db($dbname);
		$sql = "SELECT * FROM services ORDER BY name $dir";
		$content = mysql_query($sql);
		$Xcontent = mysql_fetch_array($content);	
		$cShowMax = mysql_num_rows($content);
	
		for ($y=1; $y<=$cShowMax; $y++)
		{ 
			//Get the info from the database and smack it in variables for user later 
			$service_id = $Xcontent["id"];
			$service_name = $Xcontent["name"];				
			$service_description = $Xcontent["description"];
			$service_price = $Xcontent["price"];	
		
		echo "<option value=\"$service_id\">$service_name</option>";
		
		
			$Xcontent = mysql_fetch_array($content);
	
		//mysql_free_result($content);
		}
		?>
      </select>
	  </td>



	  	<td>
			<INPUT TYPE="BUTTON" NAME="AddBtn" VALUE="    >>    " OnClick="addItems(this.form.AvailItems, this.form.SelItems); removeItems(this.form.AvailItems);"> <BR>
			<BR>
			<INPUT TYPE="BUTTON" NAME="RemoveBtn" VALUE="    <<    " OnClick="addItems(this.form.SelItems, this.form.AvailItems); removeItems(this.form.SelItems);"> <BR>
	  	</td>

		<TD VALIGN="TOP" class="clientview" align="center">
		Services Recieved<BR>
		<SELECT MULTIPLE NAME="SelItems" SIZE="5" style="width: 200px;">
		</SELECT>
		</TD>


  </tr>
  
  <tr>
	  <td class="clientviewTitle" align="center" colspan="3">
	  		<br>
			<img src="images/divider.gif" width="100%" height="1">
			<br>
			Choose product/s:	  
	  </td>
  </tr>
  
  
  <tr> 
    <td align="center" class="clientview">
	Products Available<BR>
		<SELECT MULTIPLE NAME="AvailItems2" SIZE="5" style="width: 250px;">
        <?
		include "config.php";
		$connection = mysql_connect($dbhost, $dbusername, $dbpassword);
	
		$dir = "ASC";
		mysql_select_db($dbname);
		$sql = "SELECT * FROM stock_retail ORDER BY product_name $dir";
		$content = mysql_query($sql);
		$Xcontent = mysql_fetch_array($content);	
		$cShowMax = mysql_num_rows($content);
	
		for ($y=1; $y<=$cShowMax; $y++)
		{ 
			//Get the info from the database and smack it in variables for user later 
			$product_id = $Xcontent["id"];
			$product_name = $Xcontent["product_name"];				
			$product_size = $Xcontent["size"];
			$product_price = $Xcontent["rrp_firstbuy"];	
		
		echo "<option value=\"$product_id\">$product_name ($product_size)</option>";
		
		
			$Xcontent = mysql_fetch_array($content);
	
		//mysql_free_result($content);
		}
		?>
      </select>
	  </td>



	  	<td>
			<INPUT TYPE="BUTTON" NAME="AddBtn2" VALUE="    >>    " OnClick="addItems(this.form.AvailItems2, this.form.SelItems2); removeItems(this.form.AvailItems2);"> <BR>
			<BR>
			<INPUT TYPE="BUTTON" NAME="RemoveBtn2" VALUE="    <<    " OnClick="addItems(this.form.SelItems2, this.form.AvailItems2); removeItems(this.form.SelItems2);"> <BR>
	  	</td>

		<TD VALIGN="TOP" class="clientview" align="center">
		Products Recieved<BR>
		<SELECT MULTIPLE NAME="SelItems2" SIZE="5" style="width: 250px;">
		</SELECT>
		
		</TD>


  </tr>
  

  <tr>
	  <td class="clientviewTitle" align="center" colspan="3">
       	<br>
		<img src="images/divider.gif" width="100%" height="1">
		<br>
		</td>
  </tr>  
  

  <tr> 
    <td align="center" style="clientviewTitle" colspan="3">
	<INPUT TYPE="BUTTON" VALUE="Record Transaction" 
		OnClick="this.form.Avail.value = makeStringFromSelect(this.form.AvailItems);
				this.form.Avail2.value = makeStringFromSelect(this.form.AvailItems2); 
				this.form.Sel.value = makeStringFromSelect(this.form.SelItems); 
				this.form.Sel2.value = makeStringFromSelect(this.form.SelItems2); 
				this.form.submit();">
			<img src="images/divider.gif" width="100%" height="1">
		<br>

	</td>
  </tr>  
  
  <tr>
	  <td class="clientviewTitle" align="center" colspan="3">
        Transaction Notes (optional) <br>
        <textarea rows="3" cols="35" name="notes"></textarea> </td>
  </tr>  
  
  
  
  <tr> 
    <td colspan="3"><img src="images/divider.gif" width="100%" height="1"></td>
  </tr>

</table>
</form>


<?

include "footer.php";

?>
Post Reply