Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Alright, so I am currently working on a website and for a section of the website I am trying to display some data from an SQL database. And I want the data to display in a such a way as seen here:

      <div class="project-list">
                    <ul>
                     <?php
                    try {

                        $stmt = $db->query('SELECT movieID, movieName, movieDesc, movieYear FROM latest ORDER BY movieID DESC');
                        while($row = $stmt->fetch()){
                                    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 '<li>';
                        }

                    } catch(PDOException $e) {
                        echo $e->getMessage();
                    }
                ?>
                    </ul>
                </div>

Essentially this is supposed to display a record, and it does however, my issue is that the way my style sheet is setup is it will only allow for three project divs to display per project-list/row. And I can't quite figure out how to make it so I can only display 3 records per this div and reset with PHP.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();
}
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...