Simple question about IF statement - php

I just wonder how to check if there is a value in a column in a database table? The column is named star and I want to check if it has a value of 1 if not don't do anything.
while($row01 = $res01->fetch_object()) {
if ($res01->star[$row01] == 1) { ??????????
}
}
I use thise WHILE LOOP to fetch info from a table about some objects. For some objects I have a column named STAR that is 1 och NULL. While I build up the HTML-code into variables I in this loop, I also check if the object has 1 in the STAR column and if yes I create another variable with some HTML-code to use in the list of all the objects in the table.
But when I use if($row01->star == '1') it's not working and I don't know why!? When I use this it's like, yes all objects has 1, but there is only a few that has 1, the rest has NULL value. Sorry, but I have to leave the computer for some houres, but I hope to solve this later today! Thanks!

Close, but you're referring to the column wrong.
if ($row01->star == 1) { /* do things */ }

Related

cakephp2 beginner: how to compare the data from the db with a integer?

I want to compare a data from the db in cakephp2.
For example I have a data $product['price'], this data contains a value of 0.
I want to compare it with integers like the source code below, however,I cannot compare them.
I would love to have some help.
if($product['price']==0){
echo "free";
}else{
echo "$".$product['price'];
}
Since your var_dump of $product['price'] shows it is a string you need to convert it to an integer first - use
if (intval($product['price']) == 0) {
//do something
}
else {
//do something else
}
If possible though, it would be better to change your actual database so that the field you're looking at is an integer to begin with (stops someone saving a price as "SomeRandomLetters" instead ;)

TINYINT (0, 1) for boolean values in MySQL

as the title states ,TINYINT (0, 1) for boolean values in MySQL return true and false value ,,but i want yes and no value when i retrieve TINYINT (0, 1) values from database. is it possible??
Use IF:
SELECT IF(bool_value, 'yes', 'no') as string_value...
tinyint doesnt return true or false. It returns 0 or 1. If you want yes and no, you need to specify that yourself:
if($return == 0) {
return "no";
} else {
return "yes";
}
Yes, it is possible. One way is to add CASE to it:
SELECT
CASE
WHEN value = true THEN 'yes'
ELSE 'no'
END
FROM
`table`;
Another one, as Max suggested, add IF option.
Wanting "yes" and "no" sounds like it's for display purposes, and how you choose to format data from your database/models for display should be in your view oriented code and not the database schema nor the query logic, though there are valid exceptions to the latter. Aside from considerations such as whether "first name" and "last name" as separate items could be useful rather than a single "name" field, decisions about database structure should be based on what you need to store and how different data items relate to each other, not on how data is going to look once retrieved. Particularly as you're using a framework, this should be straightforward.
That said, you could use an enum type instead, defining the enum as enum('no','yes'), though cake may still not have support for enums. You can then get and set string values or numeric ones (1 and 2, or subtract 1 to get 0 and 1). Overall though, tinyint with values 0 and 1 is probably preferable as it's conventional and less likely to lead to mistakes/bugs now or in the future.
Use AppModel::afterFind
http://book.cakephp.org/2.0/en/models/callback-methods.html#afterfind
public function afterFind($results, $primary = false) {
foreach ($results as $index => $row) {
if (isset($row[$this->alias]['yes_or_no'])) {
$results[$index][$this->alias]['yes_or_no'] = $row[$this->alias]['yes_or_no'] ? 'yes' : 'no';
}
}
return $results;
}

CakePHP: How to switch two row values from a unique field?

I need to change the value of two rows in the order field of my database. This field is unique.
What I tried:
Storing the value of both items in a PHP variable,
Setting the first row's field value to NULL (nulls are accepted in the field),
Setting the value of the second row's field to the value that was held on the first row,
Setting the first row's field value to the original second row field value.
It doesn't work, since I am getting a "dupplicate entry" error when executing the order change. I can't seem to find out how to do this using CakePHP.
Here is the code that I have written (even though it's not functionnal):
if ($second_row) {
$next = $second_row['Immeuble']['order'];
$prev = $first_row['Immeuble']['order'];
$this->Immeuble->id = $first_row_id;
$this->Immeuble->saveField('order', 'null');
$this->Immeuble->id = $second_row['Immeuble']['id'];
$this->Immeuble->saveField('order', $prev);
$this->Immeuble->id = $first_row_id;
$this->Immeuble->saveField('order',$next);
}
of course its dublicate key
you defined id two times
$this->Immeuble->id = $first_row_id;
and
$this->Immeuble->id = $second_row['Immeuble']['id'];
and then
$this->Immeuble->id = $first_row_id;
try remove this last line
The code I stated above is fully functionnal. I just had inverted some values, which would screw everything up. I will not delete the question in case someone else needs to do something similar.

PHP array element comparison

Learning PHP and I have a question.
How does one obtain an element from an array and determine if it is equal to a static value? I have a return set from a query statement (confirmed the array has all values).
I tried:
<? if($row["rowValue"] == 1) {
}
?>
I was expecting the value to be 1, but it's returning null (as if I'm doing it wrong).
You're pretty much there; something like this should confirm it for you:
echo "<p>Q: Does ".$row["rowValue"]." = 1?</p>";
if($row["rowValue"] == 1) {
echo "<p>A: Yes ".$row["rowValue"]." does equal 1</p>";
} else {
echo "<p>A: No, '".$row["rowValue"]."' does not equal 1</p>";
}
If that's still returning 'No' you could try viewing the whole of the $row array by doing a var dump of the array like so:
var_dump($row);
This will give you detailed output of how the array is built and you should be able to see if you are calling the correct element within the array.
What is returning null?
Try this:
if($row["rowValue"] === 1) { ... }
Make sure there is an element in $row called rowValue.
maybe try:
<? if($row[0]["theNameOfAColumn"] == 1) {
}
?>
Usually databases return rows like row[0], row[1], row[2], etc.
I am not sure what exactly you are doing, but try using array_filp() which will Exchanges all keys with their associated values
than you can do like
if($row["rowValue"] == 1) {
http://in1.php.net/manual/en/function.array-flip.php
If you're pulling it from mysqli_fetch_row then it wants a number, not a column name. If it's being pulled from mysqli_fetch_array then it will accept a column name.
http://php.net/manual/en/mysqli-result.fetch-row.php
http://php.net/manual/en/mysqli-result.fetch-array.php

if condition for huge amount of object properties

I have to show the record from database if at least one filed is filled with value out of 50 fields for a row.
I fetched data from database successfully. Stored in array of object like this
$obj[0]->prop1;
$obj[0]->prop2;
$obj[1]->prop1;
$obj[1]->prop2;
There are more than 50 properties for a object.
I have to check each property if one of them is not empty show that record.
I have a long if like this
if ($obj[$counter]->prop1 !='' || $obj[0]->prop2 !='' ...
echo "show record"
wanted to know if there is shorten way. let me know if anything is not clear
Declare a function isEmpty() in the class. For each instance, you will have to only call
if(!($obj[$counter]->isEmpty()) {
...
}
I guess it's better to alter your query:
select f1, f2, ... f50, f1||f2...||f50 as fsum from ...
and then you can check if $obj[51] != ''
this will perform faster than php code...
No, you have to check each property individually...
So I found the easy way myself. You can just do a type cast the object to an array and check that array for values like this
foreach ((array)$data[$counter] as $value)
{
if($value !='')
{
$addRec=true;
}
}

Categories