Populate PHP Array Values in Javascript - javascript

Here is my code:
I am getting data from mysql query and getting array of values. But i am not able to populate in javasript. could you please any one help me out...
PHP Code:
$parishFamilyInfo = $parishFamilyInfoBO->sendBirthdayGreeting($personalInfoSearchDO, $parishFamilyInfoSearchDO);
foreach ($parishFamilyInfo as $parishFamily){
if(!empty($parishFamily->email)){
print_r($parishFamily);
}
}
JavaScript:
$(document).ready(function(){
var t = $('#people').bootstrapTransfer(
{'target_id': 'multi-select-input',
'height': '15em',
'hilite_selection': true});
t.populate([ /*here i need to populate php array value*/
{
value:"<?php print_r($parishFamily->email);?>",
content:"<?php print_r($parishFamily->name);?>"
},
]);
//t.set_values(["2", "4"]);
//console.log(t.get_values());
});

print_r is only for debugging. It's not for anything else. Let me quote the documentation:
print_r — Prints human-readable information about a variable
As stated above, it is a debugging function that's used to print the contents of a variable in a more readable manner. It just returns a string — JavaScript does not and will not understand this format.
Use JSON (JavaScript Object Notation) instead. It is is a light-weight data interchange format that is very popular nowadays. You can use the built-in function json_encode to create the JSON representation of your PHP array and then transfer it to JavaScript. JSON being language independent and based on the JavaScript scripting language, your JavaScript code will now be able to parse the array:
t.populate([
{
value: <?php echo json_encode($parishFamily->email); ?>,
content: <?php echo json_encode($parishFamily->name); ?>
},
]);

Related

How to search Steam game API JSON output?

I am working with following APIs:
http://api.steampowered.com/ISteamApps/GetAppList/v0002/?format=json
https://store.steampowered.com/api/appdetails?appids=GAMEID
(gameid example: "730" - counter strike)
My goal with both of these is to search them for data. For example I want the first API to give me a name of a game based on it's ID and the second one to give me specific information about a game for example if it has trading cards (id:29 in the API).
I tried a few things but I am kinda lost on this beacuse I don't really understand JSON so I would really appreciate some help.
I am open to both PHP and JS solutions.
IN PHP, you can use the json_decode() function to convert JSON data into an array. You can them access the values as you do for a classic array :
$appID = 730 ;
$url = 'https://store.steampowered.com/api/appdetails?appids=' . $appID ;
$content = file_get_contents($url) ; // retrieve the JSON data string from the website
$data = json_decode($content, true); // convert the JSON string into PHP array
echo $data[$appID]['data']['name'] ; // Counter-Strike: Global Offensive
var_dump($data[$appID]['data']['is_free']); // bool(true)

Getting mysql data into a var array

Im using MySQL to obtain data into $username and $chance.
There are two usernames in the data but it only loads the first one.
var data = [
{
"name" : "<?php echo $username; ?>",
"hvalue" : <?php echo $chance; ?>
},
];
To give a correct answer, we'll have to know what your variables $username and $chance exactly look like.
If you only have a string there, then only one value is possible.
If these variables are php arrays, you'll have to use json_encode to make a JSON array from them, before you can add them to your javascript object. (see https://secure.php.net/manual/en/function.json-encode.php)
Anyway, the best way to send a PHP array to JS would be to create the whole array in PHP and then encode it to JSON in order to use all the values in JS.
var data = <?php echo json_encode($data); ?>
More advanced: use an ajax request to avoid mixing up PHP an JS. But that's another story.

Not getting return values in $getJSON object

Javascript:
$.getJSON("path", function(data){alert(data.name)})
Guys in this line "data" is not working as an object. name is unrecognizable here.
I am sending multiple outputs in php using json_encode() function but not able to get those values in javascript file.
PHP Code:
$book = array("name"=>"bob", "num"=>1);
echo json_encode($book);

How to read a JSON Array in php without any key?

I have a JSON formed #Javascript and passed to PHP via AJAX , problem is that I cannot assign keys to each JSON Object inside the JSON Array .
The JSON Array looks something like this :
[{"jobId":"90","cname":"Subhasish","removal_id":101,"quantity":"3"},{"jobId":"90","cname":"Subhasish","removal_id":102,"quantity":"2"},{"jobId":"90","cname":"Subhasish","removal_id":103,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":104,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":105,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":106,"quantity":"5"},{"jobId":"90","cname":"Subhasish","removal_id":107,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":108,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":109,"quantity":"4"}]
,Now , in PHP ,how to iterate the data , I am a newbie to PHP , so please excuse me if I am asking a very silly question.
The link you provided has examples with key, mine is without key.
Thanks
$json = '[{"jobId":"90","cname":"Subhasish","removal_id":101,"quantity":"3"},{"jobId":"90","cname":"Subhasish","removal_id":102,"quantity":"2"},{"jobId":"90","cname":"Subhasish","removal_id":103,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":104,"quantity":"4"},{"jobId":"90","cname":"Subhasish","removal_id":105,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":106,"quantity":"5"},{"jobId":"90","cname":"Subhasish","removal_id":107,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":108,"quantity":0},{"jobId":"90","cname":"Subhasish","removal_id":109,"quantity":"4"}]';
$array = json_decode($json,true);
foreach($array as $item) {
echo $item['jobId']."-".$item['cname']."-".$item['removal_id']."-".$item['quantity']."\n";
}
Output
90-Subhasish-101-3
90-Subhasish-102-2
90-Subhasish-103-4
90-Subhasish-104-4
90-Subhasish-105-0
90-Subhasish-106-5
90-Subhasish-107-0
90-Subhasish-108-0
90-Subhasish-109-4
Example
Using json_decode($json_object).
$json = json_decode($json_object);
foreach($json as $j){
print_r($j)
}

How can I pass foreign characters (e.g. æ) to a javascript array from a PHP array?

I have an array of file paths, which is stored in a PHP array called $files, which I would like to pass to a javascript array using this command:
var files = <?php echo json_encode($files)?>;
Which is fine, however it doesn't pass any values which have special characters for example one that contains 'æ'. Instead that value in Javascript comes out as null. Is there another way of preserving these characters? The values are all file paths, which seem to be passed over OK using json_encode, just not these special characters.
I've solved it. I essentially ran through the array using url_encode, and THEN JSON'd that for Javascript to interpret.
$filesEncoded = array();
foreach ($files as $f) {
array_push($filesEncoded,urlencode($f));
}
<script>
var files = <?php echo json_encode($filesEncoded)?>;
</script>

Categories