-2

Hello I have a simple script

This is my script, and try this script

 <?php 
        $user_id             = $_REQUEST['user_id'];
        $pid                 = $_REQUEST['pid'];
        $nopr                = $_REQUEST['nopr'];
        $tglpr               = $_REQUEST['tglpr'];
        $uraianpr            = $_REQUEST['uraianpr'];
        $jenispr             = $_REQUEST['jenispr'];
        $nilaipr             = $_REQUEST['nilaipr'];
        $costproject         = $_REQUEST['costproject'];
        $remarkpr            = $_REQUEST['remarkpr'];
        $tmpattachid         = $_REQUEST['tmpattachid'];

if ($user_id==NULL)
{
  $error "User Id Not Complete";
}
else if ($pid==NULL)
{
  $error= "PID Not Complete";
}
echo $error;
    ?>

I want if $user_id or other variables is empty/null

i confused when other variable have empty/null data i must typing code many if statement :(

example : 5 variables ($pid,$nopr,$tglpr,$jenispr,$costproject) is empty/null

this show error like this

 PID Not Complete
 NoPR Not Complete
 Tgl Pr Not Complete
 Jenis Pr Not Complete
 Cost Project Not Complete

Help Me, Thank's.

5
  • Loop through $_REQUEST??? Commented Oct 7, 2015 at 17:40
  • yup, if 3 variables null, then show 3 error if 5 variables null, then show 5 error Commented Oct 7, 2015 at 17:43
  • So what's the problem? You don't know how to make a loop? Commented Oct 7, 2015 at 17:44
  • i don't know create looping like this. so can you help me ? Commented Oct 7, 2015 at 17:46
  • 2
    I'm not going to teach you how to write a for loop, no. Open page 1 in your book and go from there. Commented Oct 7, 2015 at 17:48

2 Answers 2

2

I won't give you the full answer, as stated in comments, this is a basic basic concept you need to learn, but I will show you the template.

You want to use a FOREACH loop to check each value in the array. See http://php.net/manual/en/control-structures.foreach.php

And Set it out like this:

foreach($_REQUEST as $key=>$row){
    if (is_null($row) || empty($row)){
     $errorText .= "You have no data in your ".$key."<br>";
     }
}
unset($key,$row);

Then you can output the $errorText value telling people which rows should be fixed. Easy.

Sign up to request clarification or add additional context in comments.

Comments

1

Loop through the $_REQUEST and find the empty or null key.

CODE :

foreach ($_REQUEST as $key => $value) {

    if(trim($value) ==='' || is_null($value))
    {
        echo $key . " is not complete" . "<br/>";
    }

}

1 Comment

you should also tidy up your foreach elements at the end of your loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.