execut-6ren">
gpt4 book ai didi

PHP Bootstrap 轮播

转载 作者:行者123 更新时间:2023-11-29 10:09:57 29 4
gpt4 key购买 nike


我有这段代码可以从数据库中获取轮播图片或视频

<小时/>
        $stmt = $con->prepare("SELECT * FROM `Main`");
$stmt->execute();
$rows = $stmt->fetchAll();
return $rows;

<小时/>此代码用于显示轮播
它必须仅显示位于数据库中的图片或视频

<小时/>
<?php
$check = $pageClass->mainPage("check");
if ($check > 0) {
$sliders = $pageClass->mainPage("select","Slider");
$allowedImg = array("png","jpg","jpeg","bmp","gif");
$allowedVideo = array("mp4","mkv","flv","avi","3gp");
?>
<section class="clients-opinion">
<div class="container">
<header class="text-center">
<h3 class="h1">See what our graduates are <br />saying about the plan</h3>
</header>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php
for ($i = 0;$i < count($sliders);$i++) {
if ($i == 0) {?>
<li data-target="#myCarousel" data-slide-to="<?php echo $i; ?>" class='active'></li>
<?php } else { ?>
<li data-target="#myCarousel" data-slide-to="<?php echo $i; ?>"></li>
<?php }
}?>
</ol>
<div class="carousel-inner">
<?php
foreach ($sliders as $slider) {
for ($i = 0; $i < count($sliders); $i++) {
if ($i == 0) {?>
<div class="item active">
<?php
$explodedSlider = explode(".", $slider['Slider']);
$sliderType = strtolower(end($explodedSlider));
if (in_array($sliderType, $allowedImg)) {?>
<img src="<?php echo $imgUploads . $slider['Slider']; ?>" alt="">
<?php } else if (in_array($sliderType, $allowedVideo)) { ?>
<video controls="controls">
<source src="<?php echo $vidUploads . $slider['Slider']; ?>" type="video/<?php echo $sliderType; ?>" />
</video>
<?php } ?>
</div><?php
} else {?>
<div class="item">
<?php
$explodedSlider = explode(".", $slider['Slider']);
$sliderType = strtolower(end($explodedSlider));
if (in_array($sliderType, $allowedImg)) {?>
<img src="<?php echo $imgUploads . $slider['Slider']; ?>" alt="">
<?php } else if (in_array($sliderType, $allowedVideo)) { ?>
<video controls="controls">
<source src="<?php echo $vidUploads . $slider['Slider']; ?>" type="video/<?php echo $sliderType; ?>" />
</video>
<?php } ?>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</section>

<小时/>然而,这输出了 9 张图片,
虽然我的数据库中只有 3 张图片。
请帮忙!
提前致谢!

最佳答案

您在 foreach 中使用 for 循环,这将执行 3x3,因此是 9

foreach ($sliders as $slider) {
for ($i = 0; $i < count($sliders); $i++) {
if ($i == 0) {
// Your code

只需删除 for 循环即可。

foreach ($sliders as $i => $slider) {
if ($i == 0) {
// Your code

关于PHP Bootstrap 轮播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51178113/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com