我怎样才能在 php 中重复一段时间?
How can i repeat a while cicle in php?
我需要创建一个 'wall' 图片。
这是我的代码:
<?php
$q = $db->prepare("SELECT * FROM wall");
$q->execute();
while($row = $q->fetch(PDO::FETCH_ASSOC)) { ?>
<div class="col-4-perc"><img src="<?=$site?>/assets/img/wall/<?=$row['source']?>.png"
alt="Foto wall" class="img-fluid"></div>
<?php } ?>
我想重复 while 循环,将数据库中的照片重复至少 3/4 次,但我不知道该怎么做。
正如评论中所解释的,你必须在一段时间内使用 FetchAll
而不是 fetch
。这是您想要的结果的示例脚本:
$q = $db->prepare("SELECT * FROM wall");
$q->execute();
$allImage = $q->fetchAll() ;
$limit = 100 ;
$activeKey = 0 ;
for ($i = 0; $i < $limit ;$i++){
if (!isset($allImage[$activeKey]['source'])) {
$activeKey = 0 ;
// if the line does not exist, reset to the first key (0)
}
echo "<div class='col-4-perc'><img src='{$site}/assets/img/wall/{$allImage[$activeKey]['source']}.png'
alt='Foto wall' class='img-fluid'></div>";
$activeKey++ ;
}
图像保存在 $allImage
中,table 带有数字索引。接下来我们只需要循环使用 $limit
(你想要显示的最大图像)。
而$activeKey
为当前显示的图片,如果没有设置$allImage[$activeKey]
,则重置为0
。
我需要创建一个 'wall' 图片。 这是我的代码:
<?php
$q = $db->prepare("SELECT * FROM wall");
$q->execute();
while($row = $q->fetch(PDO::FETCH_ASSOC)) { ?>
<div class="col-4-perc"><img src="<?=$site?>/assets/img/wall/<?=$row['source']?>.png"
alt="Foto wall" class="img-fluid"></div>
<?php } ?>
我想重复 while 循环,将数据库中的照片重复至少 3/4 次,但我不知道该怎么做。
正如评论中所解释的,你必须在一段时间内使用 FetchAll
而不是 fetch
。这是您想要的结果的示例脚本:
$q = $db->prepare("SELECT * FROM wall");
$q->execute();
$allImage = $q->fetchAll() ;
$limit = 100 ;
$activeKey = 0 ;
for ($i = 0; $i < $limit ;$i++){
if (!isset($allImage[$activeKey]['source'])) {
$activeKey = 0 ;
// if the line does not exist, reset to the first key (0)
}
echo "<div class='col-4-perc'><img src='{$site}/assets/img/wall/{$allImage[$activeKey]['source']}.png'
alt='Foto wall' class='img-fluid'></div>";
$activeKey++ ;
}
图像保存在 $allImage
中,table 带有数字索引。接下来我们只需要循环使用 $limit
(你想要显示的最大图像)。
而$activeKey
为当前显示的图片,如果没有设置$allImage[$activeKey]
,则重置为0
。