我是JS,HTML和PHP的新手,通常我用C,C ++,Java编程......
我正在尝试做一个简单的页面,用SQL调用PHP一些数据,然后显示它们:
文本部分很好,但我不知道如何多次调用该脚本来创建多个独立幻灯片


 <脚本>
 var slideIndex = 1;
 showSlides(slideIndex);

 function plusSlides(n){
 showSlides(slideIndex + = n);
}

 function currentSlide(n){
 showSlides(slideIndex = n);
}

 function showSlides(n){
 var i;
 var slides = document.getElementsByClassName(“mySlides”);
 var dots = document.getElementsByClassName(“dot”);
 if(n> slides.length){slideIndex = 1}
 if(n< 1){slideIndex = slides.length}
 for(i = 0; i< slides.length; i ++){
 slides [i] .style.display =“none”; 
 }
 for(i = 0; i< dots.length; i ++){
 dots [i] .className = dots [i] .className.replace(“active”,“”);
 }
 slide [slideIndex-1] .style.display =“block”; 
点[slideIndex-1] .className + =“active”;
}
< / script>

<?php
 $ connection = mysqli_connect('HOST ','NAME','PASS')或死('无法连接:'。mysqli_error()); 
 mysqli_select_db($ connection,'DATABASE')或die('nulla:D'); 
 $ query =“SELECT marca,modello,anno,frontale,laterale,retro FROM table ORDER BY modello ASC”;
 $ result = mysqli_query($ connection,$ query)or die('non stampa: D');

 // conto il numero di occorrenze trovate nel db
 $ numrows = mysqli_num_rows($ result);



 // avvio un ciclo for che si ripete per il numero di occorrenze trovate
 for($ x = 0; $ x< $ numrows; $ x ++)
 {
 // recupero il contenuto di ogni record trovato
 $ resrow = mysqli_fetch_row($ result);
 $ marca = $ resrow [0];
 $ modello = $ resrow [1];
 $ anno = $ resrow [2];
 $ frontale = $ resrow [3];
 $ laterale = $ resrow [4];
 $ retro = $ resrow [5];

 // stampo a video il risultato
 echo“modello:”。 $马卡报; 
 ?>
 < br />
 < PHP
回声“马卡:”。 $ MODELLO; 
 ?>
 < br />
 < PHP
 echo“anno:”。 $安诺;
 ?>
 < br />

 < div class =“slideshow-container”>
 < div class =“mySlides fade”>
 < div class =“numbertext”> 1/3< / div>
 < img src =“<?php echo”$ frontale“;?>”风格= “宽度:100%” >
 < div class =“text”>标题文字< / div>
 < / DIV>

 < div class =“mySlides fade”>
 < div class =“numbertext”> 2/3< / div>
 < img src =“<?php echo”$ laterale“;?>”风格= “宽度:100%” >
 < div class =“text”> Caption Two< / div>
 < / DIV>

 < div class =“mySlides fade”>
 < div class =“numbertext”> 3/3< / div>
 < img src =“<?php echo”$ retro“;?> g”style =“width:100%”>
 < div class =“text”>标题三< / div>
 < / DIV>

 < a class =“prev”onclick =“plusSlides(-1)”>❮< / a>
 < a class =“next”onclick =“plusSlides(1)”>❯< / a>
< / div>
< br>
& #xA;< div style =“text-align:center”>
 < span class =“dot”onclick =“currentSlide(1)”>< / span> 
 < span class =“dot”onclick =“currentSlide(2)”>< / span> 
 < span class =“dot”onclick =“currentSlide(3)”>< / span> 
< / DIV>




< PHP
 }

 // chiudo la connessione
 mysqli_close($ connection);
?>



答案 0 :(得分:0)
你几乎可以做那件事。
<script>
slideIndex = 1; // This is "global" so you can reference it later.
//showSlides(slideIndex); Removed (see second script tag)
// Function definitions are correct
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
}
</script>
...
现在稍后,您将可以访问以上所有内容,因此您可以执行以下操作:
<script>
showSlides(slideIndex);
slideIndex = 2;
</script>
<script>
showSlides(slideIndex);
slideIndex = 3;
</script>
这只是演示了&#34;全球&#34;对象的状态(函数是对象)和变量slideIndex
(有点像在主范围内,因为它没有明确作用域。)
希望这有帮助。