tags:

views:

91

answers:

7

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)

A: 

You can mix php and html in every kind of loop, but your current loop does will not work because $row is not defined the first time it gets there.

jeroen
Yes. http://www.php.net/manual/en/control-structures.while.php can be used for a while loop where the condition is checked evey time the loop is run, instead of do-while where the condition is not checked the 1st time.
James
+3  A: 

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>
Residuum
do/while fails in this implementation because on the first execution, $row is undefined.
Fosco
+1  A: 

You can use curly brackets:

<?php do { ?>
foo
<?php } while ($i--); ?>
Sjoerd
+1  A: 

While I've never used the colon syntax as in your example, everything looks basically right except that on your first time through $row is unassigned.

I would switch it around to look like this:

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

This is excessive use of the embedded php tags.. When there is more PHP than HTML, you're better off using PHP and echoing the HTML.

Fosco
A: 

It doesn't have a special name :)

You may find using the heredoc syntax a bit cleaner than stepping in and out of PHP / HTML like this, though: http://php.net/manual/en/language.types.string.php You can use variables inside heredoc blocks so you'll be able to access your database results actually inline without dancing about with braces and new lines.

hollsk
A: 

1) Your best bet is to echo the HTML in strings, all within the <%php ... ?> tag. What you're doing now is an empty for loop inside of a do-while, which is essentially pointless.

2) I believe that's called "inline" coding, where the code and HTML intermingle, but the practice is frowned upon because you generally want to separate logic (PHP) and content (HTML) when developing for the web.

danyim
"inline coding" is most certainly not frowned upon - it's used extensively by the vast majority of PHP+(x)HTML projects.While separating logic and content is definitely preferable, your first echo-ing suggestion doesn't do that either, and is far less readable and less maintainable than "inline" coding.
lucideer
+1  A: 

No. From http://php.net/manual/en/control-structures.alternative-syntax.php :

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch.

On the other hand, do { ?> ... <?php } while(...) will work just fine.

What you're trying to do can be done with two foreach loops :

<div id="results">
<table class="sortable">
    <?php foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row): ?>
        <tr>
            <?php foreach ($row as $element): ?>
                 <td><?php echo $element ?></td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>
</div>

It solves the problem of having to initialize $row (and handling the empty list special case).

I'm not aware of any specific name for this, but I suspect that if you said "embedded HTML inside my PHP", people would understand.

Victor Nicollet
This answer actually solved my problem. Thanks!
blastthisinferno