The code below works I am able to get the color of the car. Now, when the site was made professionally, some of the fields didn't get filled out. So, some of the fields either have a 0, have the word null, empty, or are empty. The one I am interested in is the color column. Some of the fields are empty.
Function getCarColor($cariD){
the rest of my sql code
$carColor = $ref['color'];
return $carColor;
}
What I am stuck in is on how to check if they are empty and add a random text inside, just so that everything looks uniform.
This is my code
Function getCarColor($cariD){
the rest of my sql code
$carColorchk = $ref['color'];
$carColor == is_null (('unspecified') ?: $carColorchk);
return $carColor;
}
Please help. I will eat some extra tamales in your name on christmas dinner.
Try:
you can use null coalesce operator if you are using php 7:
$carColor = $ref['color'] ?? 'nocolor';
or user below if php >=5.3 only
$carColor = $ref['color'] ?: 'nocolor';
There are many ways to do that. You can try the below one:
<?php
if((!empty($your_value)) && ($your_value ! =0) && ($your_value !='')){
//your Code
}
?>
I'm using the ACF Gallery plugin for Wordpress that produces an array of image data.
It's called as so:
$floorplans = get_field('floorplans');
My problem is that sometimes, the array doesn't have any proper content e.g if i var_dump() it and they haven't uploaded any content, I get:
string(0) ""
If the result stored in $floorplans is empty, I want the script to skip over some statements.
I've tried:
if(!empty(get_field('floorplans'))){
...
}
... and yet the statements in my if still occur.
I tried:
if(isset(get_field('floorplans'))){
...
}
And I get:
Fatal error: Cannot use isset() on the result of a function call
So then I tried:
$floorplanCount = count(get_field('floorplans'));
if ($floorplanCount !== 0){
...
}
And you guessed it, the statements in the if still pass.
So how can I not run a section of code if there is nothing in my floorplans field?
As mentioned in comments get_field is the culprit, and a shining example of why a function shouldn't return multiple data types (array or string, array or int, etc), because it forces complexity on the calling code to have to figure out what was returned.
Try this:
if (((true === is_array($floorplans)) && (count($floorplans) > 0)) || (false === empty($floorplans))) {
// Do work
}
According to acf gallery documentation, you can use
if($floorplans)
{
//your statements goes here
}
Here is the link. http://www.advancedcustomfields.com/resources/gallery/
The reason why you cant use isset is, the get_field function returns the array if anything exists, else it will return a false or 0 ( Im not sure, but even if it is 0 or false the $floorplan variable should work seamlessly in the if statement as I mentioned above )
Here is more documentation just in case : http://www.advancedcustomfields.com/resources/get_field/
I am reading file. I read all data in $part. it is working fine but issue arise when i use empty function. It should display NULL but it is not showing NULL.
My code is as follows:
echo "\nParts------------".$parts[$r]."\n";
echo "\nParts---Size---------".strlen($parts[$r])."\n";
var_dump($parts[$r]);
// $parts[$r]=trim($parts[$r],' ');
//$parts[$r]=str_replace('""','',($parts[$r]));
if(empty($parts[$r]))
{
$entryarray[$c][$c2]='NULL';
}
else if(strlen($parts[$r])<1) //removing special characters
{
$entryarray[$c][$c2]='NULL';
// array_push($entryarray[$c]);
$valueArray=$valueArray.",".'NULL';
}
when i vardump($part) then it is showing its length is 2 instead of 0.
How to display it NULL. i cant write check as if(strlen($parts<2)) cause there is data in file which has lenght less then 2.
I think you meant to use isset instead.
$foo = array("a", "b");
isset($foo[2]); // false
The empty function check if is null or empty string or 0 or something like that.
If you want to chack if is null, i recommend u to use "is_null()"
To check if a key is on array use
array_key_exists($key,$array);
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 have the following if-clause:
if(empty($var)){
$errors[]="Failure";
}
this is for a form that will be display an errormessage when a user has made no entries. i would like to hide this field when a user has made something specific so that he cant see this input field. this div will only being displayed when another variable is set. because of the "empty" problem i got the errormessage even when there is no inputfield. so my question is:
how can i add further terms to that clause that it will be like:
if(empty($var) only when $var2===true ){
$errors[]="Failure";
}
i already tried to use && but this doesn't work the correct way. thats why i'm asking and thought maybe there is another way. thanks to all.
Try:
if(empty($var) AND $var2) {
$errors[]="Failure";
}
If $var is empty and $var2 is TRUE it will be inside the if condition. did you mean something like that
Probably $var2 is 1 and not true, because in other case this need to work
if(empty($var) && $var2===true){
$errors[]="Failure";
}
Try
if(empty($var) && $var2==true){
$errors[]="Failure";
}
== instead of === or check for logical bugs.