Pass a PHP array of data to Google Maps Javascript API - php

I have a PHP array (of arrays) of address date like this:
$date[0][lat] = 42.54
$date[0][lng] = -80.54
$date[0][title] = Business Name
$date[1][lat] = 41.54
$date[1][lng] = -81.54
$date[1][title] = Another Name
etc.
Is it possible to somehow pass this array of arrays to the google maps api to map as markers? Everything I seem to find online is either working with XML or working with data that is already a javascript array.

if you use PHP 5.2 or later, you can pass your array as follow
<?php print json_encode($date) ?>

Try to encode it as json ans then just simply echo into the JS variable:
var myGeolocatedItems = <?= $sJsonEncodedArray; ?>;

var simple ='<?php echo $sparam1; ?>';
you can assign any php variable using the above way

Related

How to get the value from an array in ionic?

I have created a php file.
Response from php-
response:"{"status":true,"originalName":"1527931554722.png","generatedName":"ceabe4c3b0074eb3d64cca21493be324.png"}"
I want to get the value of generatedName in ionic.
I used this code:->
let name = response[3]; console.log(name);
It gives output 't' on console.
response:"{"status":true,"originalName":"1527931554722.png","generatedName":"ceabe4c3b0074eb3d64cca21493be324.png"}"
try response.generatedName
You do not need index here, just access the property since its already an object.
DEMO
let response = {"status":true,"originalName":"1527931554722.png","generatedName":"ceabe4c3b0074eb3d64cca21493be324.png"};
console.log(response.generatedName);

Passing variable to remote php file for JSON data

I am building a mobile app using Titanium Studio, build: 3.1.1.201303242433, CLI version 3.0.24, Titanium SDK version 3.0.2.GA, iOS SDK: 6.1, iPad Simulator: 6.1 on MAC OS X10.7.5.
I am attempting to retrieve JSON data from a remote database. The database has records with and without a "sectionid" (sectionids are numbers 1-12). I want to retrieve the records with the "sectionid" I pass to my query. Here are the steps I am following: First I assign a "sectionid" to a tableViewRow like this:
leftImage:'images/advertising.png',
title:'Advertising',
sectionid: '1',
color:'#000',
font: {fontWeight:'bold', fontSize:16},
height:'44dp',
hasChild:true,
test:'testcatmenu.js'},
Then I pass the sectionid to another file like this:
var section = e.rowData.sectionid;
win.section = section;
In the next file I get the sectionid and pass it to the remote php file like this:
var section = Ti.UI.currentWindow.section;
var url = "http://mydomain.com/myfile.php?sectionid='section'";
Finally, in the php file I $_GET the sectionid and use it in my MySQL query like this:
$result = mysql_query("SELECT name FROM `cms_client` WHERE sectionid = '" . mysql_real_escape_string($_GET['section']) ."'") or die('Could not query');
The php file is returning the records that do not have a sectionid, so somewhere I am losing the sectionid? I do not know where my code syntax is wrong, would someone be so kind to show me the correct syntax that I need to make this work?
In the php you are using a variable $_GET['section'] but the GET variable you are passing in is called sectionid (?sectionid=). So either change both to section or both to sectionid.
Also, assuming that the
var url = "http://mydomain.com/myfile.php?sectionid='section'";
is javascript then it should be something like:
var url = "http://mydomain.com/myfile.php?sectionid="+section;
var url = "http://mydomain.com/myfile.php?sectionid='section'";
Change that to
var url = "http://mydomain.com/myfile.php?sectionid="+section;
Javascript allows you to add variables to a string using + variableName
Like
var variableT = "foobar";
var strNew = "This string contains the word: "+variableT+" and it works!";

Calling images dynamicly using javascript and php

i am new to Javascript and i have created the code below it works fine no problem at all however i want to know what is i want to pull the image dynamically using php and javascript from mysql database how can i refactor my code bellow. thanks in advance for your contribution.
var myimage = document.getelementById("mainImage");
var imageArray =["images/overlook.jpg","images/garden.jpg","images/park.jpg"];
var imageIndex =0;
function changeimage(){
myimage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if(imageIndex >= imageArray.length){
imageIndex = 0;
}
setInterval(changeimage, 5000);
One of several options.
Query the database for the column with the URL of the images.
$query = mysql_query("SELECT url FROM images");
Then something like this to get an array out of it:
$images = array();
while($row = mysql_fetch_array($query)){
$images[] = $row['url'];
}
Then generate this string (that you use in the Javascript provided):
var imageArray = ["images/overlook.jpg","images/garden.jpg","images/park.
using the array you retrieved from the database. You could use json_encode in PHP for this if you don't want to mess around with error prone string building.
$imagesAsJsonArray = json_encode($images);
Echo it. Done.
Not the most elegant of solutions. But it gives you something to play with. Check out a few PHP tutorials online and you'll soon get the hang of it.
Two choices:
Using PHP when your page is created, put an array of images in the page and use page-level javascript to cycle among them.
Using Ajax in the page, call from the page to the server to get the next image and then use client-side javascript to make that returned image visible on the page.

Make a Javascript array into JSON

I've tried searching google and nothing is clear, plus you guys are way faster and correct.
I have an array as follows:
var bannertext = new Array();
bannertext[1] = "ladidadi";
bannertext[2] = "blahblahblahblah";
bannertext[3] = "oooaaaaooooaaa";
How do I turn the bannertext array into a JSON encoded array so that I can send it to a PHP page and break it down using the json_decode function, and then how do I access the individual variables?
EDIT: Thanks to Gazler for that first part! My PHP page looks like this:
$bannertext = $_GET['bannertext'];
$banner = json_decode($bannertext);
How do I access each of those strings now? LIke echo $banner[1]; and so on?
You use JSON.stringify() however in IE you will need to include the json2 library as IE has no native JSON parsing.
var bannertext = new Array();
bannertext[1] = "ladidadi";
bannertext[2] = "blahblahblahblah";
bannertext[3] = "oooaaaaooooaaa";
console.log(JSON.stringify(bannertext));
As an aside, you can instantiate an array using the array literal syntax.
var bannertext = [];
Here is a blog post explaining the difference.
JSON.stringify:
JSON.stringify(bannertext);
Older browsers, IE7 and below, require an external library Json2 Free CDN

How to use a php result set from a query in JavaScript

I am working on a page that displays all state parks. Something like this:
http://www.comehike.com/outdoors/state.php?country_id=1&state_id=6&state_name=California&country_name=
I have the park data, including lat/lng in my database and I already get that data during the request. What I want to avoid is doing an AJAX call which will query the park data again.
Is it possible for me to just send the PHP result set to the JS somehow and to loop through the parks and display them?
I am already doing something like that by sending the map-center lat/lng so I figured it is doable with a result set.
Thanks,
Alex
This depends on how your pages are setup. If you're using some kind of MVC setup, you need to pass the variable to your view first. Or if it's a single page, just retrieve the PHP above and pass it as a variable.
<?php
$parkData = $model->getData(); //make sure this returns an array or an object array or something
$parkDataJson = json_encode($parkData);
?>
//in your view
<script type='text/javascript'>
let parkData = <?php echo $parkDataJson ?>;
//now you have a JSON array available for your use
</script>

Categories