php - Looping results to insert new div with complexities -
how can loop results insert new div complexities each result?
i have following database table following columns:
id | userid | title | introduction | content | images | background | date | pinned
i have following php code:
if($latest = $con->query("select * posts pinned='0' limit 4")) { if($latest->num_rows > 0) { //<-- result loop here } else { echo '<h1 class="alert fade">no posts</h1>'; } $latest->close(); };
i format output follows:
<div class="post standard" style="background-image:url([1]);"> <a href="view.php?id=[2]"> <div class="shader"></div> <div class="info"> <h1>[3]</h1> <p>[4] - [5]</p> </div> </div> [1] - background [2] - id [3] - title [4] - userid [5] - date
how accomplish this?
here code should need. used 1 echo
, can split more echo
es, or go out of php block. doesn't matter.
<?php if ($latest->num_rows > 0) { while ($row = $latest->fetch_assoc()) { echo ' <div class="post standard" style="background-image:url(' . $row['background'] . ');"> <a href="view.php?id=' . $row['id'] . '"> <div class="shader"></div> <div class="info"> <h1>' . $row['title'] . '</h1> <p>' . $row['userid'] . ' - ' . $row['date'] . '</p> </div> </div> '; } } ?>
example putting html code outside php block.
<?php if ($latest->num_rows > 0) { while ($row = $latest->fetch_assoc()) { ?> <div class="post standard" style="background-image:url('<?php echo $row['background']; ?>');"> <a href="view.php?id=<?php echo $row['id'] ?>"> <div class="shader"></div> <div class="info"> <h1><?php echo $row['title']; ?></h1> <p><?php echo $row['userid']; ?> - <?php echo $row['date']; ?></p> </div> </div> <?php } } ?>
Comments
Post a Comment