Php 尝试显示旋转木马滑块上显示的图像时出现问题
我目前正在尝试将它设置为我的数据库存储图像名称(如slider1.jpg)的位置,然后从数据库中检索图像名称,但问题是当我从表中选择*all时,它只显示表中的第一个链接。关于如何在代码中解决这个问题,有什么想法吗 这是我的PHP:Php 尝试显示旋转木马滑块上显示的图像时出现问题,php,Php,我目前正在尝试将它设置为我的数据库存储图像名称(如slider1.jpg)的位置,然后从数据库中检索图像名称,但问题是当我从表中选择*all时,它只显示表中的第一个链接。关于如何在代码中解决这个问题,有什么想法吗 这是我的PHP: <?php //Gets Links $stmt = $DB_con->prepare('SELECT * FROM slider'); $stmt->execute(); if($stmt->rowCo
<?php
//Gets Links
$stmt = $DB_con->prepare('SELECT * FROM slider');
$stmt->execute();
if($stmt->rowCount() > 0)
{
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
}
?>
这是我的旋转木马滑块
<header>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<!-- Slide One - Set the background image for this slide in the line below -->
<div class="carousel-item active" style="background-image: url('images/slider/<?php echo $row['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3>First Slide</h3>
<p>This is a description for the first slide.</p>
</div>
</div>
<!-- Slide Two - Set the background image for this slide in the line below -->
<div class="carousel-item" style="background-image: url('images/slider/<?php echo $row['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3>Second Slide</h3>
<p>This is a description for the second slide.</p>
</div>
</div>
<!-- Slide Three - Set the background image for this slide in the line below -->
<div class="carousel-item" style="background-image: url('images/slider/<?php echo $row['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3>Third Slide</h3>
<p>This is a description for the third slide.</p>
</div>
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</header>
您需要利用结果上的循环,以便可以使用数据库查询中的新$行构建每张幻灯片:
<?php
// build a clean array of slides from the db grab
$stmt = $DB_con->prepare('SELECT * FROM slider');
$stmt->execute();
$slides = [];
if($stmt->rowCount() > 0) {
$slides = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
?>
.... starter html ....
<ol class="carousel-indicators">
<?php foreach($slides as $i => $slide) { ?>
<li data-target="#carouselExampleIndicators"
data-slide-to="<?php echo $i;?>"
class="<?php echo (!$i?'active':'');?>"></li>
<?php }?>
</ol>
.... more html ....
<div class="carousel-inner" role="listbox">
<?php foreach($slides as $i => $slide) { ?>
<div class="carousel-item <?php echo (!$i?'active':'');?>"
style="background-image: url('images/slider/<?php echo $slide['link'];?>')">
<div class="carousel-caption d-none d-md-block">
<h3><?php echo $slide['slide_name'];?></h3>
<p><?php echo $slide['slide_desc'];?></p>
</div>
</div>
<?php } ?>
</div>
.... the rest of your html output ....
.... 初学者html。。。。
.... html输出的其余部分。。。。
我尝试了那个代码,但什么也没发生。只有大量的空白。当我将“div class=“carousel time active”放在旋转木马上时,它会同时显示所有图像,但不会显示在旋转木马中。我将幻灯片描述字段更改为幻灯片描述,将幻灯片名称字段改为幻灯片名称active,在旋转木马项目和控件上,这将是加载页面时的第一张幻灯片。没关系,我一开始就不知道如何从数据库中提取数据!谢谢你帮助我!