$query=mysql_query("SELECT * FROM `scadatel_spin`.`spin` WHERE `tanggal_berangkat`='$tanggal_berangkat' AND `tanggal_kembali`='$tanggal_kembali' ORDER BY `id`");
$data2=mysql_fetch_array($query);
$cek1=$data2['pelaksana1'];
$cek2=$data2['pelaksana2'];
$cek3=$data2['pelaksana3'];
$cek4=$data2['pelaksana4'];
if(($cek1==$pelaksana1) && ($cek2==$pelaksana2) && ($cek3==$pelaksana3) && ($cek4==$pelaksana4)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana2) && ($cek3==$pelaksana4) && ($cek4==$pelaksana3)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana3) && ($cek3==$pelaksana4) && ($cek4==$pelaksana2)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana3) && ($cek3==$pelaksana2) && ($cek4==$pelaksana4)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana4) && ($cek3==$pelaksana2) && ($cek4==$pelaksana3)){
header('location: gagal_input.php');exit;
}
if(($cek1==$pelaksana1) && ($cek2==$pelaksana4) && ($cek3==$pelaksana3) && ($cek4==$pelaksana2)){
header('location: gagal_input.php');exit;
}
If I used three AND operators, It worked properly.
But, If I used more than three AND operators, It could not work properly.
Anybody help me to solve this problem ?
This might work better for what you're trying to do. I am on my phone so please excuse any errors, but pretty much you want to put them all in an array and test if the first value matches the rest:
$query=mysql_query("SELECT * FROM `scadatel_spin`.`spin` WHERE `tanggal_berangkat`='$tanggal_berangkat' AND `tanggal_kembali`='$tanggal_kembali' ORDER BY `id`");
$data2=mysql_fetch_array($query);
$cek1=$data2['pelaksana1'];
$cek2=$data2['pelaksana2'];
$cek3=$data2['pelaksana3'];
$cek4=$data2['pelaksana4'];
$verifyData = array();
$verifyData[] = $cek1;
$verifyData[] = $cek2;
$verifyData[] = $cek3;
$verifyData[] = $cek4;
$allMatch = true;
$firstValue = current($verifyData);
foreach ($verifyData as $val) {
if ($firstValue !== $val) {
$allMatch = false;
}
}
if(!$allMatch){
header('location: gagal_input.php');
exit;
}
Related
if (lo_first == 'true' && $lo_first != '1') {
header("Location: http://example.com/myOtherPage.php");
exit(); }
I don't understand why this redirect doesn't work
Take a look at your if:
if (lo_first == 'true' && $lo_first != '1') {
Here you use both lo_first and $lo_first, which indicates that $lo_first is a variable from which the $ is forgotten at its first use. Fix:
if (lo_first == 'true' && $lo_first != '1') {
I am very new to PHP and trying to get an IF statement to work to change the membership level based on the possibility of multiple answers provided in the registration form. However, it's not working. If anyone can help me understand what I am doing wrong/missing that would be a great help. Here is my code. Thank you.
function my_pmpro_checkout_level($level)
{
if ( ! empty( $_REQUEST['registrationlevel']) && $level->id == '1' ) {
if (( $_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['experience'] != 'morethansixyears') || ($_REQUEST['registrationlevel'] == 'eligibleforlimited' && $_REQUEST['timeassesment'] == 'no')) {
$level->id = '3';
} elseif ( $_REQUEST['registrationlevel'] == 'eligibleforprovisional' && $_REQUEST['experience'] != 'noexperience') {
$level->id = '3'}
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level");
I haven't been able to find a solution to this issue and hope that a another set of eyes may pick up on something. The else $confirm = line is not executed when the first "if" is not true and I can't figure out why.
if($_POST['zip'] && $_POST['country']=='USA' && !empty($fax)){
$plus4 = substr($_POST["zip"],6,4);
if (empty($plus4)) {
$link = 'Zip + 4';
$msg = "Your Zip + 4 was not provided! We cannot fax your Representative without your Zip+4.<br/>Click here to find your ".$link ;
print "<p style=\"color: red;\"><b>$msg<br/><br/></b></p>";
}
if (empty($_POST['state']) || empty($_POST['zip'])) $statezip = false ; // Check for State & Zip Required
else $statezip = true ;
//if (empty($state) || empty($zip) || empty($plus4)) $statezip = false ; // Check for State & Zip Required
$confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($_POST['address']) && !empty($_POST['city']) && !empty($_POST['country']) && $statezip == true ; // Verify required fields
}
else $confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($address) && !empty($city) && !empty($country) && $statezip == true ; // Verify required fields
Your else statement will always return false for $confirm due to the last condition:
&& $statezip == true
You are setting that value in the if statement, so it is undefined in the else statement.
Add your curly braces to the ELSE statement. Since you use them in the defining IF, the parser is expecting them.
else {
$confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($address) && !empty($city) && !empty($country) && $statezip == true ; // Verify required fields
}
use isset to check that variable exist,
function empty return true even if it contain 0 ,
when you are using else use brackets { too
else{do something}
I am trying to do a query like:
If $_GET['page'] == 'items' AND $_GET['action'] == 'new' OR 'edit'
Here's what I have:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && $_GET['action'] == 'new' || isset($_GET['action']) && $_GET['action'] == 'edit') {
// This is what Im looking for
}
}
Is this correct, and is this the easiest way to make this query?
You could have done it like this as well:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
}
}
Your way is perfectly fine, although I would almost be tempted to do it the following way. The only reason I suggest this is that your code requires that both action and page are set. If action is not set then there isn't much point checking if the page is == 'items'.
if(isset($_GET['page']) && isset($_GET['action'])) {
if($_GET['page'] == 'items' && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
//do code here
}
}
You may also try in_array like:
if (isset($_GET['page']) && $_GET['page'] == 'items')
{
if ( !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'new', 'edit' ) )
{
// This is what Im looking for
}
}
That is one of possible solutions
if ( #$_GET['page'] == 'items' && in_array(#$_GET['action'], array('new','edit')))
Everything is ok, but also you can use function:
function paramIs($param, $values) {
$result = false;
foreach ((array)$values as $value) {
$result = $result || isset($_GET[$param]) && $_GET[$param] == $value;
}
return $result;
}
Usage:
if (paramIs('page', 'items') && paramIs('action', array('new', 'edit')))
{
// your code here
}
It will reduce the number of repetitions in your code and encapsulate logic in one place
Given the below variables:
$field1;
$field2;
$field3;
$field4;
$field5;
How can I use logical operators so that the user has to fill in either field: 1,2,3 OR either field 1, 2, 4, 5. If user does not do one of the following, then I want to give error required fields not complete.
I have tried:
if ((!$field1 | !$field2 |!$field3) | (!$field1 | !$field2 |!$field4|!$field5))
$errors[] = 'You did not complete all of the required fields.';
if (!
// Exactly 1,2,3 are filled in (not 4, 5)
(!empty($field1) && !empty($field2) && !empty($field3) && empty($field4) && empty($field5))
// or Exactly 1,2,4,5 are filled in (not 3)
&& !(!empty($field1) && !empty($field2) && !empty($field4) && !empty($field5) && empty($field3))
) {
// print error
}
Let a = not empty $field1, b = not empty $field2, ..., e = not empty $field5
You want
(a && b && c) || (a && b && d && e)
= (a && b) && ( c || (d && e))
In php:
if(!empty($field1) && !empty($field2) &&
( !empty($field3) || (!empty($field4) && !empty($field5))
) {
// process
} else {
// error
}
If you consider 0 as a valid value use isset in place of empty.
Finally get to use those algebra I learned in school.
Try:
if (!empty($field1) && !empty($field2) && !empty($field3)) {
//process
} else if (!empty($field1) && !empty($field2) && !empty($field4) && !empty($field5)) {
//process
} else {
$errors[] = 'You did not complete all of the required fields.';
}
Or, require that no extra fields are filled in for a given option:
if (!empty($field1) && !empty($field2) && !empty($field3) &&
empty($field4) && empty($field5)) {
//process
} else if (!empty($field1) && !empty($field2) && !empty($field4) && !empty($field5) &&
empty($field3)) {
//process
} else {
$errors[] = 'You did not complete all of the required fields.';
}
I am sure where the $fields are comming from, but the following might do the trick:
if (!(isset($field1) && isset($field2) && isset($field3)) || !(isset($field1) && isset($field2) && isset($field3) && isset($field4)){
//Set the error
}
I occasionally do something like the following for simple validations such as this
$fields = array('field1', 'field2', 'field3', 'field4', 'field5');
foreach($fields as $field) {
if(!isset($_POST[$field]) || strlen(trim($_POST[$field])) == 0) {
// set error message
}
}