简体   繁体   中英

PHP Display HTML retrieved from database

I'm retrieving some html source code that is stored in a database and storing that in a PHP variable. The html source code is the rows for a table and might look like this:

<tr><td>10:00 AM</td><td class="success">Available</td></tr> 
<tr><td>10:15 AM</td><td class="success">Available</td></tr> 
<tr><td>10:30 AM</td><td class="success">Available</td></tr> 

I'm storing this in a variable $tableRows.

In my PHP page I have some of the table code in the page and I'm attempting to include the table rows like this:

<table class="table table-condensed table-hover table-bordered">

<tr>
<th scope="col">Time</th>
<th scope="col"><?php  echo $date ; ?></th>
</tr>

<?php

echo $tableRows ;

?>          
</table>

In the browser I'm seeing the html source, eg I see:

<tr><td>10:00 AM</td><td class="success">Available</td></tr> 
<tr><td>10:15 AM</td><td class="success">Available</td></tr> 
<tr><td>10:30 AM</td><td class="success">Available</td></tr> 

instead of the rendered table? I've tried htmlentities but that's not working either.

try something like this:

<?php
$a='
<tr><td>10:00 AM</td><td class="success">Available</td></tr> 
<tr><td>10:15 AM</td><td class="success">Available</td></tr> 
<tr><td>10:30 AM</td><td class="success">Available</td></tr>'; 



echo '
<tr>
<th scope="col">Time</th>
<th scope="col"><?php  echo $date ; ?></th>
</tr>'.$a;

DEMO

This is giving me proper output you can try that

<?php
$tableRows='<tr><td>10:00 AM</td><td class="success">Available</td></tr> ';
echo $tableRows;
?>

Ummmmm.... Maybe your $tableRows variable has the HTML encoded, like this:

&lt;tr&gt;&lt;td&gt;10:00 AM&lt;/td&gt;&lt;td class=&quot;success&quot;&gt;Available&lt;/td&gt;&lt;/tr&gt; 
&lt;tr&gt;&lt;td&gt;10:15 AM&lt;/td&gt;&lt;td class=&quot;success&quot;&gt;Available&lt;/td&gt;&lt;/tr&gt; 
&lt;tr&gt;&lt;td&gt;10:30 AM&lt;/td&gt;&lt;td class=&quot;success&quot;&gt;Available&lt;/td&gt;&lt;/tr&gt;

In that case:

...
</tr>

<?php

echo html_entity_decode($tableRows);

?>          
</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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM