In the below code 'resAvailability' might be equal to 1 or 0. Is it possible to update this code in such a way that 1 produces 'Yes' and 0 results in 'No'?
<?php foreach ($result as $row):?>
<tr>
<td><?php echo $row['resAvailability']; ?></td>
<?php endforeach;?>
echo $row['resAvailability'] ? 'Yes' : 'No';
This is called the ternary operator.
This is the way I would do it:
echo ($row['resAvailability'] == 1) ? "Yes": "No";
Be aware that 1 will also validate as true and 0 as false so in actual fact you don't need the == 1 in my example as either way it will run as:
Is $row['resAvailability'] true, return yes, else return no.
You mean like this? Very basic stuff
if($row['resAvailability'] == 1)
{
echo "Yes";
}
else
{
echo "No";
}
edit
Emil his code is effectively the same is this, though since you asked such a basic question I thought you were quite new and in my opinion this is easier for beginners ;) though I would definitly go with Emil's way (less code and all that).
Related
I am using Views php in Drupal 6. I need to do an if statement to find if nodehierarchy_parent is equal to the value 0. Then do something. Right now I have it set to if the value contains a 0. My code is below.
<?php
if (strpos($data->nodehierarchy_parent,'0')) {
print 'hello';
}
else print $data->nodehierarchy_parent;
?>
I am new to stack. John's comment solved my problem but I cant mark his comment as correct answer. Below is the final code that worked.
<?php
if ($data->nodehierarchy_parent == 0) {
print 'hello';
}
else print $data->nodehierarchy_parent;
?>
echo (strpos($data->nodehierarchy_parent,'0') === false)
? $data->nodehierarchy_parent
: 'hello';
I'm trying to fix an issue with permissions in my app, I have a session->userdata('usrclass') set when login with different account class.
I have an ADMIN user who has a usrclass of ADMIN and I need to show some content based on that.
BUT when I do this:
<?= ($this->session->userdata('usrclass') == "ADMIN") ? 'yes' : 'no'; ?>
It outputs "no"... While it should output "yes". So I tried reading into the sessiondata with this code:
<?= echo $this->session->userdata('usrclass') ?>
This outputs the word ADMIN... This is a weird behavior, I've tried using ===, I've tried to figure out other stuff but couldn't.
What could it be?
I'm going to attach some pics of this:
may be there are some extra whitespace(s) in your session data value, try:
<?php
$sess_data = $this->session->userdata('usrclass');
$sess_data = trim($sess_data);
//and echo
echo ($sess_data == "ADMIN") ? "si" : "no";
?>
You could try making sure it's a string coming back from the session.
((string) $this->session->userdata('usrclass') == "ADMIN") ? 'yes' : 'no';
Maybe a long shot but could the first userdata('usrclass') be wiping the value. Try removing the test code.
echo $this->session->userdata('usrclass')
I want to include 2 files based on the following conditional statement
<?php
if (isset($_SESSION['name']) && $_SESSION['name'] == true) {
include 'file_a.php';
}else{
include 'file_b.php';
}
?>
Is this the correct way?
You can try next code:
if(session_id() == '') {
session_start();
include !empty($_SESSION['name']) ? 'file_a.php' : 'file_b.php';
}
It depends what you mean by 'correct':
If you mean 'is it valid' then yes, your code will work and there are no syntax errors.
If you mean 'can it be prettier', then perhaps it could, but it's personal preference whether you'd want to use ternary operators or not:
$toShow = isset($_SESSION['name']) && $_SESSION['name'] ? "file_a.php" : "file_b.php";
include $toShow;
Again, whether or not this is better or worse than your previous code, is down to your personal opinion.
TL;DR: Yes, your code is correct.
if ($row['active'] == 1) echo ''.htmlspecialchars($row['username']).''; else echo htmlspecialchars($row['username']);
Could I write this shorter and cleaner somehow?
echo $row['active'] == 1 ? ''.htmlspecialchars($row['username']).'' : htmlspecialchars($row['username']);
explained a little here http://www.addedbytes.com/php/ternary-conditionals/
I'm assuming you made a mistake putting the $id in a single quoted string, and meant for php to put the value of $id in its place in there.
$name=htmlspecialchars($row['username']);
if($row['active'] == 1) {
echo "<a href='prof?id=$id'>$name</a>";
} else {
echo $name;
}
You could take advantage of the ternary operator:
echo ($row['active'] == 1)
? ''.htmlspecialchars($row['username']).''
: htmlspecialchars($row['username'])
;
(I split the code onto separate lines for the sake of formatting.
What the heck is wrong with this:
if ($bb[$id][0] == "bizz") {
$BoxType = "bus_box";
} else {
$Boxtype = "home_box";
}
<div class="<? echo $BoxType; ?>">
$bb[$id][0] can either be 'bizz' or 'home' but no matter what it stops after the first step.
PHP variables are case sensitive. The 'T' in $BoxType is lower case in the else block.
Not entirely related to your question (which has already been answered), but you may be interested in the ternary operator :)
<div class="<?= $bb[$id][0] == "bizz" ? "bus_box" : "home_box" ?>">
Explain what you mean by "it stops after the first step". Tom is correct, $BoxType and $Boxtype are the not same variables, but it sounds like $BoxType is always getting "bus_box". If it were really "stopping after the first step", $BoxType would just be whatever it was initialized to in the event that $bb[$id][0] was "bizz" and $Boxtype would be "home_box".