2
I need to pass an array as parametor to a function, each array value has the name of an image and the function should load each of these images as img/foto1
, img/foto2
, etc. But it is not passing the array values, it is simply passing the word "Array", which returns me img/Array
. I tried to use the method implode
, but when using it, it does not separate into array and passes the value img/foto1foto2foto3
in a single tag img. Could you help me ?
My Javascript function that should return the images:
function galerias(parametroGal){
var query = new Array(parametroGal);
var imgs = document.querySelector("#gallery");
var x = 1;
imgs.innerHTML += "<div class='row'>";
imgs.innerHTML += "<div class='eight columns'>";
imgs.innerHTML += "<h4>Galeria "+x+"</h4>"
for(var i = 0; i < query.length; i++){
imgs.innerHTML += "<img src='img/"+query[i]+"'class='imgs-galeria'>";
}
imgs.innerHTML += "</div>";
imgs.innerHTML += "</div>";
imgs.innerHTML += "<a class='row' href='pics.html?gal="+x+"'><div class='twelve columns link'><p>Veja mais</p></div>";
x++;
}
My function that queries in the database:
function consultarDados($query){
$dbResult = array();
$conexao = mysql_connect($this->host, $this->usuario, $this->senha);
mysql_select_db($this->banco, $conexao);
$rs = mysql_query($query, $conexao);
while($rows = mysql_fetch_assoc($rs)){
array_push($dbResult, $rows);
}
return $dbResult;
mysql_close($conexao);
}
On my gallery page, I query this way:
<?php
include 'connectDB.php';
$conexao = new connectDb();
$galeria_1= $conexao->consultarDados('select * from portfolio where gal= 1 and theme= 1');
$parametroGal = array();
foreach($galeria_1 as $result){
array_push($parametroGal, $result['nome']);
}
?>
And that’s how I pass the array $parametroGal
as a parameter for the Javascript function:
<?php
echo'galerias("'.$parametroGal.'");';
?>
Still returned me img/foto1_1.jpg,foto1_2. jpg,foto1_3. jpg,foto1_4. jpg :/
– anuseranother
And the value of the parameter became galleries(["foto1_1.jpg","foto1_2.jpg","foto1_3.jpg","foto1_4.jpg"]);
– anuseranother
Still brought me the same return, all the images in a single tag. I tried to use the split to divide into arrays in the JS function but could not...
– anuseranother
I think it’s some problem in Javascript now, I’m simulating here to try to help you.
– Marcus Vinicius
I think so too, and thank you, I’m also trying here..
– anuseranother
I was able to make the images appear. See the changed javascript in the answer. I also removed that change in the push array_leave as it was at the beginning:
array_push($parametroGal, $result['nome']);
– Marcus Vinicius
Perfect ! Thank you very much !
– anuseranother