Validating total Cost

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.php

Response from Assignment 6-2

[/code]

[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]

Unless you really have to use preg_match for validation, you can use is_numeric(), which accomplishes the same thing. preg_match is more useful for validating email addresses and phone numbersIf a string isn’t entered, it won’t enter true or false, it just isn’t there. use if(empty()) instead.

I’m not really sure what you’re trying to validate to get that unit price.

Well, I just went back through the class notes handed out for the assignment. Looks like preg_match is what she wants us to handle.

Validate may not be the right word here. I think I need to declare a variable(four actually) for each of the allowed ProductId ($ID) Prefixes (BB,BG,GB,GG) I want the variable to be set to the price of the product and then multiply that by the Quantity($Qty) ordered and have it show in the table. Right now it calculates, but only for the last $unitPrice. Im sure it is in the way I’m trying to make the meta characters equal the variable of $unitPrice right now. I feel like I’m close. Just can’t seem to wrap my head around the correct operation to handle that part of it. I hope that clarifies it some. Here is the code I’m referring to.

//-----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]$/”;

ok, well since you have to do it with preg_match, how are the id’s being selected? and what does 1 typically look like? it could be that the pattern is off. you may actually want to use a switch instead of a large if statement (unless its required).

If I understand your question correctly; the IDs are being selected in a text box. With the Letters B | G as a set followed by a space or a dash and then 3 numbers between 4 and 8 for example BG 456. My error messages are working correctly so I believe I have the patterns correctly

Is it possible to use the string of meta characters as a string? I thought that might be where I’ve gone astray. I will try the switch. I had one going before but somehow I lost getting the calculation to appear in the form.
Thanks

actually, it sounds like the validation isn’t working, if it was, the right calculation would be taking place. The problem that i see is that your if statements are kinda backwards. You’re trying to match a pattern that hasn’t happened. needs to be changed to something like:
[php]
if (preg_match("/^[BG]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.50;
} elseif (preg_match("/^[BB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.78;
} elseif(preg_match("/^[GB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 4.56;
} elseif(preg_match("/^[GG]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 3.40;
} else {
$ID_error_msg = “Invalid Product ID!”;
}

//-----validate Quantity-----
$Qty = trim($Qty);
$vQty = preg_match("/^[1-9]$/", $Qty); // again, you have to match it before its used, otherwise, its useless to have.

// really not understanding what this for loop is for
for($i=$unitPrice; $i<5; $i++)
$total = number_format($unitPrice * $vQty, 2);
$ID_error_msg = “$total”;
}
[/php]

Its untested, but it should work.

Thanks, this actually helped, a lot! I got rid of the for loop took, your syntax for the preg_match and the calculation appears in the table where it should. I didnot use the $vQty variable though. That just made things disappear. The problem is that I still can only get it to calculate the first $unitPrice no matter what the $Id is (Should be in the format: XX-nnn, where X can be B or G and n is a number between 4 and 8).
I am still working it out. I’ll let you know what happens. thanks again

Ah funny, Sorry should be “between 4 and 8.”

If using vQty caused a problem, then there’s an issue with the match. If it ok, post the form and ill test it out

I hope that is not what it is, because I felt like I had a grip on that. Thanks so much for checking it out for me. It will be nice to have a fresh set of eyes on it. I can’t wait until I’m that good.

This is the form:

[code]

PHP/Web220 Assignment6_1.php

Assignment 6_1

Please Enter the following information

Enter Product ID
Enter Quanity:
Enter Employee ID:
[/code]

This is the validation.

[table]
[tr]
[td]

PHP/Web220 Assignment6_2.php

Response from Assignment 6-2

[/td] [/tr] [/table]

[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}$/”; //I need to match it before its used!!!
//-----validate total-----
//Should be in the format: XX-nnn, where X can be B or G and n is a number between 4 and 8
if(preg_match($validPattern,$ID )==false)
$ID_error_msg=
Invalid Product ID!”;

elseif (preg_match("/^[BG]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.50;}
elseif (preg_match("/^[BB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.78;}
elseif(preg_match("/^[GB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 4.56;}
elseif(preg_match("/^[GG]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 3.40;}
else {
}

 $totalCost = ($unitPrice*$Qty);
 $totalCost= number_format($totalCost,2); 
 $unitPrice = $totalCost;      
 $ID_error_msg .= "$$totalCost";

//-----validate Quantity-----
$Qty = trim($Qty);
$validPattern = “/^[1-9]$/”;//I need to match it before its used!!!

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}$/”; //I need to match it before its used!!!

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]

You entered the following information:

[/code] [php][/php]
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

I have it almost working. I need a valid employee ID to get past that validation. then i can get on to the total cost.

The Employee Id is anything with an X or x then00 and then any six numbers 0-9.

So x00123456 should be a valid employee id? i’ll give it a try when i get home and post the entire script - which is now one page. If its a requirement to have it on 3 pages, i’ll change it back. I just like having it all one page, its easier to troubleshoot :slight_smile:

I cleaned up the code a bit, had a of redundant lines there that really weren’t doing much, and you had some of the validation code in the wrong order, like the group for the quantity, you were validating it after it was already used, now its all in the right place and working properly :slight_smile: like i said, i’ll post what i did and you can decide what to with it :slight_smile:

Yes, that should be valid Id. All I can say is wow! I sure didn’t expect all that! I have to have it as three files. Will I be able to tell how to put it back together? I will compare what I have to yours then so that I can learn from it. Thanks much

Well, i know what’s going on. I’m not sure if its intentional or not, but its matching BG and GB, that’s why you’re getting the larger unitCost. I don’t really understand the patterns, so i don’t know how to fix it, but that is what is happening. It works fine if you do BB and GG.

I didn’t mess with the html part of it, so i won’t give you that. Don’t delete what you have for the the php, just comment it out, then put my stuff in there. that way, if something messes up, you can revert back without having to hit undo. Its always wise to not delete code until the new stuff works. It’ll save you a lot of heachaches and late nights in the future.

validation part
[php]
//-------retrieve Variables from the form-------
$ID = trim($_POST[“ID”]);
$Qty = trim($_POST[“Qty”]);
$EmpID = $_POST[“EmpID”];

print_r($_POST); // this can be removed, i just used it to verify that information was being passed

//-----validate Product ID-----
if(preg_match("/^[BG]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.50;
$ID_error_msg = “BG Product Code OK!”;
} else {
$ID_error_msg = “Invalid BG Product ID!”;
}

if(preg_match("/^[BB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.78;
$ID_error_msg = “BB Product Code OK!”;
} else {
$ID_error_msg = “Invalid BB Product ID!”;
}

if(preg_match("/^[GB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 4.56;
$ID_error_msg = “GB Product Code OK!”;
} else {
$ID_error_msg = “Invalid GB Product ID!”;
}

if(preg_match("/^[GG]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 3.40;
$ID_error_msg = “GG Product Code OK!”;
} else {
$ID_error_msg = “Invalid GG Product ID!”;
}

//-----validate Quantity-----
if(!preg_match("/^[1-9]$/",$Qty)) {
$Qty_error_msg = “** Invalid Quantity**”;
} else {
$Qty_error_msg = “Quantity OK!”;
}

//-----validate Employee ID-----
if(preg_match("/^[X|x]{1}+[0]{2}+[0-9]{6}$/", $EmpID)) {
$EmpID_error_msg = “Employee ID OK!”;
} else {
$EmpID_error_msg = “Invalid Employee ID”;
}

$totalCost = “$”.number_format($unitPrice * $Qty, 2);[/php]

The pattern problem may be something the teacher threw in there to see if you could troubleshoot it, i don’t really know. See if your classmates are having the same issue.

Thanks. It helped some, I think I confused myself along the way between your methods and class notes. I started as you suggested by commenting my stuff out and inserting your code but that messed up my error messagess and I messed something up trying to put them back. However now GB and GG calculate and the others don’t. I will work on it tomorrow and post again good or bad in a day or two after I get it back on track and hopefully solved. If you have anything for me I will check back but I’ll understand if you don’t post until I have something else to show. Thanks so much for your time

The problem is the pattern. [ ] doesn’t designate a literal string, it specifies a range of characters. a product code with BG will pass 3 of the checks, because it contains a b and a g.

Well, i don’t know if this exactly what your teacher had in mind, but its a round-about way of doing it. To keep it from going through all those if statements, i exploded ID to get just the first 2 characters, then i put that into a switch. Then i put all that preg_match stuff in there. You’re still technically using preg_match to validate the entire string. You may actually get props for thinking outside the box :slight_smile:

[php]
//-------retrieve Variables from the form-------
$ID = trim($_POST[“ID”]);
$Qty = trim($_POST[“Qty”]);
$EmpID = trim($_POST[“EmpID”]);

//-----validate Product ID-----//
$d = explode("-", $ID); // puts the contents of $ID into an array
switch($d[0]) { // $d[0] will give us just the first 2 charactars, $d[1] will give us the numbers
case ‘BG’:
if(preg_match("/^[BG]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.50;
$ID_error_msg .= “BG Product Code OK!”;
} else {
$ID_error_msg .= “Invalid BG Product ID!”;
}
break;
case ‘BB’:
if(preg_match("/^[BB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.78;
$ID_error_msg .= “BB Product Code OK!”;
} else {
$ID_error_msg .= “Invalid BB Product ID!”;
}
break;
case ‘GB’:
if(preg_match("/^[GB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 4.56;
$ID_error_msg .= “GB Product Code OK!”;
} else {
$ID_error_msg .= “Invalid GB Product ID!”;
}
break;
case ‘GG’:
if(preg_match("/^[GB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 3.40;
$ID_error_msg .= “GG Product Code OK!”;
} else {
$ID_error_msg .= “Invalid GG Product ID!”;
}
break;
}

//-----validate Quantity-----
if(!preg_match("/^[1-9]$/",$Qty)) {
$Qty_error_msg = “** Invalid Quantity**”;
} else {
$Qty_error_msg = “Quantity OK!”;
}

//-----validate Employee ID-----
if(preg_match("/^[X|x]{1}+[0]{2}+[0-9]{6}$/", $EmpID)) {
$EmpID_error_msg = “Employee ID OK!”;
} else {
$EmpID_error_msg = “Invalid Employee ID”;
}

$totalCost = “$”.number_format($unitPrice * $Qty, 2);
[/php]

This would only work if you knew that the first 2 would always be BG, BB, GB, or GG. if this was a dynamic page, we would have to find another way of doing this.

O Ya!
Still had some tweaking,my code is below.
It was definately the Product Id tripping me up. What is so awesome(to me at least), is that I was trying to do this a few days back and couldn’t get it out right, so I feel better knowing my thinking was on the right path; I just got lost in the brambles. ;D

Thanks so much!

[php]//-----validate total-----
$d = explode("-", $ID); // puts $ID into an array
switch($d[0]) { //$d[0] gives just the first 2 characters, $d[1] gives the numbers
case ‘BG’:
if(preg_match("/^[BG]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.50;
$ID_error_msg .= “$”.number_format(1.50*$Qty,2);
} else {
$ID_error_msg .= “Invalid Product ID!”;
}
break;
case ‘BB’:
if(preg_match("/^[BB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 1.78;
$ID_error_msg .= “$”.number_format(1.78*$Qty,2);
} else {
$ID_error_msg .= “Invalid Product ID!”;
}
break;
case ‘GB’:
if(preg_match("/^[GB]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 4.56;
$ID_error_msg .= “$”.number_format(4.56*$Qty,2);
} else {
$ID_error_msg .= “Invalid Product ID!”;
}
break;
case ‘GG’:
if(preg_match("/^[GG]+[ |-]+[4-8]{3}$/", $ID)) {
$unitPrice = 3.40;
$ID_error_msg .= “$”.number_format(3.40*$Qty,2);
} else {
$ID_error_msg .= “Invalid Product ID!”;
}
break;}[/php]

you can remove the . from the =. i used that to see what was going on with the the messages. Its not going to hurt anything to leave it though.

If you’re going to do the math inside the switch, then you don’t need $unitPrice in there at all. I’ll mark this topic solved for now. If there’s anything else, just post :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service