A very simple query that I cannot seem to figure out...
I'm using the CodeIgniter framework.
I'm retrieving data from my database and accessing the cols within the returned row via:
$item->available
where 'available' is the column of type int.
Now, I'd like to check whether the returned integer is 1 or not.
I believed this would be a simple case of
if ($item->available == 1) {
echo "Available";
} else {
echo "Sold";
}
}
However, this is not working. Can somebody please offer me some direction?
== will only equivelent, so true == 1 '1' == 1 etc.. to match type use === this will ensure only (int)1 === (int)1
And for consistancy, use type-casting to ensure type like so...
if ((int)$item->available === 1) {
echo "Available";
} else {
echo "Sold";
}
Related
The input record includes fifteen fields named 'student01', 'student02', student03' ... 'student15'. I need to do the same thing with the value stored in each of the fields. There's got to be a better way than:
if ($student01 != '') {
// process the info in $student01
}
if ($student02 != '') {
// process the info in $student02
}
...
if ($student15 != '') {
// process the info in $student15
}
I was thinking that PHP's variable variables might be the solution, but haven't figured out the right syntax.
Help appreciated.
Thanks!
Change the number 3 in the for statement to be 1 more than the number of students you are going to have.
for ($snum = 1;$snum < 3;$snum++) {
#echo ${'student' . str_pad($snum,2,'0',STR_PAD_LEFT)}; // left to show it works
if (${'student' . str_pad($snum,2,'0',STR_PAD_LEFT)} != '') {
echo 'Do something';
}
}
currently I have a form that allows users to fill in 3 input fields. 3 of them are text fields (title, author and ISBN) and 1 of it is a select option (categories).
What I would like to achieve is to allow users to fill in any number of the 4 fields, and return the respected values. This means that if users fills in 2 input fields, there will be 2 conditions to check in the backend. 3 inputs filled means 3 conditions.
What I have currenly is this (an example, not the actual code itself):
if($title == $allMyArray["title"]){
array_push($returningResult, $allMyArray);
}else if($author == $allMyArray["author"]){
array_push($returningResult, $allMyArray);
}else if($ISBN == $allMyArray["ISBN"]){
array_push($returningResult, $allMyArray);
}else if($categoreis== $allMyArray["categories"]){
array_push($returningResult, $allMyArray);
}else{
echo "nothing";
}
This set of code works when I only fill in one specific field. For example if I fill in only the author input, and leave the other 3 options blank, I will be returned wwith the values I want. However, if I attempt to fill in 2 or more fields at once, the returned value is incorrect.
So how else can I come up with an if else statement that will check which fields are filled, and set the conditions correctly based on the inputted fields?
Thank you all for your help in advanced! Much appreciated! :)
Changing to below code will work. The problem is when you use ELSE you are limiting your conditional statements to one result only.
Optionally, you can use switch/case statement if you find IF dirty.
But it may produce duplicates, so you need to work this out. (i dont know your code, it is just an assumption)
$atLeast1Result = false;
if($title == $allMyArray["title"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if($author == $allMyArray["author"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if($ISBN == $allMyArray["ISBN"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if($categoreis== $allMyArray["categories"]){
array_push($returningResult, $allMyArray);
$atLeast1Result = true;
}
if(!$atLeast1Result ) {
echo "nothing";
} else {
$returningResult = array_unique($returningResult); // this might now work on all versions, as i dont know what this array is.
}
The last hour I've been sitting with this problem. I have two if-statements (for testing purposes they are both IF-statements, and not IF- and ELSE IF-statements. The code runs the false IF-statement as if it is true.
The code:
<?php
$sth = $pdo->query("SELECT * FROM myDBTable WHERE alien1='$idkod' OR alien2='$idkod'");
$result = $sth->fetchAll();
if(!$result)
{
echo "No data";
}
else
{
foreach($result as $row)
{
$alien1 = $row['alien1'];
$alien2 = $row['alien2'];
if($idkod == $alien1)
{
echo $idkod . "==" . $alien1;
}
if($idkod == $alien2)
{
echo $idkod . "==" . $alien2;
}
}
}
?>
This will give me the following text on screen:
1234567891234567891234567==1234567891234567891234567
1234567891234567891234567==1234567891234567891234568
Clearly, the second text shouldn't be there, as the statement is not true.
Don't assume anything when making conditional forks, use var_dump() on the variables to temporarily look inside them - that way you best decide how to check for the exact type and value you are expecting.
Then as said already, prefer to check using ===
If you adopt this behaviour you will save countless hours and avoid some quite subtle bugs which can appear in your code.
Having the PHP Truth Tables pinned up for a while will help.
== ignores type when testing for equality. In this case it will assume that both strings are numbers and convert them. This means this will turn into:
9223372036854775807 == 9223372036854775807 //Max int val. Will be different on different systems.
=== will make sure that both arguments are the same type and will not attempt to coerce making
'1234567891234567891234567' === '1234567891234567891234568';
Give the expected result.
PHP equality is wacky sometimes.
It is wrong to use == you need to use === the second is value comparison, the first is object comparison (depending on the context)
I have modified your code a little check it
<?php
$sth = $pdo->query("SELECT * FROM myDBTable WHERE alien1='$idkod' OR alien2='$idkod'");
$result = $sth->fetchAll();
if(!$result)
{
echo "No data";
}
else
{
foreach($result as $row)
{
$alien1 = $row['alien1'];
$alien2 = $row['alien2'];
if($idkod == $alien1 && $idkod != $alien2)
{
echo $idkod . "==" . $alien1;
}
if($idkod == $alien2 && $idkod != $alien1)
{
echo $idkod . "==" . $alien2;
}
}
}
?>
I am trying to achieve the following: I ask my SQL database a query using SELECT * FROM subjects. After doing that I ask for the array using mysqli_fetch_assoc. Until that point all is fine. The problem now is that when I try to modify in each loop the value of $genero depending if it's 1 or 0. But the value of $genero never changes, it's always 1 and I am sure that the array is fetching 0 and 1. Any idea while the values of $genero are not changing through the loop?
while ($subject = mysqli_fetch_assoc($result)) {
if ($subject["sexo"] = 1) {
$genero = "<img src='images/hombre.png' />";
} else {
$genero = "<img src='images/mujer.png' />";
}
echo $genero;
}
Your comparison operator is wrong. You're using = which is an assignment operator. In your example it will always be true. You need to use == which is a comparison operator.
if ($subject["sexo"] = 1) {
should be
if ($subject["sexo"] == 1) {
In if statement you should use double equal sign: if (a == b)
$sql = 'SELECT * FROM `phpbb_profile_fields_data`';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
if ($row['pf_kp_em_no_bonethr'] == '1') {
echo " Was 1";
} else if ($row['pf_kp_em_no_bonethr'] == '2') {
echo "Was 2";
} else {
echo "Was Neither 1 or 2";
}
}
$db->sql_freeresult($result);
I am curios, In my example I am checking the field for either a value of 1 or 2 but how do I check it for a value of NULL. Would it be any of the following three:
if ($row['pf_kp_em_no_bonethr'] == '')
if ($row['pf_kp_em_no_bonethr'] == '-1')
if ($row['pf_kp_em_no_bonethr'] == 'NULL')
Normally I would just try it out but I am not at home and wont be for the foreseeable future it has been bugging me. I am pretty sure it's not the second but I have seen -1 used for a null value in other languages. So can someone verify how I would indeed check for a NULL value please.
if ($row['pf_kp_em_no_bonethr'] === NULL)
Something like this should work.
if (is_null($row['pf_kp_em_no_bonethr'])) {
echo "Is NULL";
}
MySQL will return NULL values to PHP as actual PHP NULL. In this situation, what you need is:
// Notice lack of quotes around NULL
// And use === to distinguish type properly between integer 0 and NULL
if ($row['pf_kp_em_no_bonethr'] === NULL)
However, it would be more appropriate to check it in the query if NULL values are what you need to work with in PHP.
$sql = 'SELECT * FROM `phpbb_profile_fields_data` WHERE pf_kp_em_no_bonethr IS NULL';
Or to find all three values:
$sql = 'SELECT * FROM `phpbb_profile_fields_data`
WHERE pf_kp_em_no_bonethr IS NULL
OR pf_kp_em_no_bonethr IN (1,2)
';
I'd recommend to be very carfull with this one: I have seen
<?php
$field=$row['fieldname'];
if ($field===null) {
//Do something
}
?>
fail intermittently, especially on windows. This is why I prefer
SELECT
IFNULL(fieldname,'some_safe_value') AS fieldname
...
FROM
...
and the resulting trivial null-check.
Use is_null or === NULL.
if(is_null($row['pf_kp_em_no_bonethr'])){
}
or
if($row['pf_kp_em_no_bonethr'] === NULL){
}