I am building a Project management app using JavaScript and PHP.
Many of my JavaScript files will need access to a user list from my backend database. To increase performance or server hits I would like to build the user list with user data attached 1 time in PHP and print it to the screen as a JavaScript Object which my other JavaScript files can then access to get users data.
Assuming I have 20 users.
I need to have access to these user fields in ALL my JavaScript app/objects:
- User name
- User ID
- User ACL role
- User gravatar image URL
I am uncertain if I should generate a JavaScript Object with my PHP of the userlist or if it needs to be generated as JSON?
Also which ever it be, object or JSON, can you please show how it should be formatted to include multiple user records in the list and then how I would access a users data from my other JavaScript files assuming I have the user's ID.
So if I have user ID 1, I can call a function or object from my other JavaScript files and they will have access to all data for that user with ID 1 by getting it from the JavaScript that my PHP would print into the header of page.
Any help please?
Ideally I think something like being able to call:
var user = cureentJsObject.getUser(userId);
user.gravatarUrl // https://www.gravatar.com/avatar/7ab1baf18a91ab4055923c5fd01d68a2?s=48&d=identicon&r=PG
user.username // JasonDavis
user.role // admin
Being able to do something like that inside of my other JavaScript object/apps based on the data in the header of screen for userlist would be really nice but I am not certain how to do it?
UPDATE
I think this PHP would generate what I want. Just have to figure out the best way to access it in all of my objects in other JS files in the page
<?php
// PHP array
$userList = array(
array(
'id' => '1gdfgdfkn123423423',
'username' => 'JasonDavis',
'role' => 'admin',
'gravatar' => 'https://www.gravatar.com/avatar/7ab1baf18a91ab4055923c5fd01d68a2?s=48&d=identicon&r=PG'
),
array(
'id' => '2gdfgdfkn123423423',
'username' => 'John Doe',
'role' => 'user',
'gravatar' => 'https://www.gravatar.com/avatar/7ab1baf18a91ab4055923c5fd01d68a2?s=48&d=identicon&r=PG'
),
array(
'id' => '3gdfgdfkn123423423',
'username' => 'Rick James',
'role' => 'user',
'gravatar' => 'https://www.gravatar.com/avatar/7ab1baf18a91ab4055923c5fd01d68a2?s=48&d=identicon&r=PG'
),
);
?>
var users = <?php echo json_encode($userList) ?>;
JSON output:
var users = [{
"id": "1gdfgdfkn123423423",
"username": "JasonDavis",
"role": "admin",
"gravatar": "https:\/\/www.gravatar.com\/avatar\/7ab1baf18a91ab4055923c5fd01d68a2?s=48&d=identicon&r=PG"
}, {
"id": "2gdfgdfkn123423423",
"username": "John Doe",
"role": "user",
"gravatar": "https:\/\/www.gravatar.com\/avatar\/7ab1baf18a91ab4055923c5fd01d68a2?s=48&d=identicon&r=PG"
}, {
"id": "3gdfgdfkn123423423",
"username": "Rick James",
"role": "user",
"gravatar": "https:\/\/www.gravatar.com\/avatar\/7ab1baf18a91ab4055923c5fd01d68a2?s=48&d=identicon&r=PG"
}];
UPDATE 2
I can now access the user from the above...
<script>
function findById(source, id) {
for (var i = 0; i < source.length; i++) {
if (source[i].id === id) {
return source[i];
}
}
throw "Couldn't find object with id: " + id;
}
var users = <?php echo json_encode($userList) ?>;
var id = '2gdfgdfkn123423423';
var user = findById(users, id);
alert(user.gravatar);
</script>