Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

html - PHP displaying a certain amount of records per div

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...