Robster
Robster

Reputation: 87

Include if statement in HTML table

I want to include an if statement in my PHP code that renders an HTML table. The output should be in a table row, too.

echo '<table style="border:1px solid red;">',
 '<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>',
 if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>',
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td>
</tr>',
echo "\t";
}
 '<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>  
</table>';
?>

I tried it like this, but there are some errors.

Upvotes: 1

Views: 25680

Answers (7)

Jethi Mayank
Jethi Mayank

Reputation: 53

No need of write table within PHP. it will make complexity of the code. try this:

<?php if(condition) { ?>

     <tr> <td>some content</td></tr>

<?php } elseif(condition) { ?>

      <tr><td>some content</td></tr>

<?php } else { ?>

      <tr><td>some content</td></tr>

<?php } ?>

Upvotes: 0

Athar Iqbal
Athar Iqbal

Reputation: 458

$output = "<table style=\"border:1px solid red;\">";
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">";
$output .= $mounts['name'];
$output .= "</div></td>";
$output .= "</tr>";
if ($itembind == 1) {
    $output .= "<tr align=\"center\"><td><div class=\"item-font1\">Text1</div></td></tr>";
} else if ($itembind== 2) {
    $output .= "<tr align=\"center\"><td><div class=\"item-font1\">Text2</div></td></tr>\t";
}
$output .= "<tr align=\"center\"><td><div class=\"item-font1\">";
$output .= $mounts['name'];
$output .= "</div></td></tr>";
$output .= "</table>";
echo $output;

Upvotes: 0

Yogendrasinh
Yogendrasinh

Reputation: 895

You are using , instead of .. Also need to add ; at your echo end.

<?php

$mounts['name'] = 'yyy';
$itembind = 1;
echo '<table style="border:1px solid red;">',
 '<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td>
</tr>';
 if ($itembind == 1) {
echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>';
} elseif ($itembind== 2) {
echo '<tr align="center" ><td><div class="item-font1">Text2</div></td>
</tr>';
echo "\t";
}
 '<tr align="center" ><td><div class="item-font1">'.$mounts['name'].'</div></td>
</tr>  
</table>';
?>

Upvotes: 1

Sᴀᴍ Onᴇᴌᴀ
Sᴀᴍ Onᴇᴌᴀ

Reputation: 8297

With echo, the strings can be separated by a comma (i.e. ,). However, when additional logic is needed, the echo statement must be terminated. Use a semi-colon (i.e. ;) for that.

echo '<table style="border:1px solid red;">',
    '<tr align="center" ><td><div class="item-font1">', $mounts['name'], 
    '</div></td>
     </tr>'; //use a semi-colon here to end the call to echo

if ($itembind == 1) { 

As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. 1

While it is acceptable to pass multiple strings to echo, the strings could be concatenated into one. For that, use a dot operator.

So update this line (which is missing an echo at the beginning) :

'<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div>
</td></tr></table>';

To

echo '<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div>
</td></tr></table>';

Final output:

<?php
$mounts = ['name'=>'cat'];
$itembind = 1;
echo '<table style="border:1px solid red;">',
 '<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>';
 if ($itembind == 1) {
    echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>';
} elseif ($itembind== 2) {
    echo '<tr align="center" ><td><div class="item-font1">Text2</div></td>
</tr>';
echo "\t";
}
 echo '<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td>
</tr>  
</table>';
?>

See a demonstration of the updated code in this playground example.


1http://php.net/manual/en/language.basic-syntax.instruction-separation.php

Upvotes: 4

Altimus Prime
Altimus Prime

Reputation: 2337

It would be a better idea to make the conditional statement on the value that's being inserted, but doing this your way looks like:

echo '<table style="border:1px solid red;">',
 '<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td>
</tr>';
if ($itembind == 1) {
   echo '<tr align="center" ><td><div class="item-font1">Text1</div></td>
</tr>';
} elseif ($itembind== 2) {
   echo '<tr align="center" ><td><div class="item-font1">Text2</div></td></tr>';
} else {
   echo "\t";
}
echo '<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td>
</tr>  
</table>';

It wouldn't be a bad idea to practice a tutorial or two before posting here.

Something that some new people don't realize is that you can pull the html out of the php a little. This same thing could be written:

<?php
if ($itembind == 1) $text = 'text1';
elseif ($itembind== 2) $text = 'text2';
if(isset($text)){
   $row = '<tr align="center" >
        <td>
            <div class="item-font1">'.$text.</div>
        </td>
    </tr>
} else {
  $row = '';
}

?>
<table style="border:1px solid red;">
    <tr align="center">
        <td>
            <div class="item-font1"><?=$mounts['name']?></div>
        </td>
    </tr>
    <?=$row?>
    <tr align="center" >
        <td>
            <div class="item-font1">'<?=$mounts['name']?></div>
        </td>
    </tr>  
</table>

Upvotes: 1

Jim Wright
Jim Wright

Reputation: 6058

Your syntax is wrong. You should use . to concatenate strings and ; to terminate lines.

You can also use double quotes (") and PHP will parse the string looking for variables. For example:

$myvar = 'example';
$singleQuote = 'This is an ' . $myvar;
$doubleQuote = "This is an {$myvar}";

I usually find double quoting strings easier when you are going to be using variables.

echo '<table style="border:1px solid red;">';
echo '<tr align="center"><td><div class="item-font1">' . $mounts['name'] . '</div></td></tr>';
if ($itembind == 1) {
    echo '<tr align="center" ><td><div class="item-font1">Text1</div></td></tr>';
} elseif ($itembind== 2) {
    echo '<tr align="center" ><td><div class="item-font1">Text2</div></td></tr>';
    echo "\t";
}
'<tr align="center" ><td><div class="item-font1">' . $mounts['name'] . '</div></td></tr></table>';

Upvotes: 1

Harpreet Singh
Harpreet Singh

Reputation: 979

Use . operator while concatenation. You are using comma , operator. See your code :-

echo '<table style="border:1px solid red;">','<tr align="center" ><td><div class="item-font1">', $mounts['name'], '</div></td></tr>',

should be

echo '<table style="border:1px solid red;">'.'<tr align="center" ><td><div class="item-font1">'. $mounts['name']. '</div></td></tr>'.

and same for rest of the code.

Upvotes: 1

Related Questions