Reputation: 149
How can I add if-else statement inside my echo tr and td? I try this code below but my output is a blank page.
<?php
$stmt = $dbh->prepare("$query");
$stmt->execute();
$result = $stmt->fetchAll();
$i="0";
$ext = EXT;
if ($stmt->rowCount() > 0) {
foreach($result as $value) {
$i++;
$id = $value['id'];
$gccode = $value['gccode'];
$datecreated = $value['datecreated'];
$notes= $value['notes'];
$branch = $value['branch'];
$groupid = $value['groupid'];
$status = $value['status'];
echo "<tr class=\"gradeA\">
<td>$i</td>
<td>$id</td>
<td>$gccode</td>
<td>$datecreated</td>
<td>$notes</td>
<td>$branch</td>
<td>" if ($value['groupid']=="1"){ echo 'Regular';}
else { echo 'Complimentary'; } "</td>
<td>$status</td>
</tr>";
}
}
Upvotes: 1
Views: 17172
Reputation:
Your page is blank because of php error. You need to use semicolons after every php statement.
Upvotes: 1
Reputation: 134
You need to complete your echo
statements properly:
echo "<tr class=\"gradeA\">
<td>$i</td>
<td>$id</td>
<td>$gccode</td>
<td>$datecreated</td>
<td>$notes</td>
<td>$branch</td>
<td>";
if ($value['groupid']=="1"){ echo 'Regular';}
else { echo 'Complimentary'; }
echo "</td>
<td>$status</td>
</tr>";
Again instead of writing if else statements, you can use a ternary operator.
So, your final optimized code should be:
echo "<tr class=\"gradeA\">
<td>$i</td>
<td>$id</td>
<td>$gccode</td>
<td>$datecreated</td>
<td>$notes</td>
<td>$branch</td>
<td>" . ($value['groupid']=="1") ? 'Regular' : 'Complimentary'. "</td><td>$status</td>
</tr>";
Upvotes: 3
Reputation: 560
Your page is blank because of php error. You need to use semicolons after every php statement. replace your code with below one.
echo "
<td>$i</td>
<td>$id</td>
<td>$gccode</td>
<td>$datecreated</td>
<td>$notes</td>
<td>$branch</td>
<td>";
if ($value['groupid']=="1")
{
echo 'Regular';
}
else
{
echo 'Complimentary';
}
echo "</td>
<td>$status</td>
</tr>";
Upvotes: 1
Reputation: 4584
There are multiple ways of doing this.
One of them would be to build the HTML and store it in a variable, then use a full fledged if else
to add to the variable e.g.
$myStr = '<tr class="gradeA">';
$myStr += '<td>' . $i . '</td>';
// snipped
if ($value['groupid'] == '1')
$myStr += '<td>Regular</td>';
else
$myStr += '<td>Complimentary</td>';
// do more string stuff
// return the built variable instead.
return $myStr;
But you can also use a ternary (shorthand if) statement. Using the same example:
$myStr = '<tr class="gradeA">';
$myStr += '<td>' . $i . '</td>';
// snipped
$myStr += '<td>' . ($value['groupid'] == '1' ? 'Regular' : 'Complimentary') . '</td>';
// do more string stuff
// return the built variable instead.
return $myStr;
The main advantage to this method over straight out echo
ing the content is that you can now also manipulate the variable itself in different places, in different ways - allowing for much more flexibility and more clean code (tho not always true).
Upvotes: 1
Reputation: 4010
You can change this line like bellow:
<td>" . ($value['groupid']=="1" ? 'Regular' : 'Complimentary') . "</td>
Upvotes: 4
Reputation: 23958
You need to break the echo statement by semi colon (;
)
Corrected code:
echo "<tr class=\"gradeA\">
<td>$i</td>
<td>$id</td>
<td>$gccode</td>
<td>$datecreated</td>
<td>$notes</td>
<td>$branch</td>
<td>";
if ($value['groupid']=="1"){
echo 'Regular';
}
else {
echo 'Complimentary';
}
echo "</td><td>$status</td></tr>";
Upvotes: 1