If I got your question right, you want to limit the amount of divs to 3 with PHP. Try adding a LIMIT 3
to your SQL-query:
$stmt = $db->query('SELECT movieID, movieName, movieDesc, movieYear FROM latest ORDER BY movieID DESC LIMIT 3');
I also noticed that there is one echo '<li>';
out of place near the end of your while loop. This may or may not be a problem but I would just delete this if there is not need for it.
Edit: May I have another try?
If I got you right this time, you want PHP to generate any number of <div class="project-list">
with a maximum of 3 <li>
inside each of them, right?
Here you go:
<?php
try {
// this line is for my local setup, you have to change or remove it:
$db = new PDO('mysql:host=localhost;dbname=stackmovies;charset=utf8', 'root', '');
$stmt = $db->query('SELECT movieID, movieName, movieDesc, movieYear FROM latest ORDER BY movieID DESC');
$i = 0;
while($row = $stmt->fetch()){
echo "
"; // for debugging
if ($i % 3 == 0) {
echo '<div class="project-list">';
echo '<ul>';
}
echo "
"; // for debugging
echo '<li>';
echo '<a href="#" class="no-underline">';
echo '<div class="project">';
echo '<h2 class="project-date">'.$row['movieYear'].'</h2>';
echo '<div class="poster"><img src="assets/img/FoTD_Poster.png" width="160" height="188"></div>';
echo '<h2 class="project-title" align="center"><b>'.$row['movieName'].'</b></h2>';
echo '</a>';
echo '</div>';
echo '</li>';
echo "
"; // for debugging
if ($i % 3 == 2) {
echo '</ul>';
echo '</div>';
}
echo "
"; // for debugging
$i++;
}
if ($i > 0 && ($i - 1) % 3 != 2) {
echo '</ul>';
echo '</div>';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…