Mon code php ne fonctionne pas dans le javascript

Résolu
msi79 Messages postés 509 Date d'inscription lundi 24 août 2009 Statut Membre Dernière intervention 2 mai 2023 - Modifié le 26 avril 2023 à 20:17
JBEL77 Messages postés 2 Date d'inscription mercredi 26 avril 2023 Statut Membre Dernière intervention 28 avril 2023 - 28 avril 2023 à 03:34
<script type="text/javascript">
    function addText(p, text) {
        var rt;
        var logement;
        var t = document.createElementNS("http://www.w3.org/2000/svg", "text");
        var b = p.getBBox();
        t.setAttribute("transform", "translate(" + (b.x + b.width/2) + " " + (b.y + b.height/2) + ")");
        t.textContent = text;
        t.setAttribute("fill", "white");
        t.setAttribute("font-size", "40");
        t.setAttribute("text-anchor", "middle"); // Centre horizontalement
        t.setAttribute("dominant-baseline", "middle"); 
        p.parentNode.insertBefore(t, p.nextSibling);
    }

 
 var paths = document.querySelectorAll("path");
    <?php
    $r=0;
      foreach ($data as $id => $info) {
    $r++;
    ?>  
    <?php echo $r-1; ?>
    addText(paths[<?php echo $id-1; ?>], "<?php echo $info['logement']; ?>");
    <?php
     }
    ?>
      addText(paths[0], "LOT 150");
    addText(paths[1], "LOT 151");
    addText(paths[2], "LOT 152");
    addText(paths[3], "LOT 153");
    addText(paths[4], "LOT 154");
    addText(paths[5], "LOT 155");
    addText(paths[6], "LOT 156");
    addText(paths[7], "LOT 90");
    addText(paths[8], "LOT 90");
</script>

2 réponses

tu devrais pas transpercer le DOM comme ça...aussi bien il faut une quête du code et y travailler pour le scope...de la variable voire...du script...aussi bien là...pisser du js dans un scope qui est côté client...alors que le Maître est Apache/PHP...pisse tout en PHP. tu y verras plus clair.

p.s: pense à la chronologie dans la site. parfois un echo de js en haut, parfois en bas...parfois une session pour le scope...:)

regarde les balises js...tu peux les mettre en bas...avant body conforme au 3W et entre deux...! echo tralalala...tu t'inventes une approche...plugin quoi ! dans un scope et une chronologie où...y a peut être une func en include ????

0
msi79 Messages postés 509 Date d'inscription lundi 24 août 2009 Statut Membre Dernière intervention 2 mai 2023 1
26 avril 2023 à 16:22

je comprends pas bien ta reponse. peux tu ecrires le code javascrpt pour moi ?

voici mon code complet

<?php
// Connexion à la base de données
include('./functions/bdd.php');


$conn = new mysqli($serveur, $admin, $mdp, $base);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Récupération des données de la base de données
$sql = "SELECT * FROM logements WHERE id_residence = '".$_GET['id_program']."' AND id  IN(796,797,798,799) ";
$result = $conn->query($sql);


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <!-- This file has been downloaded from Bootsnipp.com. Enjoy! -->
    <title>Data List Product - Bootsnipp.com</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1 , minimal-ui">
        <script src="https://code.jquery.com/jquery-3.3.1.js" ></script>

        <style>
        .map__image{
    width:50%;
    float:left;
}

 .map__image path{
  fill:red;
   stroke:yellow;
   stroke-width:3px;
   transition: fill 0.3s ;
}

.map__image path:hover{
   fill: yellow;
}

.map__list {
   float:right;
   width:50%;
}

.map__list a{
   color: inherit;
   text-decoration: none;
   transition: color 0.3s;
}
</style>




</head>
<body>
  
   <div class="map" id="map">
        
        <div class="map__image">
      <?php

// Requête SQL pour récupérer les données
$sql = "SELECT id, lot_path, logement FROM logements WHERE id IN(796,797,798,799)";
$result = $conn->query($sql);

// Stockage des données dans un tableau
$data = array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $data[$row["id"]] = array("path" => $row["lot_path"], "text" => $row["logement"]);
    }
}

// Génération du code SVG
$svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:amcharts="http://amcharts.com/ammap" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"  viewBox="0 0 1749 2481">	<g>';
foreach ($data as $id => $info) {
    $path = $info["path"];
    $text = $info["text"];
    $svg .= '<a id="region-' . $id . '" xlink:href="http://google.com/?q=' . $id . '" xlink:title="LOT-' . $text . '">
        <path id="region-' . $id . '" serif:id="' . $text . '" d="' . $path . '" />
       
    </a>';
}
$svg .= '	</g></svg>';

// Affichage du code SVG
echo $svg;

// Fermeture de la connexion à la base de données
$conn->close();
?>
        </div>
        
        
        
    </div>


<script type="text/javascript">
    function addText(p, text) {
        var t = document.createElementNS("http://www.w3.org/2000/svg", "text");
        var b = p.getBBox();
        t.setAttribute("transform", "translate(" + (b.x + b.width/2) + " " + (b.y + b.height/2) + ")");
        t.textContent = text;
        t.setAttribute("fill", "white");
        t.setAttribute("font-size", "40");
        t.setAttribute("text-anchor", "middle"); // Centre horizontalement
        t.setAttribute("dominant-baseline", "middle"); 
        p.parentNode.insertBefore(t, p.nextSibling);
    }

    var paths = document.querySelectorAll("path");
    <?php
      //foreach ($data as $id => $info) {
    ?>  
  <!--  addText(paths[<?php echo $id-1; ?>], "<?php echo $info['logement']; ?>");-->
    <?php
    // }
    ?>
    
         addText(paths[0], "LOT 150");
    addText(paths[1], "LOT 151");
    addText(paths[2], "LOT 152");
    addText(paths[3], "LOT 153");
    addText(paths[4], "LOT 154");
    addText(paths[5], "LOT 155");
    addText(paths[6], "LOT 156");
    addText(paths[7], "LOT 90");
    addText(paths[8], "LOT 90");
</script>
</body>
</html>
0
JBEL77 Messages postés 2 Date d'inscription mercredi 26 avril 2023 Statut Membre Dernière intervention 28 avril 2023
28 avril 2023 à 03:34

Met plutôt JavaScript dans PHP je pense que c'est plutôt ça qui marche

0
Rejoignez-nous