I strongly recommend a templating library like Twig or Mustache. However, the basics would be to use external PHP files as HTML, and use require. Here is a bit of a hacky example:
<?php
$comments = array();
while (mysqli_stmt_fetch($stmt)) {
$comments[] = $stmt;
}
require 'comments.php';
Then in comments.php
:
<?php foreach ($comments as $comment) : ?>
<tr>
<td><?php echo $comment['title'] ?></td>
<td><?php echo $comment['date'] ?></td>
<td align='center'>
<a href='#'>
<span class='view' title='View This Comment'></span>
</a>
</td>
<td class='td_catchall' align='center'>
<a href='#'>
<span class='edit' title='Edit This Comment'></span>
</a>
</td>
<td align='center'>
<a href='#'>
<span class='delete' title='Delete This Comment'></span>
</a>
</td>
</tr>
<?php endforeach ?>
Here, I am adding each comment (or whatever it may be) to an array. Then, I include a file called comments.php
. Your main file should be mostly PHP and should handle any logic, while comments.php
should be mostly HTML and only use PHP for presentation (aka looping through an array and echoing out variables).
Your required file has access to all the variables it would have access to if it were inline.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…