我目前正试图将它放在我的数据库存储图像名称(如slider1.jpg)的位置,然后我从我的数据库中检索它,但问题是当我从表中选择* all时仅显示表格中的第一个链接。关于如何在我的代码中修复此问题的任何想法?
继承我的PHP:
<?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>
答案 0 :(得分:2)
您需要在结果上使用循环,以便您可以使用数据库查询中的新$行构建每张幻灯片:
<?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 ....