Make a Javascript array into JSON - php

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

Related

Actionscript to php then to word template and replace variables

I need to write a code which can do following using actionscript:
1. Send the variables to a php
2. php should open a [re designed word document.
3. Specific word document entries should be replaced with the variables sent from actioncript retaining the formatting of the template.
4. Save the document and provide a link for its download OR just mail it to an address.
I have asked few questions before which very pretty simple compared to this and i did not get any answer for it.. I dont know if this will either. If possible please help me.
To send the variables from ActionScript you would need to do something like the following:
var http:HTTPService = new HTTPService();
http.url = "http://www.yourWebsite.com/yourPhpFile.php";
http.method = "POST";
var variablesToSend:Object = new Object();
for (var varName:String in yourVariables){
variablesToSend[varName] = yourVariables[varName];
}
http.send(variablesToSend);
From "yourPhpFile.php" I would suggest adding some special keywords to search for in the template and do a str_replace() with the variables received through the $_POST object. I'm no php expert but something like:
$yourFilePath = "C:\your\file\path\here.txt";
$yourFileAsString = file_get_contents($yourFilePath);
foreach($_POST as $key=>$value){
$yourFileAsString = str_replace($key,$value,$yourFileAsString);
}
file_put_contents(genNewFileName(),$yourFileAsString);

Pass a PHP array of data to Google Maps Javascript API

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

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.

Parse iTunes API (which uses JSON) w/ PHP

This is the first time I have came in contact with JSON, and I literally have no idea how to parse it with PHP. I know that functions to decode JSON exist in PHP, but I am unsure how to retrieve specific values defined in the JSON. Here's the JSON for my app:
http://itunes.apple.com/search?term=enoda&entity=software
I require a few values to be retrieved, including the App Icon (artworkUrl100), Price (price) and Version (version).
The things I am having issues with is putting the URL of the App Icon into an actual HTML image tag, and simply retrieving the values defined in the JSON for the Price and Version.
Any help/solutions to this would be fantastic.
Thanks,
Jack
Yeah, i have something similar, for my App review website, here is a bit code:
$context = stream_context_create(array('http' => array('header'=>'Connection: close')));
$content = file_get_contents("http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStoreServices.woa/wa/wsLookup?id=$appid&country=de");
$content = json_decode($content);
$array = $content->results["0"];
$version = $array->version;
$artistname = $array->artistName;
$artistid = $array->artistId;
Thats what I used to get Information from the AppStore, maybe you can change the link and some names and it would work for you.

How do I make a script to grab certain piece of data from a JSON API?

I'm trying to build a little script that would let me do this:
http://example.com/appicons.php?id=284417350
and then display this in plain text
http://a3.mzstatic.com/us/r1000/005/Purple/2c/a0/b7/mzl.msucaqmg.png
This is the API query to get that information (artworkUrl512):
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=284417350
Any help and example code would be much appreciated!
I am not sure why you have jQuery in your tags, unless you want to make the request dynamically without a page refresh. However you can do this simply in PHP using the following example:
$request = array (
"app_id" => #$_GET["id"]
);
// parse the requests.
if (empty($request["app_id"])) {
// redirects back / displays error
}
else {
$app_uri = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=" . $request["app_id"];
$data = file_get_contents ($app_uri);
$json = json_decode (trim($data));
print($json->results[0]->artworkUrl100);
}
$request = file_get_contents($itms_url);
$json = json_decode(trim($request));
echo $json[0]->artworkUrl512;
should work in PHP. Unless of course there is more than one hit to the search. A solution using jQuery is probably not very much more difficult.

Categories