Pass array as parameter does not work

Asked

Viewed 560 times

2

I need to pass an array as parametor to a function, each array value has the name of an image and the function should load each of these 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 method implode, but when using it, it does not separate into array and passes the value img/foto1foto2foto3 in a single tag img. 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 in 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 gallery page, I 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 that’s how I pass the array $parametroGal as a parameter for the Javascript function:

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

?>

1 answer

4


Use the json_encode for that reason:

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

The way you were doing, you treated Array PHP as if it were a string. To work, you need to transform the Array of PHP in a Array 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++;

}

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

See a example working in Phpfiddle.

  • Still returned me img/foto1_1.jpg,foto1_2. jpg,foto1_3. jpg,foto1_4. jpg :/

  • And the value of the parameter became galleries(["foto1_1.jpg","foto1_2.jpg","foto1_3.jpg","foto1_4.jpg"]);

  • Still brought me the same return, all the images in a single tag. I tried to use the split to divide into arrays in the JS function but could not...

  • I think it’s some problem in Javascript now, I’m simulating here to try to help you.

  • I think so too, and thank you, I’m also trying here..

  • I was able to make the images appear. See the changed javascript in the answer. I also removed that change in the push array_leave as it was at the beginning: array_push($parametroGal, $result['nome']);

  • Perfect ! Thank you very much !

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.