Séraphin Hochart
Séraphin Hochart

Reputation: 193

Javascript with MySQL request embedded in Php

I've been working on that for a while, and google didn't help me that much on that one,

Basically, I am creating different divs, when you hover on them, you get more information. I managed to do that in a way that when you hover the div, the text changes.

Now, I would like to make it dynamic with a MySQL database, my different divs (already created) will get the text and change it with javascript.

UPDATE : The following code kinda works but doesn't show the iteration. My concern would be that the first div gets the first result (ID = 1), the second div gets the second result... and so on! I am fairly new to php/MySQL, and I didn't find a way to do it.

<script>
var n = 0;
$("div.enterleave").mouseenter(function() {
 n += 1;
 $(this).find("span").text( "<?php

        $reponse = $bdd->query('SELECT possesseur FROM jeux_video LIMIT 0, 4');
        while ($donnees = $reponse->fetch()) {
            echo $donnees['possesseur'] ; }
            $reponse->closeCursor(); 
    ?>");
}).mouseleave(function() {
 $(this).find("span").text("<?php

            $reponse = $bdd->query('SELECT nom FROM jeux_video LIMIT 0, 1');
            while ($donnees = $reponse->fetch()) {
                echo $donnees['nom'] ; }
                $reponse->closeCursor(); 
    ?>");
});

</script>

Thanks in advance!

Upvotes: 0

Views: 156

Answers (2)

Krzysztof
Krzysztof

Reputation: 16130

You have concoct before and after this MySQL-related code. You should put semicolon after [...]text('" and write echo before "'); (after closeCursor).

 <?php

echo "<script>
var n = 0;
$('div.enterleave').mouseenter(function() {
 n += 1;
 $(this).find('span').text('";
            $reponse = $bdd->query('SELECT nom FROM jeux_video LIMIT 0, 10');
            while ($donnees = $reponse->fetch()) {
                echo $donnees['nom'] ; 
                }
                $reponse->closeCursor();
echo "');
}).mouseleave(function() {
 $(this).find('span').text('What does Patrick likes?');
});

</script>";

?>

Upvotes: 2

Scott
Scott

Reputation: 21882

You can't concatenate there.

echo "<script>
var n = 0;
$('div.enterleave').mouseenter(function() {
 n += 1;
 $(this).find('span').text('";


        $reponse = $bdd->query('SELECT nom FROM jeux_video LIMIT 0, 10');
        while ($donnees = $reponse->fetch()) {
            echo $donnees['nom'] ; 
            }
            $reponse->closeCursor();

 echo "');
}).mouseleave(function() {
 $(this).find('span').text('What does Patrick likes?');
});

</script>";

Upvotes: 1

Related Questions