我尝试在每次用户访问时使用数据库中的随机结果生成6列和9行长电影网格。我很容易得到随机的54个结果,但是我无法将结果分开并将它们插入6列,每列9行。我试图打破并继续循环但没有运气。我怎样才能做到这一点?也许我应该在循环中运行循环?我目前的代码如下:
<?php
$i = 0;
$sql = "SELECT * FROM Movies ORDER BY RAND() LIMIT 54";
$gridmovies = $mysqli->query($sql);
if ($gridmovies->num_rows > 0) {
while($row = $gridmovies->fetch_assoc()) {
$i++
?>
<div class="column" style="margin-top: 0px;">
<a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
<div class="overlay ov-show">
<div class="icon"><i class="fa fa-heart"></i></div>
</div>
<img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
</a>
</div>
<?php if ($i == 9) {break;} ?>
<div class="column" style="margin-top: 40px;">
<?php continue; ?>
<a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
<div class="overlay ov-show">
<div class="icon"><i class="fa fa-heart"></i></div>
</div>
<img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
</a>
</div>
<?php if ($i == 18) {break;} ?>
<div class="column" style="margin-top: 80px;">
<?php continue; ?>
<a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
<div class="overlay ov-show">
<div class="icon"><i class="fa fa-heart"></i></div>
</div>
<img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
</a>
</div>
<?php if ($i == 27) {break;} ?>
<div class="column" style="margin-top: 120px;">
<?php continue; ?>
<a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
<div class="overlay ov-show">
<div class="icon"><i class="fa fa-heart"></i></div>
</div>
<img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
</a>
</div>
<?php if ($i == 36) {break;} ?>
<div class="column" style="margin-top: 160px;">
<?php continue; ?>
<a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
<div class="overlay ov-show">
<div class="icon"><i class="fa fa-heart"></i></div>
</div>
<img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
</a>
</div>
<?php if ($i == 45) {break;} ?>
<div class="column" style="margin-top: 200px;">
<?php continue; ?>
<a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
<div class="overlay ov-show">
<div class="icon"><i class="fa fa-heart"></i></div>
</div>
<img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
</a>
</div>
<?php
} $gridmovies->free();
} else {
echo 'error';
}
?>
答案 0 :(得分:0)
首先尝试将结果放在一个地方并将其显示在其他地方,这样你的代码就会有一些分离:(相信我,这将在未来帮助你)
$sql = "SELECT * FROM Movies ORDER BY RAND() LIMIT 54";
$gridmovies = $mysqli->query($sql);
$movies = [];
if ($gridmovies->num_rows > 0) {
while ($row = $gridmovies->fetch_assoc()) {
$movies[] = $row;
}
}
为了制作网格(二维实体),您需要两个循环(每个维度的每个循环)。一个用于行,一个用于列,如下所示:
$rows = 9;
$columns = 6;
$index = 0;
for ($i = 0; $i < $columns; $i++) {
if (!isset($movies[$index])) {
break;
}
?>
<div class="column">
<?php
for ($j = 0; $j < $rows; $j++) {
if (isset($movies[$index])) {
$row = $movies[$index];
// your html here
?>
<a href="/similar/<?php echo $row['ImdbID']; ?>" class="item-card">
<div class="overlay ov-show">
<div class="icon"><i class="fa fa-heart"></i></div>
</div>
<img src="<?php echo $row['PosterURL']; ?>" alt="<?php echo $row['EngTitle']; ?> / <?php echo $row['GeoTitle']; ?>">
</a>
<?php
} else {
break;
}
$index++;
}
?>
</div >
<?php
}
这样可以避免重复代码。生成所有内容,如果您想要以4乘4更改网格格式,这很简单,您只需更改$rows
和$columns
变量即可。
修改强> 请查看此示例及其生成方式:{{3}}
此代码未经过测试。它可能需要一些调整