Gallery with PHP and Javascript not working correctly

Asked

Viewed 121 times

1

I have a function in JavaScript that mounts an image gallery. It had this whole function in Javascript and it worked perfectly, now I need to insert the PHP so that the user can make changes to the images, and I am passing the parameter to this function through the result of the query in PHP. It should work this way: Gallery title above, and below 4 different images of the result of query. Below another title, and other images. The problem is that PHP is passing the same parameter to the function, making the function return: gallery title and below 4 identical images(the first value of the query), Other gallery title and other identical images(the second value of query). How can I get the gallery to upload 4 different images to a gallery, not 4 identical images to different galleries ?

My role in JS is like this:

function galerias(query){
    var galeria = new Array();
        //aqui eu pego a div que armazena as galerias
        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>"

        //passo o valor da query para o array galeria
        for(var i = 0; i <= 3; i++){
            galeria[i] = query;
        }

        e insiro dentro da div
        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++;

}

My function in 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($dbResult, $rows);
        }
    return $dbResult;
    mysql_close($conexao);
}

I insert this php in window.onload page:

<?php
    foreach($galeria_1 as $result){
        echo'galerias("'.$result['nome'].'");';
    }
?>

The visual result is this: inserir a descrição da imagem aqui

and it should be this: inserir a descrição da imagem aqui

1 answer

2


Checks whether this array ($galeria_1) really has all positions, because it looks like it only has one position.

  • Yes, he has all the positions !

  • What is the result of the $result variable ?

  • $result['name'] returns the values of the column of my database: foto1_1.jpg foto1_2.jpg foto1_3.jpg foto1_4.jpg, and only the $result returns Array Array Array... is part is perfect, apparently

  • So that’s the error my friend, realize that there are no galleries function you get "query" which was to be an array and not a name of one of the images, because in the course of the function you do a for mounting the images passing "query" as the name of the image in "src" but this "query" is a single value that you passed in "echo'galerias("'.$result['name'].'");';" .

  • Actually gave yes ! But how could I pass the array as parameter to the function...?

  • Creates a Json!!!

  • Thank you for the reply !

  • You are welcome, we are here to help each other! Strong hug my friend.

Show 3 more comments

Browser other questions tagged

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