douchendan0040 2015-06-05 23:11
浏览 157

传递数组作为参数,不起作用

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>
  • 写回答

1条回答 默认 最新

  • douwen7603 2015-06-05 23:16
    关注

    Have you tried something like this:

    <?php
            echo'<script>galerias("'.implode("\",\"",$parametroGal).'");</script>';
    ?>
    

    Edited

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看