我有一个图片库,我用javascript函数调用它,它工作正常。现在我将在PHP中创建它,以便用户可以更改和删除一些图像。我决定仍然在javascript中执行它,只是在PHP中传递参数。该函数将图像名称作为参数,并在页面上的window.onload
中将其传递给php。
问题:
每个画廊都有4张照片。我应该有3个画廊,每个画廊有4张不同的图片,相反,我有4个画廊,每个画廊都有重复的图像。 php使用相同的参数执行JS函数4次,因为我的表中有4个值。
我怎么能做对的?
以下是代码:
function galerias(query){
var galeria = new Array();
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 <= 3; i++){
galeria[i] = query;
}
for(var i = 0; i < galeria.length; i++){
imgs.innerHTML += "<img src='img/"+query+"'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++;
}
//this is in my window.onload, so it loads the galleries with the page:
<?php
foreach($galeria_1 as $result){
echo'galerias("'.$result['nome'].'");';
}
?>
//this is in my separate file that make the DB functions:
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);
}
//and this is how i call it in my galleries page:
<?php
include 'connectDB.php';
$conexao = new connectDb();
$galeria_1= $conexao->consultarDados('select * from portfolio where gal= 1 and theme= 1');
?>