Condition to check a BIT field - php

...and I'm checking this field like so:
if((bool)$website['IsDeleted']) { }
but it ALWAYS returns an empty string regardless of the value in the MySQL field wether its 0 or 1:
["IsDeleted"]=> string(1) "" }
Please tell me what am I doing here? Should the if condition be modified?

I can't see anything wrong with your if-statement.
<?php
$website['IsDeleted'] = 1;
if((bool)$website['IsDeleted']) { echo 'found you';}
?>
would output
found you
<?php
$website['IsDeleted'] = 1;
if((bool)$website['IsDeleted']) { echo $website['IsDeleted'];}
?>
would output 1
If your value is NULL, it won't work though. Then you will have to check NULL-value as well.
<?php
$website['IsDeleted'] = null;
if((bool)$website['IsDeleted'] || $website['IsDeleted'] === null) { echo 'found you';}
?>
would output found you.

Related

How do you make sure an array is empty in PHP?

Im writing a page in HTML/PHP that connects to a Marina Database(boats,owners etc...) that takes a boat name chosen from a drop down list and then displays all the service that boat has had done on it.
here is my relevant code...
if(isset($_POST['form1'])){//if there was input data submitted
$form1 = $_POST['form1'];
$sql1 = 'select Status from ServiceRequest,MarinaSlip where MarinaSlip.SlipID = ServiceRequest.SlipID and BoatName = "'.$form1.'"';
$form1 = null;
$result1 = $conn->query($sql1);
$test = 0;
while ($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$values1[] = array(
'Status' => $row['Status']
);
$test = 1;
}
echo '<p>Service Done:</p><ol>';
if($test = 1){
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
echo '</ol>';
}else{
echo 'No service Done';
}
the issue im having is that some of the descriptions of sevice are simply Open which i do not want displayed as service done, or there is no service completed at all, which throws undefined variable: values1
how would I stop my script from adding Open to the values1 array and display a message that no work has been completed if values1 is empty?
Try this
$arr = array();
if (empty($arr))
{
echo'empty array';
}
We often use empty($array_name) to check whether it is empty or not
<?php
if(!empty($array_name))
{
//not empty
}
else
{
//empty
}
there is also another way we can double sure about is using count() function
if(count($array_name) > 0)
{
//not empty
}
else
{
//empty
}
?>
To make sure an array is empty you can use count() and empty() both. but count() is slightly slower than empty().count() returns the number of element present in an array.
$arr=array();
if(count($arr)==0){
//your code here
}
try this
if(isset($array_name) && !empty($array_name))
{
//not empty
}
You can try this-
if (empty($somelist)) {
// list is empty.
}
I often use empty($arr) to do it.
Try this instead:
if (!$values1) {
echo "No work has been completed";
} else {
//Do staffs here
}
I think what you need is to check if $values1 exists so try using isset() to do that and there is no need to use the $test var:
if(isset($values1))
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
Or try to define $values1 before the while:
$values1 = array();
then check if it's not empty:
if($values1 != '')
foreach($values1 as $v1){
echo '<li>'.$v1['Status'].'</li>';
}
All you have to do is get the boolean value of
empty($array). It will return false if the array is empty.
You could use empty($varName) for multiple uses.
For more reference : http://php.net/manual/en/function.empty.php

if statement when NULL (PHP)

I want to have if the variable = null then he makes it to 1.
if the variable exist do nothing and dont make it again to 1.
i got this code:
if (isset($_POST["register"])) {
if(!$l_NextPage){
$l_NextPage = 1;
echo "helaas" . "</br>";
}
if($l_NextPage == 1){
echo "hoi";
$l_NextPage = 2;
}else if($l_NextPage == 2){
echo "doei";
}
}
only the code dont work i tried empty, isset, $var == FALSE but everytime he makes $l_NextPage to 1. is there any solution i tried this too with session but even it don't work!
What am I doing wrong?
what happen when you refresh page, it assign $l_NextPage = 1 every time, thats why all the time hoi printed
you can use sessions for preserving value of variable after page refresh
try this code
// write this line of code at top of php block
session_start();
if (isset($_POST["register"]))
{
if (!isset($_SESSION["l_NextPage"]))
{
$_SESSION["l_NextPage"] = 1;
echo "helaas" . "</br>";
}
if($_SESSION["l_NextPage"] == 1)
{
echo "hoi";
$_SESSION["l_NextPage"] = 2;
}
else if($_SESSION["l_NextPage"] == 2)
{
echo "doei";
//unset( $_SESSION['l_NextPage'] ); unset varibale
}
}
after reaching at prefixed condition you can unset varible using
unset( $_SESSION['l_NextPage'] );
i have not tested code but this should work
you should try:
if(!isset($l_NextPage)) {
$l_NextPage = 1;
echo "helaas" . "</br>";
}
elseif($l_NextPage == 1) {
(...)
try like this,
if(!empty(trim($l_NextPage)))

Boolean input flipping

I'm getting really strange results on a php script that takes boolean input. The idea is that the data needs to be stored as either a 1 or a 0, but the input to the php script is a string in true/false format. Check this out:
<?php
function boolToBinary($str) {
echo $_POST['wants_sms'] . " " . $str;
die();
// posting this so that you can see what this function is supposed to do
// once it is debugged
if ($str == true) {
return 1;
} else {
return 0;
}
}
$gets_sms = boolToBinary($_POST['wants_sms']);
Here is the output from this function:
false true
How can that be??? Thanks for any advice.
EDIT: Solution: Still not sure why my output was flipped, but the fundamental problem is solved like this:
if ($str === 'true') {
return 1;
} else {
return 0;
}
Thanks to RocketHazmat for this.
http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
see this example:
var_dump((bool) "false"); // bool(true)
And the explanations:
When converting to boolean, the following values are considered FALSE:
...
the empty string, and the string "0"
...
Every other value is considered TRUE (including any resource).
In your case the $_POST['wants_sms'] variable contains a string "false";

PHP if statement returns same value

My code is always returning true when I'm comparing these variables.
What am I doing wrong?
<?php
$postuser = (integer)bp_activity_user_id(); //echos 1int(0)
$posteduser = (integer)bp_activity_comment_user_id(); //echos 3int(0)
if ( $postuser === $posteduser) {
echo 'true';
} else {
echo 'false';
}
?>
You need to use the function that RETURNs the value, not outputs it.
From docs I found for whatever this is,
bp_activity_user_id() X-Ref
Output the activity user ID.
bp_get_activity_user_id() X-Ref
Return the activity user ID.
return: int The activity user ID.
The function you are using echoes the variable, NOT returns it and therefore you can't set a variable with that function. Same for the this function.
bp_activity_comment_user_id() X-Ref
Output the ID of the author of the activity comment currently being displayed.
bp_get_activity_comment_user_id() X-Ref
Return the ID of the author of the activity comment currently being displayed.
return: int|bool $user_id The user_id of the author of the displayed
To use in an assignment, the function has to return a value. That's why your values are always (int)0: the functions you are using have no return value. So, it returns null which is cast to 0.
<?php
$postuser = bp_get_activity_user_id();
$posteduser = bp_get_activity_comment_user_id();
//no need to cast: these functions return integers
if ( $postuser === $posteduser) {
echo 'true';
} else {
echo 'False';
Just use intval and == i think it should work and evaluate it fine
<?php
$postuser = intval(bp_activity_user_id()); //echos 1int(0)
$posteduser = intval(bp_activity_comment_user_id()); //echos 3int(0)
if ( $postuser == $posteduser) {
echo 'true';
} else {
echo 'False';
}
?>
Your prbolems are probably the wrong typecasts:
Change (integer) to (int)
$postuser = (int)bp_activity_user_id(); //echos 1int(0)
$posteduser = (int)bp_activity_comment_user_id();
http://php.net/manual/en/language.types.integer.php

If/then statement on field value in drupal 7

I'm trying to write a statment that looks at the value in a boolean field (field_solo) and returns one of two template files that I have created in Drupal 7.
My field "field_solo" is correctly outputting a value of 0 or 1 and I have cleared the cache.
Can someone tell me if I am doing this correctly? Right now I am not getting it to display when the statement is TRUE.
function motg_preprocess_node(&$vars) {
$node = $vars['node'];
if($node->field_solo[0]['value'] == 1)
{
$vars['theme_hook_suggestion'] = 'node__solo';
} else
{
$vars['theme_hook_suggestion'] = 'node__video';
}
}
Instead of
if($node->field_solo[0]['value'] == 1)
Make it
if($node->field_solo['und'][0]['value'] == 1)
// OR
if($node->field_solo[LANGUAGE_NONE][0]['value'] == 1)
Have a look at this it could be helpful:
http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
This worked for me
<?php if ($content["field_hide_title"]["#items"][0]["value"] == 0) { ?>
<?php echo $node->title; ?></h2>
<?php } ?>

Categories