gpt4 book ai didi

javascript - foreach slider 仅显示一个数据库条目

转载 作者:行者123 更新时间:2023-11-29 06:38:34 24 4
gpt4 key购买 nike

我有一个数组,它从数据库表中获取所有推荐并通过 foreach 数组显示它们。我想要某种 slider 来淡入和淡出结果。

这是我的查询:

$showTestimonials = array(); 
$getTestimonials = mysqli_query($mysqli,"SELECT * FROM testimonials ORDER BY testimonial_id DESC") OR die (mysqli_error($mysqli));

while($row = mysqli_fetch_array($getTestimonials)){
$row = array(
'testimonialName' => $row['name'],
'testimonialMessage' => $row['message']);
$showTestimonials[] = $row;
}

我的 PHP foreach 代码:

$count = 0; 
foreach ($showTestimonials as $stt):
echo '<div class="content welcome features container">';
echo '<div class="content welcome features testimonial">';
echo '<div id="goSlide">';

if($count == 0) {
echo '<div class="slide show">';
echo '<div>'.$stt['testimonialMessage'].' '.$stt['dModel'].'</div>';
echo '<div>'.$stt['testimonialName'].'</div>';
echo '</div>';
}

echo '</div>';
echo '</div>';
echo '</div>';
$count++;
endforeach;

我的一段 Javascript 代码

$(document).ready(function(){
var slides = $('#goSlide > .slide').length;
var current = 0;

setInterval(function() {
var next = ( ( current + 1 ) == slides ? 0 : current + 1 );

$( $( "#goSlide > .slide" )[ current ] ).fadeOut(1000).removeClass(".show");
$( $( "#goSlide > .slide" )[ next ] ).fadeIn(1000);

current = next;
}, 4000);

});

它目前只显示一个(数据库中最新的)并且只淡入和淡出那个。它不显示其他推荐。

最佳答案

问题是你在循环中更新 $row

while($row = mysqli_fetch_array($getTestimonials)){
$row = array( <---- here $row is getting updated
'testimonialName' => $row['name'],
'testimonialMessage' => $row['message']);
$showTestimonials[] = $row;
}

改成

while($row = mysqli_fetch_array($getTestimonials)){
$row_1 = array(
'testimonialName' => $row['name'],
'testimonialMessage' => $row['message']);
$showTestimonials[] = $row_1;
}

关于javascript - foreach slider 仅显示一个数据库条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22946419/

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