I have an assignment that is supposed to validate a Product ID($ID),Quantity ordered($Qty), and an Employee ID($EmpID) using meta character strings and regex functions. I have that all working(at least it appears that I do). When Product Id and Quantity are valididated The total cost is supposed to appear in the row for the product id under the “Validation Status” colum/field and that is where my problem lies. I seem only able to get it to calculate for the last Product ID. I have tried a switch condition, substring/strstr, and a for loop which is where I am now and gives me the best result, yet it still isn’t right. This file calls variables from another file.
Here is my code for the 1 file. The problem seems to lie in the section I commented out as /----validate total—… /—end validate total.
Thanks in advance to anyone who can help me narrow this down.
[code]
PHP/Web220 Assignment6_2.phpResponse from Assignment 6-2
[php]<?php
//-------retrieve Variables from the form-------
$ID = $_REQUEST [“ID”];
$Qty = $_REQUEST [“Qty”];
$EmpID = $_REQUEST [“EmpID”];
//----declaring variables----
$ID_error_msg = “”;
$Qty_error_msg = “”;
$EmpID_error_msg = “”;
//-----validate Product ID-----
if($ID == false)
$ID_error_msg = "
Product ID cannot be blank";
else
$ID =trim($ID); //removes spaces at lead and trail
$validPattern = “/^[BG|BB|GB|GG]+[ |-]+[4-8]{3}$/”;
//-----validate total-----
if ($validPattern = “/^[BG]+[ |-]+[4-8]{3}$/”){
$unitPrice = 1.50;
}
elseif ($validPattern ="/^[BB]+[ |-]+[4-8]{3}$/") {
$unitPrice = 1.78;
}
elseif($validPattern ="/^[GB]+[ |-]+[4-8]{3}$/"){
$unitPrice = 4.56;
}
else($validPattern ="/^[GG]+[ |-]+[4-8]{3}$/");
$unitPrice = 3.40;
for($i=$unitPrice; $i<5; $i++)
$total=($unitPrice *$Qty);
$total = number_format($total, 2);
if(preg_match($validPattern,$ID))
$ID_error_msg .= "$total";
else {
$ID_error_msg .= "<span style = 'color:red'>"
."**Invalid Product ID!**</span>";
}
//end validate total
//-----validate Quantity-----
$Qty = trim($Qty);
$validPattern = “/^[1-9]$/”;
if(preg_match($validPattern,$Qty)==false)
$Qty_error_msg ="<span style = 'color:red'>
** Invalid Quantity**</span>";
else
$Qty_error_msg = "Quantity OK!";
//-----validate Employee ID-----
$EmpID = trim($EmpID);
$validPattern = “/^[X|x]{1}+[0]{2}+[0-9]{6}$/”;
if(preg_match($validPattern,$EmpID))
$EmpID_error_msg =
"Employee ID OK";
else
$EmpID_error_msg =
"<span style = 'color:red'>**Invalid Employee ID**</span>";
?>[/php]
<[code]div style=“text-align:center;”>
You entered the following information:
Form Field | Value | Validation Status |
---|---|---|
Product ID | <?php echo $ID ?> | <?php echo $ID_error_msg?> |
Quantity | <?php echo $Qty?> | <?php echo $Qty_error_msg?> |
EmployeeID | <?php echo $EmpID?> | <?php echo $EmpID_error_msg?> |
BB: 2.20 BG: 1.78 GB: 4.56 GG: 3.40
[/code] [php]<?php include("web220_footer.html");?>[/php]