2
I’m using the "Vegas Background Slideshow" library in a project with the following code:
<script>
$("#main").vegas({
slides: [
{ src: "assets/site/img/uploads/banners/01.jpg" },
{ src: "assets/site/img/uploads/banners/02.jpg" },
{ src: "assets/site/img/uploads/banners/03.jpg" },
{ src: "assets/site/img/uploads/banners/04.jpg" }
],
animation: 'random',
align: 'center',
valign: 'center',
cover: 'false',
delay: 9000,
transitionDuration: 1000,
timer: false,
overlay: 'assets/site/vendors/vegas/overlays/07.png',
walk: function(index) {
if (index == 2) {
$('#info').text("Slide index " + index);
}
}
});
$('a.slide-previous').on('click', function(e) {
$("#main").vegas('previous');
e.preventDefault();
});
$('a.slide-next').on('click', function(e) {
$("#main").vegas('next');
e.preventDefault();
});
</script>
As it is it works perfectly, but the images to be displayed should come from a query in the database. So I’m creating a JSON whose output I can print like this:
var obj = JSON.parse('<?= $jsonImagens; ?>');
The result is as follows:
[["assets/site/img/uploads/banners/01.jpg","assets/site/img/uploads/banners/02.jpg","assets/site/img/uploads/banners/03.jpg","assets/site/img/uploads/banners/04.jpg"]]
I am performing the query in the database and placing the result in the array as follows:
<?php
$imagens = array();
$sql = "select * from tb_banner where status = 1";
$search_query = mysql_query($sql);
while($select = mysql_fetch_array($search_query)){
$imagens[] = $select["imagem"];
$textos[] = $select["texto_principal"];
}
$res = array($imagens);
$jsonImagens = json_encode($res, JSON_UNESCAPED_SLASHES);
?>
My question is: what should I do to consume this result in place of the structure in which I declare the images fixed? That is, how I declare this JSON in place of the code below?
slides: [
{ src: "assets/site/img/uploads/banners/01.jpg" },
{ src: "assets/site/img/uploads/banners/02.jpg" },
{ src: "assets/site/img/uploads/banners/03.jpg" },
{ src: "assets/site/img/uploads/banners/04.jpg" }
],
Thanks for the help!!!
Any reason why your JSON is an array of string arrays? And not just a string array?
– Vinicius Zaramella
Look at the editing I did. That’s the way I’m putting the data in the Array.
– SpammingOff