Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP do-while with HTML in between

Tags:

html

php

I am trying to write PHP code to loop through an array to create an HTML table. I have been trying to do something like:

<div id="results">
<table class="sortable">
    <?php $results = $statement->fetchAll(PDO::FETCH_ASSOC); ?>
    <?php do: ?>
        <tr>
            <?php for ($i = 0; $i < count($columns); $i++): ?>
                 <td><?php echo $row[$i] ?></td>
            <?php endfor; ?>
        </tr>
    <?php while (($row = next($results)) != false); ?>
</table>
</div>

So 2 questions:

  1. Is there an equivalent do-while syntax as there is a for, if, or foreach syntax in PHP, where you can split the PHP code up and have HTML in between?

  2. What is this called when you split PHP code up with HTML in between? (if there is a special term for it)

like image 536
blastthisinferno Avatar asked May 05 '26 08:05

blastthisinferno


2 Answers

I do not know of a do while syntax that behaves like that, but you can still end your PHP block like this:

<div id="results">
<table class="sortable">
    <?php $results = $statement->fetchAll(PDO::FETCH_ASSOC); ?>
    <?php do { ?>
        <tr>
            <?php for ($i = 0; $i < count($columns); $i++): ?>
                 <td><?php echo $row[$i] ?></td>
            <?php endfor; ?>
        </tr>
    <?php } while (($row = next($results)) != false); ?>
</table>
</div>
like image 114
Residuum Avatar answered May 06 '26 23:05

Residuum


You can use curly brackets:

<?php do { ?>
foo
<?php } while ($i--); ?>
like image 27
Sjoerd Avatar answered May 06 '26 22:05

Sjoerd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!