There are two questions: 1) how to get what I need from the database. 2) how to pass, what I got from the database in javascript I have an html file. It includes javascript map.js and the file map.php. Java script is normally executed, and pkhp file is not executed like. How to bind javascript and pkhp together in an html file, I do not know. I did not study PCP and javascript, I have a slightly different specialization, but now I have to write something by necessity.

Here is the code map.php:

<?php $hostname = "***************"; $username = "****************"; // имя пользователя (в Denwer`е по умолчанию "root") $password = "**********"; $dbName = "***********"; $table = "test_table"; mysql_connect($hostname, $username, $password) or die ("Не могу создать соединение"); mysql_select_db($dbName) or die (mysql_error()); $query = "SELECT coord1,coord2 FROM $table"; $res = mysql_query($query) or die(mysql_error()); mysql_close(); ?> 

here from the base I need to get an array of elements of the coordinate 1 and coordinate 2 an example of the format of the element of the array [54.345324,23.4354353]

then this array needs to be transferred to javascript, here it is already initialized in me, but you need to fill it from the base. The array is called the markers. map.js

 var map; var point = 1; //точки для двигателя var markers = { point1:[55.755786, 37.617633], point2:[55.7543712, 37.6104643], point3:[55.7531491, 37.6101551], point4:[55.7506212, 37.6101143], point5:[55.7541852, 37.5274149], point6:[55.7473746, 37.609723], point7:[55.803607, 37.328598], point8:[55.7422534, 37.6064208], point9:[55.7397707, 37.6055861], point10:[55.704945, 37.5277036], }; $(document).ready(function(){ //карта var latlng = new google.maps.LatLng(55.755786, 37.617633); var myOptions = { zoom: 13, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); //отмечаем все точки на карте var marker = []; for(i in markers){ marker[i] = new google.maps.Marker({ position: new google.maps.LatLng(markers[i][0], markers[i][1]), map: map }); } //map.panTo(new google.maps.LatLng(markers.point10[0], markers.point10[1])); //двигатель карты по точкам function mover(){ map.panTo(new google.maps.LatLng(markers["point"+point][0], markers["point"+point][1])); point ++; if(point>10)point=1; } //карта не используется google.maps.event.addListener(map, 'idle', function(){ setTimeout(mover, 500);//время в милисекундах }); }); 

PS since, due to the lack of points, there is no possibility of a plus or answer, I will write here. I thank user Inkognitoo , dekameron for help. I got what I wanted to implement. Everything is working. I used the idea of ​​decameron.the first or second time I work with php. I bought for a long time what the point was and why it does not work, it turns out I had an html file and in it <? ?> and also <script> then I did index.php and drove html into echo, also javascript drove into echo. and in the right place pasted var markers. In general, thank you all for your help and your time.

BUT DECISION Deonisa turned out to be better because you do not have to put everything in echo, you can use the html page and everything will be neat and clean. indeed it will not be a code.

http://www.wiseguys.com.ua/ here is a ready-made solution, well, this is only part of what I plan, but it’s already good that one piece is ready. I am not a web developer at all, I work with Android OS.

  • use json format. - SverxnovA
  • Doesn't it seem to me that I see such a question on this site for the first time? - Vfvtnjd 5:24 pm

3 answers 3

mysql_query (); returns not a table, but a handle to the resulting table. In your case - two columns with elements. You can get values ​​from a descriptor, for example, using the mysql_fetch_assoc () function ; . The array from php to javascript is transmitted through the good old echo . Read about it here

The code may look like this: (dialed from memory, there may be errors)

 $query = "SELECT coord1,coord2 FROM $table"; $res = mysql_query($query) or die(mysql_error()); echo 'var markers = {'; while($myrow = mysql_fetch_assoc($res)) echo "point{++$i}:[{$myrow['coord1']}, {$myrow['coord2']},"; echo '};'; 
  • Looks like there was no more stupid way, so you decided to stop transferring data in a loop. Nothing, this option can also compete for "first place". - Deonis
  • Do you know another way? Not attracting cycles? Share it? - Pavel Sotnikov
  • @Inkognitoo, sir, no need to juggle, I was talking about data transfer in a loop. If you do not understand this - condolences. A more human way, suggested @dekameron: to form a complete answer and only then send. - Deonis
  • one
    The man immediately wrote: "I did not study PCP and javascript." I remember myself and my first steps very well. When I asked a question - I was interested in the simplest solution that I can understand, apply, improve and redo. Before flying - learn to walk, and even crawl. PS I advise you to read to everyone who loves to throw words “stupid” and “condolent”: habrahabr.ru/post/178747 - Pavel Sotnikov
  • one
    @Inkognitoo, It’s better not to offer anything at all than to enter, even if it’s a simple one, but a solution from the category of “ we learn gomnodit from scratch ”. - Deonis

My answer is more addition / comment to the answer @dekameron .

All anything, but neither in the first answer, nor in yours, says nothing about ajax . Like, left the TC food for thought? ))

Php

 $data = false; if(mysql_num_rows($res) > 0){ // если хоть что-то есть, то формируем ответ while($myrow = mysql_fetch_assoc($res)){ $data['point'.(++$i)] = array($myrow['coord1'], $myrow['coord2']); } } echo json_encode($data); // и ничего дописывать не надо 

Js

 // отправляем запрос в map.php $.ajax({ url:'map.php', type:'POST', dataType: 'json', data: {key:val}, // если надо success: function(data){ if(data){ console.log(data); // все полученные координаты, именно в том виде, который требовался ТС } else { // действия в случае, если координаты в БД отсутствуют } } }); 
     $query = "SELECT coord1,coord2 FROM $table"; $res = mysql_query($query) or die(mysql_error()); $data = new stdClass(); while($myrow = mysql_fetch_assoc($res)){ $key = 'point'.(++$i); $data->$key = array($myrow['coord1'], $myrow['coord2']); } //В нужном месте: echo 'var markers = '.json_encode($data).';'; 

    upd

    PS When will you stop using mysql_ * functions?

    PPS stdClass for empty $ data to output curly braces instead of square brackets