Jump to content

I need help with PHP codes


KhaledSalh

Recommended Posts

Hi guys,
I am taking a PHP class and I need your help. I have the instructions below:

1. Create a variable called $validations that stores an empty array.
2. For each data field’s input, make a call to the appropriate validation function for the field and store the result an new index in the $validations array.
3. Create a variable called $errors that stores an empty
array.
4.Write a foreach loop for the $validations array. With each iteration, check if the index is empty. If the index is not empty, add the value as a new index of the $errors array.
5. Otherwise, if the form has not been submitted, write statements for th
e following:
6. Initialize a variable for each form input with a value of an empty string.
Example: $first_name = '';

I have written the code, but I am not sure if I got it right, this is the code:

 

$validations = array();

$validatfirst_name = required($first_name, $min , "First Name" );
$validatlast_name = required($last_name, $min , "Last Name" );
$validatemail = valid_email($email, "Email Address");
$validatephone_num = valid_int($phone_num, $min, "Phone");
$validatephone_num2 = valid_int($phone_num2, $min, "Phone");
$validatephone_num3 = valid_int($phone_num3, $min, "Phone");
$validatmessage = required($message, '1', message);

$validations = array($validatfirst_name, $validatlast_name, $validatemail,
$validatephone_num, $validatephone_num2,
$validatephone_num3, $validatmessage);

$errors = array();
foreach($validations as $index=>$validation) {
if(!empty($index)) {
$errors[$index] = $validation;
}
}
if(!isset($_POST['submit'])) {
$first_name = '';
$last_name = '';
$email = '';
$phone_num = '';
$phone_num1 = '';
$phone_num2 = '';
$phone = '';
$message = '';
}
Edited by KhaledSalh
Link to comment
Share on other sites

For #2 you didn't literally follow the instruction, but your end result is exactly the same so I think that'll be fine.

(You were probably intended to put the values directly into $validations without those temporary variables, but like I said it's all the same.)

 

#4 is weird. You did what it said but what it said is... odd. Think about it for a second.

In the foreach loop the $index will be the array key and the $validation will be the value. Right so far. But the index doesn't really do a whole lot. Since you aren't using custom array keys they'll just be numbers: 0, 1, 2, and so on for each item. Checking "if the index is empty" doesn't get you anything. Ooh, it's a number. So?

 

What the instruction should probably be is

Write a foreach loop for the $validations array. With each iteration, check if the value is empty. If the value is not empty, add the value as a new index of the $errors array.

But clear that with your instructor before you change your code.
Link to comment
Share on other sites

You actually need to reverse the start of the script since you have to do default values (empty strings).  Something like

if(!isset($_POST['submit'])) {
   $first_name = '';
   $last_name = '';
   $email = '';
   $phone_num = '';
   $phone_num1 = '';
   $phone_num2 = '';
   $phone = '';
   $message = '';
} else {
   $validations = array();
   $errors = array();
   $validatfirst_name = required($first_name, $min , "First Name" );
   $validatlast_name = required($last_name, $min , "Last Name" );
   $validatemail = valid_email($email, "Email Address");
   $validatephone_num = valid_int($phone_num, $min, "Phone");
   $validatephone_num2 = valid_int($phone_num2, $min, "Phone");
   $validatephone_num3 = valid_int($phone_num3, $min, "Phone");
   $validatmessage = required($message, '1', message);

   $validations = array($validatfirst_name, $validatlast_name, $validatemail,
   $validatephone_num, $validatephone_num2,
   $validatephone_num3, $validatmessage);

   foreach($validations as $index=>$validation) {
      if(!empty($index)) {
         $errors[$index] = $validation;
      }
   }
}

This way, you're doing the validation when the button has been submitted, not when the page is entered.

Edited by richei
Link to comment
Share on other sites

What's your current code?

 

5. Otherwise, if the form has not been submitted...

"Otherwise"? I don't see anything that says "if the form has been submitted then...", which should be somewhere to pair up with this instruction.

 

I agree with Fou-Lu on codingforums.com: the instructions are odd, nevermind that the sequence of steps in richei's code is better than what you're being told to do. I suggest you talk with your teacher and get things straightened out.

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.