Sam Ram San Sam Ram San - 5 months ago 8
PHP Question

How can this be packed into a foreach loop

The query and the fetching are working properly, but it's too repetitive, I tried (applying some tutorials on for loops) but can't seem to figure out.

<div class="spctcls">
<div class="artc_cnt">
<a class="art_liga" href="efecto.php?libelula=noticias&artic=<?= $articulos[0]['id']; ?>&gen=<?= $articulos[0]['genero']; ?>&id=<?= $articulos[0]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[0]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[0]['id']; ?>&gen=<?= $articulos[0]['genero']; ?>&id=<?= $articulos[0]['id'] ?>">
<?= $articulos[0]['titulo']; ?>
</a>
</a>
</div>
</div>


<div class="spctcls">
<div class="artc_cnt">
<a href="efecto.php?libelula=noticias&artic=<?= $articulos[1]['id']; ?>&gen=<?= $articulos[1]['genero']; ?>&id=<?= $articulos[1]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[1]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[1]['id']; ?>&gen=<?= $articulos[1]['genero']; ?>&id=<?= $articulos[1]['id'] ?>">
<?= $articulos[1]['titulo']; ?>
</a>
</a>
</div>
</div>


<div class="spctcls">
<div class="artc_cnt">
<a href="efecto.php?libelula=noticias&artic=<?= $articulos[2]['id']; ?>&gen=<?= $articulos[2]['genero']; ?>&id=<?= $articulos[2]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[2]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[2]['id']; ?>&gen=<?= $articulos[2]['genero']; ?>&id=<?= $articulos[2]['id'] ?>">
<?= $articulos[2]['titulo']; ?>
</a>
</a>
</div>
</div>


<div class="spctcls">
<div class="artc_cnt">
<a href="efecto.php?libelula=noticias&artic=<?= $articulos[3]['id']; ?>&gen=<?= $articulos[3]['genero']; ?>&id=<?= $articulos[3]['id'] ?>">
<div class="artc_foto">
<img src="img/chica/<?= $articulos[3]['foto']; ?>" alt="" />
</div>
<a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulos[3]['id']; ?>&gen=<?= $articulos[3]['genero']; ?>&id=<?= $articulos[3]['id'] ?>">
<?= $articulos[3]['titulo']; ?>
</a>
</a>
</div>
</div>


It's working OK but I want to make it lightweight.

Thank You for any suggestions.

Answer

It's kind of easy really:

<?php foreach ($articulos as $articulo) { ?>
      <div class="spctcls">
          <div class="artc_cnt">
            <a class="art_liga" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
              <div class="artc_foto">
                <img src="img/chica/<?= $articulo['foto']; ?>" alt="" />
              </div>
              <a class="art_titl" href="efecto.php?libelula=noticias&artic=<?= $articulo['id']; ?>&gen=<?= $articulo['genero']; ?>&id=<?= $articulo['id'] ?>">
                <?= $articulo['titulo']; ?>
              </a>
            </a>
          </div>
        </div>
<?php } ?>