Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Unhappy Need Help with passing mysql/php array to javascript

    hello,
    I have spent nearly the past week scouring the net, looking for a solution. I am needing to query mysql with php, with gives me a array of information.
    And I need to feed, this info into my javascript google maps api
    now before you say anything, my google map is working, i just want to add multiple markers.
    And the line I believe that needs to be changed, is just a plain javascript.

    first i wil start off and show my mysql query.
    PHP Code:
        $cityl=mysql_query("SELECT * FROM citylocation WHERE citylong < {$ir['userlongitute']}+5 AND citylong > {$ir['userlongitute']}-5 AND citylat > {$ir['userlatitude']}-5 AND citylat < {$ir['userlatitude']}+5 ");
        
    $cityarray mysql_fetch_array$cityl); 
    the npclocation table has the following fields w/descriptions

    citylocid - autoincrement
    cityid - id for cities
    citylname - name for the cities
    citylong - longitude for cities
    citylat - latitude for cities

    inside the javascript I have
    Code:
    var newpoints = new Array();
    and here is the function inside the javascript
    Code:
    function addPoints() {
     
    	newpoints[0] = new Array(51.45400691005981, -2.63671875, icon0, 'bristol', 'bristol'); 
    	newpoints[1] = new Array(53.30462107510271, -6.1962890625, icon0, 'dublin', 'dublin'); 
    	newpoints[2] = new Array(30.031055426540206, -90, icon0, 'New Orleans', 'New Orleans'); 
     
    	for(var i = 0; i < newpoints.length; i++) {
    		var point = new GPoint(newpoints[i][1],newpoints[i][0]);
    		var popuphtml = newpoints[i][4] ;
    		var marker = createMarker(point,newpoints[i][2],popuphtml);
    		map.addOverlay(marker);
    	}
    }
    As you can see there are numerous newpoints[somenumber] and array info.
    I dont want to have to have lines like that, i want to integrate the table fields array into the line and then i guess looped through until the end of the line.
    to something like
    Code:
    newpoints[?] = new Array(citylong, citylat, icon0, 'citylname', 'citylname');
    or something like that
    I am terrible at js skills
    my php skills could be better, but I can get the job done in php.

    Any help would be great appreciated.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,710
    Thanks
    25
    Thanked 660 Times in 659 Posts
    Normally you pass php variables to js like this:

    var jsVar = <?php echo '"'.$phpVar.'"'; ?>;
    That's two single quotes around a double quote, in both places.

  • #3
    New Coder
    Join Date
    Jun 2011
    Posts
    18
    Thanks
    7
    Thanked 0 Times in 0 Posts
    is that for a single variable or an array? I have been able to get single php variables in js, barely lol.

    I am looking to fetch an array of data from mysql and put that array into the javascript function

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    All you need to do, is set up a while for your database mysql_fetch_array (or a foreach once you've formed the array outside this while) to output the values into the array. Use the information you know - you know how to set up an array in javascript, and you know how to loop through an array in PHP. You just need an incrementing counter (this is based on your example btw) that will increment the javascript array's index, and input the values based on that. You've just got to remember that PHP and JS are separate - you're basically getting PHP to output the javascript code for the browser to run, not directly running the javascript with the PHP.

    An example:
    PHP Code:
    // Echo the start of the function
    // The two new lines and the tab (\n\n\t) at the end are just for ease of reading the code
    // It should match your current code's layout (maybe haha). It's completely optional
    echo 'function addPoints() {'."\n\n\t";

    // Set a counter
    $i=0;

    // Go through every row, echo the javascript code as you go
    while($cityarray mysql_fetch_array($cityl)){
        echo 
    "newpoints[".$i."] = new Array('".$cityarray['information']."');\n\t";
        
    $i++;
    }

    // echo rest of function
    echo 'for(var i = 0; i < newpoints.length; i++) {
            var point = new GPoint(newpoints[i][1],newpoints[i][0]);
            var popuphtml = newpoints[i][4] ;
            var marker = createMarker(point,newpoints[i][2],popuphtml);
            map.addOverlay(marker);
        }
    }'



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •