开发者

PHP values into javascript

                <script type="text/javascript">
                    var places = [];
                </script>
                <?php
                $date = $_POST['orderdate'];
                $file = fopen("http://www.spc.noaa.gov/climo/reports/".$date."_rpts_hail.csv", "r");
                $content = fgetcsv($file, 1000, ",");
                $id = 1;
                while (($content = fgetcsv($file, 1000, ",")) !== FALSE) {
/*******************************************************************************************
                    Values of content
                    (ignore)****content[0] = Time*******(ignore)
                                content[1] = Size
                    (ignore)****content[2] = Location***(ignore)
                                content[3] = City
                                content[4] =开发者_运维知识库 State
                                content[5] = Lat 
                                content[6] = Long
                                content[7] = Comments
*******************************************************************************************/
                    if ($content !== false) {
                    ?>
                        <script type="text/javascript">                             
                        places.push(new google.maps.LatLng(<?php echo json_encode($content[5]); ?>, <?php echo json_encode($content[6]); ?>));
                        </script>
                    <?php           
                    }

Is there any way that when a page loads, some php code will run and get an array of information and then have that information be put into a javascript variable to use?


Yes.

Use for example json_encode():

<?php 
   $array = array("first" => 1, "second" => 2, "third" => 3, "fourth" => 4);
 ?>

 <script>
   var my_array = <?php echo json_encode($array); ?>
   alert(my_array[1]); // alerts "first"
 </script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜