Skip to content Skip to sidebar Skip to footer

How To Properly Display This Php Block

I'm using data from MySQL database and display looks like this: I want 3 articles in one line. The pictures below are made with HTML and CSS. This is my code: while($row=mysqli_f

Solution 1:

This seems to be a nice point in your project to implement a template engine. There a couple out there. I really liked working with typo3 fluid, but back to your question... For your existing code, you can end your php code, use html and just echo the php variables you need.

<?php// ... some php stuff?>
<div class="container">
  /* somehtmlstuff * /
  <imgclass="malaSlika" src="<?phpechoUPLPATH . $row['slika']; ?>">
  /* somemorehtmlstuff */
  /* <?= isshortfor <?phpecho
  <imgclass="malaSlika" src="<?= UPLPATH . $row['slika']; ?>">
</div>
<?php
// ... somemorephpstuff

As said, maybe a template engine would help this even further. Edit: To add div class="row" it could be something like this.

<?php ... ?>

<div class="container">

<?php
$i = 0;
while($row=mysqli_fetch_array($result)) :

if($i % 3 == 0) : ?>
                <divclass="rowpadding">
<?phpendif; ?>
                    <divclass="col-md-4 col-xs-12">
                        <divclass="card">
                            <imgclass="card-img-top" src="<?phpechoUPLPATH . $row['slika']; ?>">
                                <divclass="card-body">
                                    <h6class="card-title">&laquo; <?phpecho $row['naslov']; ?> &raquo;</h4>
                                </div>
                                <pclass="ispod"><?phpecho $row['datum'];?> </p>

                        </div>

                    </div>

<?phpif($i % 3 == 0) : ?>
                </div>
<?phpendif; ?>



<?php
$i++;
endwhile;
?>

</div>

It will always get ugly at some point I think. This is a quick solution. the modulo (%) devides by the number and returns the rest value. so $i modulo 3 return 0 at 0, 3, 6, 9, 12 etc. Meaning it will always add the "row padding" div.

Post a Comment for "How To Properly Display This Php Block"