I have a JavaScript function with calls a picture gallery. I get the img's name in my DB with php, and trying to pass the results as a parameter to the js function, so it will load the imgs on my page. It should load 4 images, but he's assuming the values <img src="img/array1array2array3array4" />
, when it should be <img src="img/array1" />
, <img src="img/array2" />
, and etc... How could i do it ? Here's my JS function:
function galerias(query){
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'>";
x++;
}
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>";
}
PHP DB query:
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 insere valores do $rows ao final do array $dbResult
array_push($dbResult, $rows);
}
return $dbResult;
mysql_close($conexao);
}
This is how i made the query in the pictures page:
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 how i pass the value as parameter to the JS function, on my window.onload
:
<?php
echo'galerias("'.$parametroGal.'");';
?>
This results:
<div class="eight columns">
//that way it just give me as result the word array, since $parametroGal is an array
<img src="img/A" class="imgs-galeria">
<img src="img/r" class="imgs-galeria">
<img src="img/r" class="imgs-galeria">
<img src="img/a" class="imgs-galeria">
<img src="img/y" class="imgs-galeria">
</div>
//i need to get the value of that array, like this:
<div class="eight columns">
//the result of the array, the results of the query in the DB
<img src="img/foto1_1.jpg" class="imgs-galeria">
<img src="img/foto1_2.jpg" class="imgs-galeria">
<img src="img/foto1_3.jpg" class="imgs-galeria">
<img src="img/foto1_4.jpg" class="imgs-galeria">
</div>