Reputation: 65
I have the following PHP code in which I am trying to use an if/else statement to assess if the returned string is empty; but, I have had no success so far.
$result = '<div class="card-deck">';
if($rows > 1) {
while($list = $engrams->fetch_assoc()) {
$result .= '
<div class="col-4 p-2">
<div class="card border-info m-1">
<img src="/assets/images/items/'.$list['image'].'" alt="'.$list['name'].'" class="card-img-top">
<div class="card-body" style="margin-left: 60px; position: absolute; text-align: center;">
<p class="card-title">'.$list['name'].'</p>
</div>
<div class="card-body" style="margin-top: 50px;">
<p>'.$list['description'].'</p>
<p>'.$list['res_name1'].'</p>
<p>'.$list['res_name2'].'</p>
<p>'.$list['res_name3'].'</p>
</div>
<div class="card-footer d-flex justify-content-between">
<small><span class="text-left text-muted">Level Required: '.$list['lvl'].'</span></small>
'if (empty($list['engram_points'])) {'<small>PLM</small>'}'
'else {'<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>'}'
</div>
</div>
</div>
';
}
}
$result .= '</div>';
echo json_encode(['result'=>$result,'rows'=>$rows,'qty'=>$qty,'active'=>$active])
Upvotes: 0
Views: 75
Reputation: 303
You can use a closure, if you are really looking to put the logic in a variable:
<?php
function createLister($list) {
return function() use ($list) {
// Your logic
if (empty($list['engram_points'])) {
return '<small>PLM</small>';
} else {
return '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>';
}
};
}
$lister = createLister($list);
echo $lister(); // Your output
See more here:
http://php.net/manual/en/class.closure.php
Otherwise the ternary operator might be what you are looking for:
<?php
$condition = true;
$doThis = 'Hello World';
$elseThis = 'Goodbye';
// Outputs 'Hello World'
$condition ? $doThis : $elseThis;
Upvotes: 0
Reputation: 370
As mentioned above the Ternary operator is the way to go.
When used with your if else code it will look as follow:
<div class="card-footer d-flex justify-content-between">
<small>
<span class="text-left text-muted">Level Required: '.$list['lvl'].'</span>
</small>'
. ( empty($list['engram_points']) ? '<small>PLM</small>' : '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>' ) .
'</div>'
Upvotes: 3
Reputation: 2621
You can check if the variable is empty before assigning all the values to the $result
variable.
Example:
if (empty($list['engram_points'])){
$points = "<small>PLM</small>";
}else{
$points = '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>';
}
$result .= '
<div class="col-4 p-2">
<div class="card border-info m-1">
<img src="/assets/images/items/'.$list['image'].'" alt="'.$list['name'].'" class="card-img-top">
<div class="card-body" style="margin-left: 60px; position: absolute; text-align: center;">
<p class="card-title">'.$list['name'].'</p>
</div>
<div class="card-body" style="margin-top: 50px;">
<p>'.$list['description'].'</p>
<p>'.$list['res_name1'].'</p>
<p>'.$list['res_name2'].'</p>
<p>'.$list['res_name3'].'</p>
</div>
<div class="card-footer d-flex justify-content-between">
<small><span class="text-left text-muted">Level Required: '.$list['lvl'].'</span></small>
'.$points.'
</div>
</div>
</div>
';
Upvotes: 1
Reputation: 4401
You cannot put code inside a string like this. You can, however, break the string and use if/else inside the loop... like this:
while($list = $engrams->fetch_assoc()) {
$result .= '
<div class="col-4 p-2">
<div class="card border-info m-1">
<img src="/assets/images/items/'.$list['image'].'" alt="'.$list['name'].'" class="card-img-top">
<div class="card-body" style="margin-left: 60px; position: absolute; text-align: center;">
<p class="card-title">'.$list['name'].'</p>
</div>
<div class="card-body" style="margin-top: 50px;">
<p>'.$list['description'].'</p>
<p>'.$list['res_name1'].'</p>
<p>'.$list['res_name2'].'</p>
<p>'.$list['res_name3'].'</p>
</div>
<div class="card-footer d-flex justify-content-between">
<small><span class="text-left text-muted">Level Required: '.$list['lvl'].'</span></small>';
if (empty($list['engram_points'])) {
$result .= '<small>PLM</small>';
}
else {
$result .= '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>;
}
$result .= ' </div>
</div>
</div>
';
}
Upvotes: 1
Reputation: 3509
The following should do the trick:
<div class="card-footer d-flex justify-content-between">
<small><span class="text-left text-muted">Level Required: '.$list['lvl'].'</span></small>
'.empty($list['engram_points']) ? '<small>PLM</small>' : '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>'.'
</div>
Upvotes: 1
Reputation: 1571
You can use PHP's ternary operator as follows:
$var='<small>'.($list['engram_points']?$list['engram_points']:'PLM').'<small>';
The format is (expr1 ? expr2 : expr3) and evaluates to - if expr1 is set then use expr2 else use expr3
Further info : http://php.net/manual/en/language.operators.comparison.php
Upvotes: 0