search

Home  >  Q&A  >  body text

The smartest way to generate duplicate HTML source code. Using PHP?

Hi, I would like to know what is the sensible way to generate duplicate HTML source code (shown below). Let's say I have 30 rows numbered from 1 to 30. I have a lot of pages like this that need this structure. I've used a PHP loop to generate this code in the page itself, since I'm using PHP in my project anyway.

My question is, is this a wise approach? What is the best practice in this situation? I'm not sure it's wise to use PHP to generate HTML every time in a page. Or is it?

Thank you for your guidance or suggestions.

<a href="images/mypic (1).jpg">
<img src="images/mypic (1).jpg" alt="Picture 1">
</a>

<a href="images/mypic (2).jpg">
<img src="images/mypic (2).jpg" alt="Picture 2">
</a>

etc..

P粉146080556P粉146080556460 days ago595

reply all(1)I'll reply

  • P粉014293738

    P粉0142937382023-09-11 14:01:50

    This is exactly what PHP is for: generating HTML code.

    You can use a simple for loop:

    <?php for($i=1; $i<=30; $i++) : ?>
      <a href="images/mypic (<?= $i ?>).jpg">
        <img src="images/mypic (<?= $i ?>).jpg" alt="Picture <?= $i ?>">
      </a>
    <?php endif; ?>
    
    

    reply
    0
  • Cancelreply