I have an associative Array in PHP and I want to transfer it into an Javascript on the fly, how can I do that?
My PHP array looks something like this:
$pairs = ["LBo"=>"Large Blocks",
"Bo"=>"Blocks",
"bo"=>" blocky"]
I get the PHP array size into JS using
const pairsSize= "<?php print(count($pairs)); ?>"
Next I create a loop in which the indexname of the associative array and the assigned value has to be read from the PHP array. I'm stuck and I don't know how to do it.
let lithPairs = []; //my JS "associative" array
for (x = 0; pairsSize> x; x++) {
....
}
I want the result to look like this;
lithPairs['LBo']="Large Blocks";
lithPairs['Bo']="Blocks";
lithPairs['bo']="blocky";
any help would be much appreciated!
use json encode php array and fetch it using URL or Cookies/ localstorage
I send a php array like:
$var = array (
0=> 4,
1=> 6,
2=> 8,
...
as json_encode($var); into the uri and then I receive it into javascript file is still ok here but when I push it into new array like this :
this.patg.push(attd);
Is inserted like this below .
var attds = ["4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606"]
I know I have to find the problem. but
Questions:
could you please tell me why this could happends or help me to deal with it.
but in any case just for knowledge . how you would add the extra " " surrounding the , that i miss to be an array , or is that crazy idea to fix this?
If you are receiving a string value and you want to use it as an array of integers you should split it into an array first:
var receivedData = "4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606";
var dataArray = receivedData.split(",");
Afterwards you can use it with another array, however be aware that if you already have a defined array into which you want to push the dataArray you shouldn't push but concat instead.
In other words if you have:
var previousArray = [1,2,3];
previousArray.push(dataArray);
You will get
[1,2,3,[4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606]]
meaning that the whole array is pushed onto the 4th position of previousArray.
If, on the other hand, you concat the arrays will merge:
var previousArray = [1,2,3];
previousArray.concat(dataArray);
[1,2,3,4,6,7,8,9,5558,5560,5573,5574,5586,5589,5606]
Source: http://www.w3schools.com/jsref/jsref_concat_array.asp
I am returning string from database in following
format =opRadio=1&selSchool=0&opRadio2=1&selClg=0
What I want to access values like 1 or 0 in javascript or jQuery. I mean I want to retrieve 1 when I pass opRadio.
I tried by encoding string into json but no luck.
Thanks in advance.
Try this :You can use split('&') to seperate key value pair and then use split('=') to get key and value to put it in map. use map to retrieve values by passing key
var dbString = "opRadio=1&selSchool=0&opRadio2=1&selClg=0";
dbString = dbString.split('&');
var map = {};
for(var i=0;i<dbString.length;i++)
{
var keyValue = dbString[i].split('=');
map[keyValue[0]] = keyValue[1];
}
//to read value by passing key
alert(map['opRadio']);
I am grabbing an array from a p tag and it works absolutely fine, but I am unable to use it as an array.
var leaderboard = [];
leaderboard = $(".tojs").text();
console.log(leaderboard);
Output (as intended):
[["aname1",1,649,201],["aname2",2,362,171],["aname3",3,270,234],["aname4",4,233,60],["aname5",5,211,9],["aname6",6,186,101],["aname7",7,157,41]]
But the problem occurs when I try to call a value.
leaderboard[0][0]
Output:
[
How would I be able to read an array from a p tag?
The output you get from console.log is a string, not an array. Try using JSON.parse() on it before trying to access like Sergiu suggested in his comment.
var leaderboard = [];
leaderboard = JSON.parse($(".tojs").text());
console.log(leaderboard[0][0]); //should be "aname1"
var hbar = new RGraph.HBar('cvs', [100,700])
The above code is a JavasSript function which generates a bar graph 100 and 700 as parameters. The problem is MySql coloumn values. I need to pass through that function to get a complete graph of values that are been inserted.
database name :fosdb Table name [vedordb] coloumn name [name]under namejackjames are values
Tried few code snippets but was unable to figure out how to detect those mysql coloumn values and fetch them to javascript variables using php.
Ex: say MySql coloumn 'Aname' has values inserted with 100, 300
I need first fetch them to PHP; repeat a loop depending on the length then assign each array to a JavaScript loop which assigns one more array then pass these array variables to the above JavaScript function so that I can see dynamic results every time.
Please help me out friends.
Thanks in advance.
Take a look on this example, its similar to like this:
//$database_data = array(100, 200);
$data_collector = array();
// Your Query here
$stmt = "select Aname from Table_Name";
$query = mysql_query($stmt);
while( $row = mysql_fetch_array($query) ) {
$data_collector[] = $row['Aname'];
}
// Implode it
$collection = implode(", ", $data_collector);
PHP data Example : http://codepad.org/SRvxXjVK
Now pass $collection to JavaScript array like:
var Collection = [<?php echo $collection ?>];
// OR according to your code
var hbar = new RGraph.HBar('cvs', [<?php echo $collection ?>])
Hope this help you.