我有一个JavaScript函数,可以调用图片库。我使用php在我的数据库中获取img的名称,并尝试将结果作为参数传递给js函数,因此它将在我的页面上加载imgs。它应该加载4个图像,但是它假定值<img src="img/array1array2array3array4" />
,它应该是<img src="img/array1" />
,<img src="img/array2" />
等等......我怎么能这样做?这是我的JS功能:
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数据库查询:
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);
}
这是我在图片页面中进行查询的方式:
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']);
}
我如何将值作为参数传递给我的window.onload
上的JS函数:
<?php
echo'galerias("'.$parametroGal.'");';
?>
结果:
<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>
答案 0 :(得分:0)
你有没有试过这样的事情:
<?php
echo'<script>galerias("'.implode("\",\"",$parametroGal).'");</script>';
?>
<强>被修改强>