Passing array as parameter does not work

2

I need to pass an array as a parameter to a function, each array value has the name of an image, and the function should load each of those 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 implode method, but when I use it, it does not separate into an array and passes the img/foto1foto2foto3 value to a single img tag. 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 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 galleries page, I'll 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 this is how I pass the array $parametroGal as a parameter to the function in JavaScript:

<?php
        echo'galerias("'.$parametroGal.'");';

?>
    
asked by anonymous 06.06.2015 / 17:04

1 answer

4

Use json_encode for this:

<script>
    galerias(<?php  echo json_encode($parametroGal); ?>);
</script>

The way you were doing, it treated PHP's Array as if it were a string. In order to work, you need to make Array of PHP a Array of Javascript.

Also change Javascipt to:

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"" />';
    }

    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++;

}

You do not need to instantiate a new Array in Javascript because the parameter will already arrive as an array.

See a example running on PhpFiddle .

    
06.06.2015 / 17:34