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
}
Related
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
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.
I'm trying to find a smarter way to validate my inputs with PHP. If the array finds an empty field, it has to add a new element to the array and display an error message.
So far, I haven't succeeded.
The code behind
$felter = array();
if(isset($_POST['submit'])) {
$produktnavn = $_POST['produktnavn'];
$kategori = $_POST['kategori'];
if( !empty( $felter ) ) {
foreach ($felter as $felt) {
if ($felter == '') {
$fejl = true;
}
}
}
else {
$sql = "UPDATE produkt SET produkt_navn = '$produktnavn', fk_kategori_id = '$kategori' WHERE produkt_id=$id";
mysqli_query($db, $sql);
echo "Produktet blev opdateret";
}
Input form
<input type="text" class="form-control" name="produktnavn" value="<?php echo $produktnavn; ?>">
The code starts with $felter = array(); which initializes an empty array.
Then, without changing the array itself, you're checking for non-emptiness of $felter
if( !empty( $felter ) ) {
foreach ($felter as $felt) {
if ($felter == '') {
$fejl = true;
}
}
}
You're trying to iterate over an array that has not gotten any elements pushed into it. And the logic statement if( !empty ($felter)) will also not work as expected either.
As a test, before the check for !empty, put something in the array with $felter[] = 'Test word'; and then, underneath it... (if you're looking for a non-empty array, the logical checker could be if(count($felter)) { before iterating over the array with foreach ($felter as $felt) { if ($felt == '')
$felter = array();
$felter[] = 'Test word';
if(isset($_POST['submit'])) {
$produktnavn = $_POST['produktnavn'];
$kategori = $_POST['kategori'];
if( count( $felter ) ) {
foreach ($felter as $felt) {
if ($felt == '') {
$fejl = true;
}
}
}
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;
}
}
I'm using the following
if (!empty($data['var_1'])
&& !empty($data['var_2'])
&& !empty($data['var_3'])
&& !empty($data['var_4'])
&& !empty($data['var_5'])
&& !empty($data['var_6'])
&& !empty($data['var_7'])
&& !empty($data['var_8'])
&& !empty($data['var_9'])) {
//BLOCK HERE
}
Basically, what I'm trying to achieve is if all of the variables are empty, hide the block. If 8 or less are empty, display the block.
Where am I going wrong?
You want || not &&. This will display the block only if they are all not empty. I think there is probably a nicer way to do this, though, like array_filter.
Well, you could just use a loop and an $isok variable:
$isok = false;
for($i=1; $i<10; $i++) {
if( !empty($data['var_'.$i])) {
$isok = true;
break; // no need to continue looping
}
}
if( $isok) {
// BLOCK HERE
}
This is easier to edit too, in case you change the var_ part or want a different range of numbers.
You can also try
$data = array_filter($data); // remove all empty value form array
if (count($data)) {
// do your thing
}
The code you wrote will display the block if ALL of the variables aren't empty. If you want it to be displayed when ANY of the variable isn't empty, use OR instead of AND by replacing the && by ||.
<?php
if (!empty($data['var_1']) || !empty($data['var_2']) || !empty($data['var_3']) || !empty($data['var_4']) || !empty($data['var_5']) || !empty($data['var_6']) || !empty($data['var_7']) || !empty($data['var_8']) || !empty($data['var_9'])) {
//BLOCK HERE
}
You can use array_values() for that:
if ( count(array_values($data)) ) {
//BLOCK HERE
}
Replace && (AND) with || (OR)
if (!empty($data['var_1'])
|| !empty($data['var_2'])
|| !empty($data['var_3'])
|| !empty($data['var_4'])
|| !empty($data['var_5'])
|| !empty($data['var_6'])
|| !empty($data['var_7'])
|| !empty($data['var_8'])
|| !empty($data['var_9'])) {
//BLOCK HERE
}
if (empty(array_values($data))) { /* will return you true if all variables are empty*/}