Multiple Slideshows for all Records on one page
2 posts by 2 authors in: Forums > CMS Builder
Last Post: July 28, 2021 (RSS)
By kovali - July 20, 2021
Hi, for a carparts website, I want to have an images slideshow for all possible +50 carparts (Records), all on one webpage.
I found this slideshow code to be looking perfect for what I need: https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_two
But I need many more slideshows one the same webpage ofcourse...
On my website this would look like this example: https://www.remar-autoafbraak.be/new/vw_onderdelen.php
Problem I have is that I need to increase many arrays by +1 for every next carpart/slideshow...
<?php foreach ($ra_vw_onderdelenRecords as $record): ?>
<?php echo htmlencode($record['title']) ?>
<?php echo htmlencode($record['content']) ?>
<?php foreach ($record['foto_s'] as $index => $upload): ?>
<img class="mySlides1" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides1" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides1" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<?php endforeach ?>
<?php endforeach ?>
So for every extra carpart I need to increase class="mySlides1" to class="mySlides2", class="mySlides3", class="mySlides4", class="mySlides5" etc...
<img class="mySlides2" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides2" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides2" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides3" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides3" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides3" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides4" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides4" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides4" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
...
And also in the script at page bottom I need to increase the value of some parameters by +1 for every carpart
<script>
var slideIndex = [1,1,1,1,1]; (extra ",1" for each carpart/slideshow)
var slideId = ["mySlides1", "mySlides2", "mySlides3", "mySlides4"] (etc. for each carpart/slideshow)
showDivs(1, 0);
showDivs(1, 1);
showDivs(1, 2);
function plusDivs(n, no) {
showDivs(slideIndex[no] += n, no);
}
function showDivs(n, no) {
var i;
var x = document.getElementsByClassName(slideId[no]);
if (n > x.length) {slideIndex[no] = 1}
if (n < 1) {slideIndex[no] = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex[no]-1].style.display = "block";
}
</script>
How can I automate this so that when I have e.g. 25 carparts I can show 25 slideshows on the page ?
I know I should increase these values depending on the amount of carparts, but I can't find the answer or php code to achieve this... :-(
Please help, thanks a lot!
Koen
By daniel - July 28, 2021
Hi Koen,
I think I see how this can be accomplished.
In your main loop you'll want to create a counter variable that increments each time you display a new slideshow, and use that value to update each class. Something like this:
<?php $count = 0; ?>
<?php foreach ($ra_vw_onderdelenRecords as $record): ?>
<?php $count++; ?>
<?php echo htmlencode($record['title']) ?>
<?php echo htmlencode($record['content']) ?>
<?php foreach ($record['foto_s'] as $index => $upload): ?>
<img class="mySlides<?php echo $count; ?>" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides<?php echo $count; ?>" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<img class="mySlides<?php echo $count; ?>" src="<?php echo $upload['urlPath'] ?>" style="width:100%">
<?php endforeach ?>
<?php endforeach ?>
Then within the script, we can use the end result of that counter variable to produce the necessary changes:
<script>
var slideIndex = [<?php
for ($x = 1; $x <= $count; $x++) {
if ($x != 1) { echo ', '; }
echo 1;
}
?>];
var slideId = [<?php
for ($x = 1; $x <= $count; $x++) {
if ($x != 1) { echo ', '; }
echo '"mySlides' . $x . '"';
}
?>];
showDivs(1, 0);
showDivs(1, 1);
showDivs(1, 2);
function plusDivs(n, no) {
showDivs(slideIndex[no] += n, no);
}
function showDivs(n, no) {
var i;
var x = document.getElementsByClassName(slideId[no]);
if (n > x.length) {slideIndex[no] = 1}
if (n < 1) {slideIndex[no] = x.length}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex[no]-1].style.display = "block";
}
</script>
The one thing I wasn't sure is if the "showDivs()" functions also needed to be modified with each new slideshow, so I did not change it.
Let me know if that helps get your slideshows working, or if you have any other questions!
Thanks,
Technical Lead
interactivetools.com