So I'm using a checkbox field, and I check it's value by using the code below and print things out accordingly. Anyway, if the field's checkboxes don't have any value, meaning all of them are unchecked, I get an error.
Warning: in_array() expects parameter 2 to be array, boolean given in /filepath.php on line 647
<?php if (in_array( 'Subbed', get_field('episode_sdversion'))) { ?>
Subbed
<?php } else {
echo '--';
} ?>
So basically, what can I do with this code to make it so when all values are unchecked, that would automatically mean that "Subbed" value is also unchecked, so it should simply just show echo '--';. So how can I make this echo '--'; run when all values are unchecked. So it shouldn't come up with that error?
I'm not sure what your get_field() function does, presumably it's part of a framework or something, but I'm guessing it returns the value of $_REQUEST['episode_sdversion'], which will be FALSE when the checkbox is empty.
In this case, if I understand your question correctly, a simple check to first see if get_field() is returning something other than FALSE would suffice:
<?php if (get_field('episode_sdversion') && in_array('Subbed', get_field('episode_sdversion'))) { ?>
Subbed
<?php } else {
echo '--';
} ?>
You are getting the error because get_field returns false instead of an array when no boxes are checked. The && operator is short circuit, meaning that if the first part gets evaluated as false, the second part won't get executed. So you can avoid the error by replacing your first line (if in_array(...)) with
if(get_field('episode_sdversion) && in_array('Subbed', get_field('episode_sdversion')))
You either have to change that code, or the get_field return value. A way to do it, would be to declare an array() in all cases, and then add every posted checkbox, so that you always have an array passed as the second parameter of the in_array function.
Related
let me explain my problem..
This is my array
$data['id']=1;
$data['name']='Bhavik';
$data['salary']=0;
$data['TA']=0;
before processing on the data I am checking whether the key is empty or not if it is empty then we have to neglect it.
like..
if(!empty($data['id']))
{
$responseData['id']=$data['id'];
}
if(!empty($data['name']))
{
$responseData['name']=$data['name'];
}
if(!empty($data['salary']))
{
$responseData['salary']=$data['salary'];
}
if(!empty($data['TA']))
{
$responseData['TA']=$data['TA'];
}
print_r(responseData);
it gives me only id and name
it considers 0 as empty
what if I want salary and TA with Zero value?
I can do it with checking whether the key is exists or not and the check whether it has any value or not but is there any other way.
I am not using key_exists just because if we pass array like $data['id] without any value still that condition will be true but it gives error bcoz it does not have any value while processing the data.
so any help will be appreciated.
Thank you
you can add second OR condition within IF condition statement like
if(!empty($a) || $a==0)
Try this hope it will solve your problem
You can use this function instead of empty:
function is_blank($value) {
return empty($value) && !is_numeric($value);
}
Treating the values as strings, check the string length with strlen() and the validity of characters inside it, in this case (from the description I assume all-digit values) - ctype_digit().
If you don't know whether the key exists, check that with isset().
You can check using strict compare
if(null!==$a){
//... 0 is passed as true value
}
This is honestly the most finicky and inept language I've ever coded in. I'll be glad when this project is good and over with.
In any case I have to us PHP so here's my question.
I have an Array named $form_data as such:
$form_data = array
('trav_emer_med_insur',
'trav_emer_single',
'trav_emer_single_date_go',
'trav_emer_single_date_ba',
'trav_emer_annual',
'trav_emer_annual_date_go',
'trav_emer_extend',
'trav_emer_extend_date_go',
'trav_emer_extend_date_ef',
'trav_emer_extend_date_ba',
'allinc_insur',
'allinc_insur_opt1',
'allinc_single_date_go',
'allinc_single_date_ba',
'allinc_insur_opt2',
'allinc_annual_date_go',
'allinc_annual_date_ba',
'cancel_insur',
'allinc_annual_date_go',
'allinc_annual_date_ba',
'visitor_insur',
'country_select',
'visitor_supervisa',
'visitor_supervisa_date_go',
'visitor_supervisa_date_ba',
'visitor_student',
'visitor_student_date_go',
'visitor_student_date_ba',
'visitor_xpat',
'visitor_xpat_date_go',
'visitor_xpat_date_ba',
'txtApp1Name',
'txtApp2Name',
'txtApp1DOB',
'txtApp2DOB',
'txtApp1Add',
'txtApp1City',
'selprov',
'txtApp1Postal',
'txtApp1Phone',
'txtApp1Ext',
'txtApp1Email',
'conpref', );
These are the names of name="" fields on an HTML form. I have verified that ALL names exist and have a default value of '' using var_dump($_POST).
What I want to do is very simple, using the $form_data as reference do this:
create a new array called $out_data which can handle the data to display on a regurgitated form.
The structure of $out_data is simple the key will be the name of the element from the other array $out_data[txtApp1Name] for example, and then the value of that key will be the value.
Now what I want is to first check to see if every name="" is set or not, to eliminate errors and verify the data. Then regardless of whether it is set or not, create its placeholder in the $out_data array.
So if $_POST[$form_data[1]] (name is 'trav_emer_single') is not set create an entry in $out_data that looks like this $out_data([trav_emer_single] => "NO DATA")
If $_POST[$form_data[1]] (name is 'trav_emer_single') is set create and entry in $out_data that looks like this: $out_data([trav_emer_single] => "whatever the user typed in")
I have tried this code:
$out_data = array();
$count = count($form_data);
for( $i = 0; $i < $count; $i++ )
{
if(!isset($_POST[$form_data[$i]])) {
$out_data[$form_data[$i]] = "NO_DATA";
}
else {
$out_data[$form_data[$i]] = $_POST[$form_data[$i]];
}
}
Now this code technically is working, it is going through the array and assigning values, but it is not doing so properly.
I have hit submit on the form with NOTHING entered. Therefore every item should say "NO_DATA" on my regurgitated output (for user review), however only some items are saying it. All items I have confirmed have name="" and match the array, and have nothing entered in them. Why is "NO_DATA" not being assigned to every item in the array?
Also of note, if I fill in the form completely $out_data is fully and correctly populated. What is the problem with !isset? I've tried doing $_POST[$form_data[$i]] == '' which does put no_data in every instance of no data, however it throws an 'undefined index' warning for every single item on the page whether I write something in the box or not.
Really I just want to know WTF is going on, the dead line for this project is closing fast and EVERY step of the PHP gives me grief.
As far as I can tell by reading around my code is valid, but refuses to execute as advertised.
If you need more code samples please ask.
Really befuddled here, nothing works without an error, help please.
Thanks
-Sean
Instead of checking !isset(), use empty(). If the form posts an empty string, it will still show up in the $_POST as an empty string, and isset() would return TRUE.
I've replaced your incremental for loop with a foreach loop, which is almost always used in PHP for iterating an array.
$out_data = array();
foreach ($form_data as $key) {
if(empty($_POST[$key])) {
$out_data[$key] = "NO_DATA";
}
else {
$out_data[$key] = $_POST[$key];
}
}
PHP's isset returns TRUE unless the variable is undefined or it is NULL. The empty string "" does not cause it to return FALSE. empty() will do exactly what you need, though.
http://php.net/manual/en/function.isset.php
isset() will return FALSE if testing a variable that has been set to
NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP
NULL constant.
Returns TRUE if var exists and has value other than NULL, FALSE
otherwise.
I'd like to separate two things.
If a $_POST["something"] was sent, BUT empty.
OR
If a $_POST["something"] was not sent.
Is there a way?
You might be tempted to use empty(), but it blows up in your face if your possible values include a 0. Best to use something like:
if (isset($_POST['something'])) {
// the form field was sent ...
if ($_POST['something'] === '') {
// the form field was sent, but is empty
}
}
This works, as ALL data coming out of the _GET/_POST/_REQUEST superglobals is a string, regardless of whether it's a number or not.
You'll want to use isset()
if (isset($_POST["something"])){
echo "The Post Variable 'something' was posted";
if (strlen($_POST["something"])==0)
echo "The Post Variable is blank";
else
echo "The Post Variable Contains ".$_POST['something'];
}
look towards isset and empty.
http://php.net/manual/fr/function.isset.php
http://php.net/manual/fr/function.empty.php
Edit : The comments below are true. Empty should be used for empty strings, not numeric values, float values or a zero in a string. However, here's a function if you need to accept these as valid values:
function is_blank($value) {
return empty($value) && !is_numeric($value);
}
i use the php function isset() to check if a POST was sent or not
http://php.net/manual/en/function.isset.php
if ($_POST["something"])
will return true if there is such field
if $_POST["something"] == ""
will do the rest
I'm trying to just write a simple PHP if statement that checks if a custom field has anything entered into or if it has been left blank.
when it is blank it is meant to not print anything to the page,
if something is set in the custom field then it should create a li element with an a tag inside of it.
here is my code so far:
<ul class="externalLinks">
<? $emptycheck = get('linkname',2,1,0);
if (isset($emptycheck)){ ?>
<li><? echo get('linkname',2,1,0);?></li>
<? } else { '' } ?>
<li>Download a PDF of this project</li>
</ul>
The custom fields in this case are set by the wordpress admin (through the flutter plugin).
The issue I am having is simply that if the custom fields are left blank an empty
<li><a></a></li>
is created.
get('linkname',2,1,0) returns the field content obviously (this part works).
Any ideas would be much appreciated.
Thanks,
Jannis
Just because a variable is empty doesn't mean it's not isset().
You want to check if it's empty() or not.
$whatever = get('whatever',2,1,0)
if (! empty($whatever)){
//output your stuff
}
To be clear: isset() will just tell you if the variable name exists in the symbol table. empty() will return true when a variable is empty.
According to the documentation:
The following things are considered to
be empty:
"" (an empty string)
0 (0 as an integer)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)
Edited: empty() is kind of special, and operates only on a regular variable. You can't test an expression (for instance, the return value of a function) with empty(). This is clearly documented on the manual page.
You're setting $emptycheck regardless, if the return value is falsy you might want
if ( $emptycheck ) {
} else {
}
Otherwise it will always evaluate to true. Or flip the logic around with a negation operator ( ! ) if it's the other way around.
I am getting this error in a PHP class...
Fatal error: Can't use method return
value in write context in
C:\webserver\htdocs\friendproject2\includes\classes\User.class.php
on line 35
Here is the troubled part.
if(isset($this->session->get('user_id')) && $this->session->get('user_id') != ''){
//run code
}
This code is in my contrustor, is a value is not already set for $this->session->get('user_id') then it will return false instead of a Number. So as you can see I was hoping to check if this value is a number or not or not even set.
Any help with fixing appreciated.
You can't use isset for the result of a function. Consider the following code instead:
if( $this->session->get('user_id') ){
//run code
}
isset() only works with variables as
passing anything else will result in a
parse error. For checking if constants
are set use the defined() function.
From the PHP Manual.
You can't use isset on a function. However, since false, 0, and '' all equate to a falsey statement, write your test this way:
if( $id = $this->sessions->get('user_id') ){
// Will only run if $id does not equal '', False, or 0
}
That way you have run your test and assigned the variable in one step.