PHP <ul> <li>数据库</li> </ul>

时间:2015-03-16 19:30:20

标签: php html css tags

我正在尝试将数组中的每部电影的图像,标题和评级放入<ul> <li>,但我只能将第一部电影,标题和评级放入其中。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


<?php
require_once 'init.php';
require_once 'sessionCheck2.php';

$query = $con ->query
("
SELECT movies.id, movies.image, movies.movie_name, AVG(user_movie_ratings.rating) AS rating
FROM movies
LEFT JOIN user_movie_ratings
ON movies.id = user_movie_ratings.movie_id
GROUP BY movies.id
");

$movieTitle = array();

while($row = $query->fetch_object())  {
$movieTitle[] = $row;
}

?>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>


<body>

<div id="wrapper">

    <div id="header">

        <div id="h1">

        </div>

        <div id="login">
            <?php //include 'loginCheck.php';?>
            <?php include 'login.php';?>
        </div>
    </div>

<div id ="movies">

        <?php foreach($movieTitle as $movie):?>

            <?php echo "<ul> <li>" .'<img src="'.$movie->image.'"/>';?>

                <div class = "cFFF">
                <?php echo $movie->movie_name; ?>
                </div>
                <div class="s11 c999">
                    Rating: <?php echo round($movie->rating); ?>
                </div>
            <?php echo "</li> </ul>"; ?>

            </div>
        <?php endforeach; ?>

    <div id="footer">
        This is the Footer
    </div>

</div>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

从逻辑上思考你打开<ul><li>的位置与关闭它们的位置(即它应该在循环中还是在外面,显然它不应该被打开以便打开并且关闭,无论是打开还是关闭都进入循环或外部,而不是一进一出)并在结束时修复<?ul>的拼写错误。

并且不要像<ul><li><div></li></ul></div>那样。您需要按照打开它们的顺序关闭标记。即<ul><li><div></div></li></ul>

我想你想要做的事情:

<div id="movies">
<ul>
<?php 
foreach($movieTitle as $movie)
{
    echo '<li>';
    echo '<img src="' . $movie->image . '" />';
    echo '<div class="cFFF">' . $movie->movie_name . '</div>';
    echo '<div class="s11 c999">Rating: ' . round($movie->rating) . '</div>';
    echo '</li>';
}
?>
</ul>
</div>

答案 1 :(得分:0)

你也可以通过使用下面的php语句打印$ movieTitle数组的输出来检查:

echo count($ movieTitle);

确保您的MySQL JOIN返回正确的结果。