I have following code to print some data,
$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th><th>Time</th></tr>"
.if(count($result->http_response_body->items) > 0) {
foreach ($result->http_response_body->items as $key) {.
"<tr>
<td>". $key->recipient . "</td>
<td>". $key->event . "</td>
<td>" . @$key->tags[0] . "</td>
<td>" . date("r", $key->timestamp) . "</td>
</tr>"
.}
//fetchLogs($result);
}.
"</table>";
echo $html;
When I execute code it gives syntax error, unexpected 'if' condition. How to insert this if condition and other variables inside this table string. Please help.
you have a lot of syntax errors
$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th> <th>Time</th></tr>";
if ( count( $result->http_response_body->items ) > 0 ) {
foreach ( $result->http_response_body->items as $key ) {
$html .= "<tr>
<td>" . $key->recipient . "</td>
<td>" . $key->event . "</td>
<td>" . @$key->tags[ 0 ] . "</td>
<td>" . date( "r", $key->timestamp ) . "</td>
</tr>";
}
//fetchLogs($result);
}
$html .= "</table>";
echo $html;
also, dont supress errors ( @$key->tags[0]
). Do a proper check with isset
first. Or if you are on php7 you can use the null coalesce operator like $key->tag[0] ?? 'something else'
$key->tag[0] ?? 'something else'
.
Please try this
$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th><th>Time</th></tr>" ;
if(count($result->http_response_body->items) > 0) {
foreach ($result->http_response_body->items as $key) {
$html.= "<tr>
<td>". $key->recipient . "</td>
<td>". $key->event . "</td>
<td>" . @$key->tags[0] . "</td>
<td>" . date("r", $key->timestamp) . "</td>
</tr>";
}
//fetchLogs($result);
}
$html.="</table>";
echo $html;
Firstly PHP statements close with ;
that is semi-colon.
So, your first statement needs to be corrected as:
<?php
$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th><th>Time</th></tr>";
if (count($result->http_response_body->items) >0) { // Removed dot
foreach ($result->http_response_body->items as $key) { // Removed dot
$html .= "<tr><td>" .$key->recipient ."</td>
<td>" .$key->event ."</td>
<td>" .@$key->tags[0] ."</td>
<td>" .date("r", $key->timestamp) ."</td></tr>";
} // Removed Dot.
// fetchLogs($result);
}
$html .= "</table>";
echo $html;
?>
the trick is quite simple, you just have to understand string concatination, and once you've mastered that you can compose complex strings like this one.
$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th><th>Time</th></tr>";
if(count($result->http_response_body->items) > 0) {
foreach ($result->http_response_body->items as $key) {
$html."<tr>
<td>". $key->recipient . "</td>
<td>". $key->event . "</td>
<td>" . @$key->tags[0] . "</td>
<td>" . date("r", $key->timestamp) . "</td>
</tr>";
}
}
$html."</table>";
echo $html;
there are syntax errors in the code.You can use this
$html = "<table><tr><th>Email Address</th><th>Event</th><th>Tag</th> <th>Time</th></tr>";
if ( count( $result->http_response_body->items ) > 0 ) {
foreach ( $result->http_response_body->items as $key ) {
$html .= "<tr>
<td>" . $key->recipient . "</td>
<td>" . $key->event . "</td>
<td>" . @$key->tags[ 0 ] . "</td>
<td>" . date( "r", $key->timestamp ) . "</td>
</tr>";
}
}
$html .= "</table>";
echo $html;
RAther than using string concatenation which can have performance penalties with large strings my personal preference is to use an array into which items are added - implode at the end to output to screen.
<?php
$html=array();
$html[]="
<table>
<tr>
<th>Email Address</th>
<th>Event</th>
<th>Tag</th>
<th>Time</th>
</tr>";
if ( count( $result->http_response_body->items ) > 0 ) {
foreach ( $result->http_response_body->items as $key ) {
$html[]= "
<tr>
<td>{$key->recipient}</td>
<td>{$key->event}</td>
<td>{@$key->tags[ 0 ]}</td>
<td>" . date( "r", $key->timestamp ) . "</td>
</tr>";
}
}
$html[]= "</table>";
echo implode( PHP_EOL, $html);
unset( $html );
?>
Another way to do that :
<table>
<tr>
<th>Email Address</th>
<th>Event</th>
<th>Tag</th>
<th>Time</th>
</tr>
<?php if(count($result->http_response_body->items) > 0) : ?>
<?php foreach ($result->http_response_body->items as $key):?>
<tr>
<td><?php echo $key->recipient; ?></td>
<td><?php echo $key->event?; ?></td>
<td><?php echo @$key->tags[0]; ?></td>
<td><?php echo date("r", $key->timestamp); ?></td>
</tr>
<?php endforeach;?>
<?php endif;?>
</table>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.