Check if something is an array - php

Currently I have the following issue. I need to figure out how I can check if something is an array.
if(isset($_GET['koophuur']) && $_GET['koophuur'] == 'koop'){
$this->objects->search->koop = true;
$this->objects->search->min_koopprijs = (isset($_GET['min_prijs']) && !empty($_GET['min_prijs'])?$_GET['min_prijs']:null);
$this->objects->search->max_koopprijs = (isset($_GET['max_prijs']) && !empty($_GET['max_prijs'])?$_GET['max_prijs']:null);
}elseif(isset($_GET['koophuur']) && $_GET['koophuur'] == 'huur'){
$this->objects->search->huur = true;
$this->objects->search->min_huurprijs = (isset($_GET['min_prijs']) && !empty($_GET['min_prijs'])?$_GET['min_prijs']:null);
$this->objects->search->max_huurprijs = (isset($_GET['max_prijs']) && !empty($_GET['max_prijs'])?$_GET['max_prijs']:null);
}
if(isset($_GET['koophuurgarage']) && $_GET['koophuurgarage'] == 'koopgarage'){
$this->objects->search->koop = true;
$this->objects->search->min_koopprijs = (isset($_GET['min_prijs']) && !empty($_GET['min_prijs'])?$_GET['min_prijs']:null);
$this->objects->search->max_koopprijs = (isset($_GET['max_prijs']) && !empty($_GET['max_prijs'])?$_GET['max_prijs']:null);
}elseif(isset($_GET['koophuurgarage']) && $_GET['koophuurgarage'] == 'huurgarage'){
$this->objects->search->huur = true;
$this->objects->search->min_huurprijs = (isset($_GET['min_prijs']) && !empty($_GET['min_prijs'])?$_GET['min_prijs']:null);
$this->objects->search->max_huurprijs = (isset($_GET['max_prijs']) && !empty($_GET['max_prijs'])?$_GET['max_prijs']:null);
}
I am not getting these results in my search query right now.

You can use is_array function. it return true if array else false.
is_array($variable);
Also if you need to check if the array is empty or not then
empty($array) : returns true if empty.
If need to check if any key is set or not then you can use
isset($array['key']) or array_key_exists('key', $array)
is_array , array_key_exists

Related

Multiple checkbox validation before sql insert

I have a problem similar to the post:
how to check multiple $_POST variable for existence using isset()?
My issue is, I have a rather large list of checkboxes I need to know if they were checked in order to set the status of $row. it would be exactly like the mentioned script,all and's, except I have and OR thrown in to the works.
if ( isset($_POST['Facial1'])
&& ($_POST['Facial2'])
&& ($_POST['Facial3'])
&& ($_POST['Facial4'])
&& ($_POST['Intra-Oral1'])
&& ($_POST['Intra-Oral2'])
&& ($_POST['Intra-Oral3'])
&& ($_POST['Intra-Oral4'])
&& ($_POST['Intra-Oral5'])
&& ($_POST['Intra-Oral6'])
&& (
(
($_POST['X-Ray1'])
&& ($_POST['X-Ray3'])
)
|| ($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
Each time I run a test, as soon as it gets to an element that is not checked it throws an error: undefined index: xxx. Where xxx is the first none checked item.
Checkboxes that are not checked do not get sent as part of the POST body. Your code needs to use isset:
if ( isset($_POST['Facial1'])
&& isset($_POST['Facial2'])
&& isset($_POST['Facial3'])
&& isset($_POST['Facial4'])
&& isset($_POST['Intra-Oral1'])
&& isset($_POST['Intra-Oral2'])
&& isset($_POST['Intra-Oral3'])
&& isset($_POST['Intra-Oral4'])
&& isset($_POST['Intra-Oral5'])
&& isset($_POST['Intra-Oral6'])
&& (
(
isset($_POST['X-Ray1'])
&& isset($_POST['X-Ray3'])
)
|| isset($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
the isset() allows you to check one variable isset($var) or multiple variables at a time isset($var,$var1,$var2) this will return true is all exists and false if one or more are not set.
Your code is only checking one and then doing well I am not sure what
Try
if ( isset(
$_POST['Facial1'], $_POST['Facial2'], $_POST['Facial3'],
$_POST['Facial4'], $_POST['Intra-Oral1'], $_POST['Intra-Oral2']
$_POST['Intra-Oral3'], $_POST['Intra-Oral4'],
$_POST['Intra-Oral5'], $_POST['Intra-Oral6']
)
&&
(
isset($_POST['X-Ray1'], $_POST['X-Ray3'])
||
isset($_POST['X-Ray5'])
)
)
{
$Status = 1;
} else {
$Status = 0;
}
I hope this is what you intended, its not totally obvious from your code.

How to shorten multiple isset functions in PHP

Look at my script:
if((isset($_POST['unma']) && isset($_POST['livi'])) && (isset($_POST['cont']) &&
isset($_POST['phone']))){
if(
(
(
(
(isset($_POST['fnma']) && isset($_POST['lnma']))
&&
(isset($_POST['occ']) && isset($_POST['hom']))
)
&&
(
(isset($_POST['town']) && isset($_POST['dist']))
&&
(isset($_POST['dispmb']) && isset($_POST['fbc']))
)
)
&&
(
(
(isset($_POST['gp']) && isset($_POST['twt']))
&&
(isset($_POST['ins']) && isset($_POST['flc']))
)
&&
(
(isset($_POST['ile']) && isset($_POST['cam']))
&&
(isset($_POST['cam_co']) && isset($_POST['prof_phr']))
)
)
)
&&
isset($_POST['awards'])
){
// Codes here
}
How can I short this isset functions. I found an solution with foreach but they are not checking the post isset, rather they are checking if the posted values are not empty
You can use
if (isset($_POST['a'], $_POST['b'], $_POST['c'], ...))
and so on. If any of the variables is not set then you will get false.
$expectedKeys = ['fnma', 'lnma', ...];
if (!array_diff_key(array_flip($expectedKeys), $_POST)) {
// all keys are set
}
I think something like this would work:
// add all names of the values that needs to be set in the `$_POST` array
$values = array('fnma', 'lnma');
function checkSet($array) {
foreach($array as $value) {
if(!isset($_POST[$value])) return false;
}
return true;
}
if(checkSet($values)) {
// your code
}
put required fields in a array and check for it using loop,it is easy and easy to add required fields.
$req_values = array('fnma','lnma','occ','hom','town','dist','dispmb','fbc','gp','twt','ins','flc','ile','cam','cam_co','prof_phr');
foreach($req_values as $key) {
if (empty($_POST[$key])) {
echo $key .'is required';
exit;
}
}
Knowing that isset can take multiple values, you could do this:
if (isset($_POST['unma'], $_POST['livi'], $_POST['cont'], $_POST['phone'], $_POST['fnma'], $_POST['lnma'], $_POST['occ'], $_POST['hom'], $_POST['town'], $_POST['dist'], $_POST['dispmb'], $_POST['fbc'], $_POST['gp'], $_POST['tat'], $_POST['ins'], $_POST['flc'], $_POST['ile'], $_POST['cam'], $_POST['cam_co'], $_POST['prof_phr'], $_POST['awards'])) {
// Codes here
}
But that seems nightmarish. I would make the $_POST keys an array & then do the following:
// Set an array of post values.
$post_values = array('unma','livi','cont','phone','fnma','lnma','occ','hom','town','dist','dispmb','fbc','gp','tat','ins','flc','ile','cam','cam_co','prof_phr','awards');
// Set '$post_valid' to an array.
$post_valid = array();
// Roll through the post values.
foreach ($post_values AS $post_key => $post_value) {
// If the value is set, set '$post_valid' to TRUE or else set it to FALSE.
// $post_valid = isset($_POST[$post_key]) ? TRUE : FALSE; // Not really needed. Simple `isset` should work.
$post_valid[$post_key] = !empty($_POST[$post_key]);
}
// Now if '$post_valid' values are all true, do something.
$post_valid_values = array_values($post_valid);
if (!empty($post_valid_values) && (count($post_valid_values) == count($post_values))){
// Codes here
}

How to simplify this code?

I'm just wondering if there's a way to simplify this code?
foreach ($parent_data as $ind_port_record) {
if ( isset($ind_port_record['port_name']) && (strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2') ){
$record_to_include['remote_id'] = $ind_port_record['remote_id'];
$record_to_include['remote_name'] = $ind_port_record['remote_name'];
$record_to_include['remote_object_id'] = $ind_port_record['remote_object_id'];
$record_to_include['remote_object_name'] = $ind_port_record['remote_object_name'];
break;
}
}
//make sure you have something in remote object details
if ( ! isset($record_to_include['remote_id']) ){
$record_to_include['remote_id'] = '';
$record_to_include['remote_name'] = '';
$record_to_include['remote_object_id'] = '';
$record_to_include['remote_object_name'] = '';
}
I just need to make sure the values inside the $record _to_include are not uninitialized or NULL.
Thanks.
First off, simplify the if()
You currently have a lot of conditions
(strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2')
Let's make that check an array
in_array( strtoupper($ind_port_record['port_name'], array('GI/2','G2','GI2')) )
Now to check if $record_to_include are not uninitialized or NULL
Let's loop through the array and do a simple check.
foreach($record_to_include as $record => $value) {
$record_to_include[$record] = is_null($value) OR !isset($record_to_include[$record]) ? '' : $value;
}
$record = array_filter($parentData, function (array $record) {
return isset($record['port_name']) && preg_match('!^(GI/2|G2|GI2)$!i', $record['port_name']);
});
$recordToInclude = $record ? current($record) : array(
'remote_id' => null,
'remote_name' => null,
'remote_object_id' => null,
'remote_object_name' => null
);
$gi_constraint = array('GI/2', 'G2', 'GI2'); // you are checking one and the same variable for different values, so you can use in_array here
foreach ($parent_data as $ind_port_record) {
if (isset($ind_port_record['port_name']) && in_array(strtoupper($ind_port_record['port_name']), $gi_constraint)){
foreach ($ind_port_record as $k=>$v) {
$record_to_include[$k] = $v; // as they have the same keys, you can specify the key and assign to the value of $in_port_record
}
break;
}
}
//make sure you have something in remote object details
if (!isset($record_to_include['remote_id']) ){
foreach ($record_to_include as $k => &$v) {
$v = ''; // when using reference, it will change the original array
}
}
Explanations in the code
Try
$arr = array('remote_id','remote_name','remote_object_id','remote_object_name');
if ( isset($ind_port_record['port_name']) && (strtoupper($ind_port_record['port_name']) == 'GI/2' || strtoupper($ind_port_record['port_name']) == 'G2' || strtoupper($ind_port_record['port_name']) == 'GI2') ){
foreach($arr as $ar){
$record_to_include[$ar] = (isset($ind_port_record[$ar]) && isset($record_to_include['remote_id']))?$ind_port_record[$ar]:NULL;
}
}

How to check xml object is empty or not in php

if ($xml) {
$emailId = $xml->mail->id;
$mailPassword = $xml->mail->password;
if (!empty($emailId) && !empty($mailPassword)) {
$data['emailIdandPasswordCheck'] = 0;
}
}
this is a condition in which we are checking the values are empty or not but it is not working
in this id is empty and password is not empty.
but everytime xml object is coming in both id and password
because of this both the things are coming as non empty.
what check i can use to check xml object is empty or not?
You can use cast an array and you check the condition
$emailId = (array)$emailId;
$mailPassword = (array)$mailPassword;
if (!empty($emailId) && !empty($mailPassword) ){
}
OR
$emailId = (array)$emailId;
$mailPassword = (array)$mailPassword;
if (count($emailId)==0 && count($mailPassword)==0 ){
}
I think you want this:
if ($xml) {
$emailId = $xml->mail->id;
$mailPassword = $xml->mail->password;
if ( $emailId !=null && $emailId !="" && $mailPassword!=null && $mailPassword!="" ) {
$data['emailIdandPasswordCheck'] = 0;
}
}
Use isset() instead of empty()
if (!isset($emailId) && !isset($mailPassword)) {
$data['emailIdandPasswordCheck'] = 0;
}
OK. Here is what your code is doing.
$emailId = $xml->mail->id;
This sets null in $emailId if there is no value.
$mailPassword = $xml->mail->password;
And this sets null in $mailPassword if there is no value.
And your condition says
if emailId is non-empty and mailPassword is non-empty
And using empty always tells the conditions are true. Because empty returns true on null.
So instead using isset will solve the issue.
You can see the comparison table for further info.

make an ifnot statement and if statement in one line

I'm trying to make an if statement with 2 conditions. One that checks if one variable is NOT present & does NOT matches the word "good2go" and the other that checks to make sure "body" variable is present. I'm trying to trip the error message here. Here is what I have and what I've tried, and none of it seems to work.
if (stripos($_POST['check'], 'good2go') == FALSE && $_POST['body']) {
$error = true; }
if (!$_POST['check'] == 'good2go' && $_POST['body']) {
$error = true; }
if (!stripos($_POST['check'], 'good2go') && $_POST['body']) {
$error = true; }
if ((!stripos($_POST['check'], 'good2go')) && $_POST['body']) {
$error = true; }
How do I get this to work?
here's the entire code of contact_us.php this has the validation code and the email code.
$error = false;
if (isset($_GET['action']) && ($_GET['action'] == 'send')) {
// Winnie the pooh check
//$t = tep_db_prepare_input($_POST['verify']);
if (!isset($_POST['check']) && !$_POST['check']=='good2go' && isset($_POST['body'])) {
$error = true;
} else { // Winnie the pooh Check
$name = tep_db_prepare_input($_POST['name']);
$email_address = tep_db_prepare_input($_POST['email']);
//IP recorder start
$ipaddress = $_SERVER["REMOTE_ADDR"];
$ip = "\n\nIP: " . $ipaddress;
$content = "\n\nName: ".$name."\n\nComments: ".$_POST['enquiry'];
$product = tep_db_prepare_input($_POST['product']);
if ($product) {
$product_text = "\n\nProduct Interest: ".$product; }
$content_ip = $content . $product_text. $ip;
$enquiry = tep_db_prepare_input($content_ip);
//IP recorder end
}
// BOF: Remove blank emails
// if (tep_validate_email($email_address)) {
// tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, $enquiry, $name, $email_address);
// tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
// } else {
// $error = true;
// $messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
if (! tep_validate_email($email_address)) {
$error = true;
$messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
}
if ($enquiry == '') {
$error = true;
$messageStack->add('contact', ENTRY_EMAIL_CONTENT_CHECK_ERROR);
}
if ($error == false) {
tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, $enquiry, $name, $email_address);
tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
// EOF: Remove blank emails
}
}
Solution to your updated problem:
if (!isset($_POST['check']) || !$_POST['check']=='good2go' || !isset($_POST['body'])) {
$error = true;
}
The reason for the pipes vs ampersands is that you want to throw an error if ANY of the fields has issue. Also, you want to check if body is NOT set vs IS set. Glad this worked out for you!
and the other that checks to make sure "body" variable is not present.
if(stripos($_POST['check'], "good2go") !== false && !isset($_POST['body'])){
//code here
}
According to PHP docs regarding the stripos function:
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
So you need to change the first line to:
// Doing stripos checks you MUST use === (not ==)
if (stripos($_POST['check'], 'good2go') !== FALSE && $_POST['body']) {
$error = true; }
And to check if there is no $_POST['body'] you can change the above to:
if (stripos($_POST['check'], 'good2go') !== FALSE && (!isset($_POST['body'])) {
-- Update --
According to your comment, you need $_POST['check'] to equal 'good2go', then you shouldn't be using stripos as it will check for the existence of good2go regardless if it's exactly equal, or part of a string; 'wow this hamburger is good2go'.
So I would change the conditional to:
if (((isset($_POST['body'])) && (strlen($_POST['body']) > 0)) && ((!isset($_POST['check'])) || ($_POST['check'] !== 'good2go'))) {
// Post body has a value and Post check DOES NOT equal good2go, someone is hax0rin!
}
You may want to read up on Cross-site request forgery as it seems right inline with what you are working on.
One that checks if one variable is present & matches the word "good2go"
isset($_POST['check']) AND $_POST['check'] == 'good2go'
and the other that checks to make sure "body" variable is not present.
!isset($_POST['body'])
so, just put them together
if (isset($_POST['check']) AND $_POST['check'] == 'good2go' AND !isset($_POST['body'])) {
$error = true;
}
try this:
if(!empty($_POST['check']) && $_POST['check']=='good2go' && empty($_POST['body'])) { $error=true; }
Consider using empty instead of isset if your $_POST['body'] can be present with an empty value.
No need for all those unneeded functions. What you are trying to achieve is:
if (isset($_POST['check']) && $_POST['check']=='good2go' && !isset($_POST['body']) {
// your code
}
However, As per the title of the question: Use a ternary statement. Syntax is as such
$var = <condition> ? <true> : <false>;

Categories