0

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.

7 Answers 7

1

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'.

Sign up to request clarification or add additional context in comments.

Comments

0

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;
?>

Comments

0

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;  

Comments

0

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; 

Comments

0

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 );
?>

Comments

0

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>

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.