Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

How can HTML outside of php tags do this?

New Here ,
Mar 14, 2007 Mar 14, 2007

Copy link to clipboard

Copied

Can someone explain to me how this segment of code works please? The part I don't understand is how the html tags inside the while loop are able to generate many rows when they are just once and not contained within php tags. I thought I would need something like echo "<tr>" or need to use printf function in order to write the html tags using php. Instead <tr> and <td> tags are not within the <?php /> tags so how can the table be dynamic and grow and shrink if php isn't writing the individual rows on each of the loops?

TOPICS
Server side applications

Views

269
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 14, 2007 Mar 14, 2007

Copy link to clipboard

Copied

In the section:

<?php while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)){?>
<tr>
<td><?php echo $newArray['fldFirstName']; ?></td>
<td><?php echo $newArray['fldLastName']; ?></td>
</tr>
<?php } ?>

PHP repeats everything between while { and } for as many records as are
retrieve by mysql_fetch_array. It doesn't matter that the HTML tags aren't
echoed by PHP, the PHP processor will still repeat everything inside the
while loop. In the end the whole page is a PHP page, so the whole page is
run through the PHP processor, not just the PHP tags, so its able to repeat
the HTML as many times as needed.

--
Gareth
http://www.phploginsuite.co.uk/
PHP Login Suite V2 - 34 Server Behaviors to build a complete Login system.


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 14, 2007 Mar 14, 2007

Copy link to clipboard

Copied

cscampste wrote:
> Can someone explain to me how this segment of code works please? The part I
> don't understand is how the html tags inside the while loop are able to
> generate many rows when they are just once and not contained within php tags. I
> thought I would need something like echo "<tr>" or need to use printf function
> in order to write the html tags using php. Instead <tr> and <td> tags are not
> within the <?php /> tags so how can the table be dynamic and grow and shrink if
> php isn't writing the individual rows on each of the loops?

It's a common misconception that everything has to be displayed using
echo. PHP is an embedded language. When the PHP engine sees an opening
PHP tag it starts parsing the PHP, when it sees a closing tag, it just
outputs the HTML.

The closing brace of the while loop is after the table row. It tells the
PHP engine to go back to the top of the loop and to continue doing so
until the condition is no longer true.

If you study the code generated by Dreamweaver for a repeat region,
you'll see that it uses the same principle. The only difference is that
Dreamweaver uses a do... while loop, rather than a simple while loop.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 14, 2007 Mar 14, 2007

Copy link to clipboard

Copied

LATEST
Oh that was my mistake. I thought everything had to be within <?php ?> tags and any html had to be echoed to the page. I see I still have a lot to learn 🙂 Thanks for the explanation guys.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines