-2

sorry for my bad english :)

my problem is count i in loop for javascript code php :

$query3 = " SELECT ID,id_type_Lieux, ID_pere,Libelle,logitude,latitude  
            FROM LIEU WHERE id_type_Lieux=2 AND ID_pere=".$rowregion['ID'];
    $result3 = mssql_query($query3);
    $i=0;
    while($row3 = mssql_fetch_array($result3)){
        $ville = array();
        $ville['Libelle']=$row3['Libelle'];
        $ville['latitude']=$row3['latitude'];
        $ville['logitude']=$row3['logitude'];
        $lieuxtype2[$i]= $ville;
        $i++;
    }

code javascript

var sites=[];
for (var i=0; i < <?php echo $ln; ?>; i++) {
  var site=['<?php echo $lieuxtype2[i]['Libelle'] ?>','<?php echo $lieuxtype2[i]['latitude'] ?>'];
  sites.push(site);
};
Dave
  • 3,073
  • 7
  • 20
  • 33
  • $ln=sizeof($lieuxtype2); – mir.mir Oct 25 '16 at 10:40
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Epodax Oct 25 '16 at 10:43
  • You can't mix server code and browser code like this. The server code will always run and finish first, then the server will respond to the browser with the results and only then can your JavaScript activate. – Emil S. Jørgensen Oct 25 '16 at 10:43
  • What he is trying to do is completely normal, considering the javascript code is also in a .php file and the value will be ready by the end of the execution. – Phiter Oct 25 '16 at 10:43
  • @PhiterFernandes The problem is that he is trying to use a JavaScript variable that doesn't exists yet as an active part of his php code. – Emil S. Jørgensen Oct 25 '16 at 10:45
  • Didn't see the `i` there. – Phiter Oct 25 '16 at 10:46

2 Answers2

1

The execution of javascript started after getting result from the server. Php is server side language and JavaScript is client side first you have to understand this. As i see there is no meaning of this code

    var sites=[];
for (var i=0; i < <?php echo $ln; ?>; i++) {
  var site=['<?php echo $lieuxtype2[i]['Libelle'] ?>','<?php echo $lieuxtype2[i]['latitude'] ?>'];
  sites.push(site);
};

Where you are trying to run a server side script in side client side script.

Sunil K
  • 143
  • 9
1

As stated in my comment, you can't use JavaScript in PHP (without AJAX, but that is a whole other story).

What you can do is to create HTML, which will become JavaScript when the page has loaded.

The easiest way to do this is to create the JavaScript list in PHP like this:

$query3 = " SELECT ID,id_type_Lieux, ID_pere,Libelle,logitude,latitude  
            FROM LIEU WHERE id_type_Lieux=2 AND ID_pere=".$rowregion['ID'];
    $result3 = mssql_query($query3);
    $i=0;
    while($row3 = mssql_fetch_array($result3)){
        $ville = array();
        $ville['Libelle']=$row3['Libelle'];
        $ville['latitude']=$row3['latitude'];
        $ville['logitude']=$row3['logitude'];
        $lieuxtype2[$i]= $ville;
        $i++;
    }
echo '<script>var sites = '.json_encode($lieuxtype2).';</script>';

The above script will create a variable named sites, that contains all of the data from your PHP loop.

Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28