I am making a login app in ionic using PHP, I have a login function which works well but I need to be able to store certain values to local storage for later use.
Here is a sample from my code:
$scope.login = function() {
Login.login($scope.login).then(function (data) {
if (Object.keys(data.data).length === 1) {
var datao = angular.toJson(data);
$scope.profile = datao;
$scope.email = $scope.profile.email;
$scope.username = $scope.profile.username;
console.log(data);
console.log($scope.profile);
// $state.go('dashboard');
}
....
});
....
});
$scope.profile displays the following:
[Log]
{
"data": [{
"id": "5",
"username": "ozombo",
"reputation": "ceo",
"activate": "8425",
"followercount": "3",
"praycount": "0",
"donetour": "0",
"fb_id": "0",
"fullname": "Adebambo Oyelaja",
"church": "RCCG ",
"photo": "1417451697.jpg",
"twtr_id": "0",
"screen_name": "",
"email": "oyexs911#yahoo.com",
"bio": "Geek, Father, Husband",
"country": "Nigeria",
"state": "Lagos",
"followscount": "0",
"account": "",
"password": "5cf1bc1b9a2ada1ae9e29079aae1aefa",
"signup_date": "1413305984",
"signupdevice": "web",
"keycode": "5fc868f0767b0c164341a9e8e35edbe4",
"last_login": "1436519269",
"city": "Palmgroove",
"campaign": "",
"bday": "29-09-1988",
"gender": "Male",
"approved": "0",
"donewelcome": "0",
"phonenumber": "07017993683",
"secure": "private",
"churchid": "1",
"views": "68",
"marital": "Married"
}],
"status": 200,
"config": {
"method": "GET",
"transformRequest": [null],
"transformResponse": [null],
"url": "http://localhost:8888/opb/api/login.php?user=oyexs911#yahoo.com&pass=oyelaja",
"headers": {
"Accept": "application/json, text/plain, /"
}
},
"statusText": "OK"
}
How will I get the user email for example
You can get the email by accesing to data.email like var email = $scope.profile.data.email
You forgot that you have data before you can get the email.
{
"data": [{
"username": "ozombo",
"email": "oyexs911#yahoo.com",
...
}]
}
You can get access to the user email by iterating over the data array:
$scope.profile.data.forEach(function(d) {
console.log(d.email);
});
Hope that helps!
Related
The task is to fetch json user data from an api and store it in array. Then be able to make changes to it different routes.
Here is a sample of the data from the api, they are 10 in total and in an array.
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere#april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna#melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
},
i fetched the data using this:
$jsonurl = "http://jsonplaceholder.typicode.com/users";
$json = file_get_contents($jsonurl);
global $userData;
$userData = json_decode($json);
I displayed the data in a /users route with this
Route::get('/users', function(){
global $userData;
echo json_encode($userData);
});
I was told to get set a /delete route that deletes the last object from the array.
Route::get('/delete', function(){
global $userData;
$lastObjIndex = count($userData) -1;
array_splice($userData, $lastObjIndex,1);
//print_r('last user deleted');
echo json_encode($userData);
$userData = $userData;
});
The problem is that the delete route does not only works once(it does not delete subsequent last objects and returns the same thing when the route is accessed again) and does not show any change in when i access the users route. The data isn't stored in the server.
I feel there is something i don't know about that could help with this. Any suggestion would be appreciated.
The following is my JSON data.
[
{
"id": 1,
"Name": {
"FirstName": "Wilmer",
"LastName": "Crona",
"FullName": "Mr. Cameron Prosacco"
},
"Address": {
"Address1": "84154 Vickie Burg Apt. 994",
"Address2": "Suite 339",
"ZipCode": "89080-0376"
},
"Phone": "316-269-7694 x1364"
},
{
"id": 2,
"Name": {
"FirstName": "Mercedes",
"LastName": "Kshlerin",
"FullName": "Dr. Kellie Bashirian"
},
"Address": {
"Address1": "12638 Cali Spurs",
"Address2": "Suite 353",
"ZipCode": "76622"
},
"Phone": "319-329-3169 x8848"
}
]
Here is where I want my JSON like this: https://github.com/Zlob/php-json-server but in the method. The JSON file must one file and process only one JSON (db.json).
If my JSON file json1.json , json2.json, etc. How should I do?
You can't decode only first element, you can decode all and get first element only, here is online compiler
<?php
$json = '[
{
"id": 1,
"Name": {
"FirstName": "Wilmer",
"LastName": "Crona",
"FullName": "Mr. Cameron Prosacco"
},
"Address": {
"Address1": "84154 Vickie Burg Apt. 994",
"Address2": "Suite 339",
"ZipCode": "89080-0376"
},
"Phone": "316-269-7694 x1364"
},
{
"id": 2,
"Name": {
"FirstName": "Mercedes",
"LastName": "Kshlerin",
"FullName": "Dr. Kellie Bashirian"
},
"Address": {
"Address1": "12638 Cali Spurs",
"Address2": "Suite 353",
"ZipCode": "76622"
},
"Phone": "319-329-3169 x8848"
}
]';
$data = json_decode($json);
$firstObject = $data[0];
var_dump($firstObject);
pass database name in url and set the db name in config. put all your json file inside db folder.
namespace App\Http\Controllers;
use Request;
use Response;
use Config;
use JsonServer\JsonServer;
class JsonServerController extends Controller
{
public function handleRequest($db, $uri)
{
$data = Request::all();
$method = Request::method();
$pathToJson = storage_path($db .'.json'); //if your path in inside storage folder of laravel
Config::set('pathToDb', $pathToJson); //here we set db
$jsonServer = new JsonServer();
$response = $jsonServer->handleRequest($method, $uri, $data);
$response->send();
}
}
Now define in routes
Route::any('api/{db}/{all}', "JsonServerController#handleRequest")->where('all', '.*');
Now the request will be like this for json1 database
GET api/json1/posts
POST api/json1/posts
PUT api/json1/posts/1
PATCH api/json1/posts/1
DELETE api/json1/posts/1
For json2 database
GET api/json2/posts
POST api/json2/posts
PUT api/json2/posts/1
PATCH api/json2/posts/1
DELETE api/json2/posts/1
Bellow JSON contain my fan page info in the json format.I tried getJson in the jquery but it didn't work for me
{
"about": " One who does something \n\n",
"description": "Doers Web development\nDoers Software Development\nDoers Graphics design\nDoers Hardware & Networking\nDoers Mobile Application Development",
"is_published": true,
"location": {
"street": "",
"city": "Colombo",
"country": "Sri Lanka",
"zip": ""
},
"phone": "+94773633412",
"talking_about_count": 686,
"username": "DoersIncorporation",
"website": "http://www.doers.lk",
"were_here_count": 0,
"category": "Computers/technology",
"id": "397319800348866",
"name": "Doers Inc",
"link": "https://www.facebook.com/DoersIncorporation",
"likes": 767,
"cover": {
"cover_id": 398352273578952,
"source": "http://sphotos-f.ak.fbcdn.net/hphotos-ak-snc7/s720x720/484190_398352273578952_36222530_n.png",
"offset_y": 0
}
}
What i want to know is how to parse cover image from it using php and jquery ???
Save the JSON to a variable:
var x = theJSON
x.cover.source
will return the url
Fiddle
I'm working with some videos on PHP, using zencoder to encode the videos, save them on s3 and then notify my site back when it's all done.
Everything is working until I have to process the notifications returned as json and pull out the new url to the saved video.
this:
$notification = $zencoder->notifications->parseIncoming();
if($notification->job->state == "finished")
{
$encode_id=$notification->job->id;
}
works fine. I just need some pointers on accessing the url.
The notification is sent as:
{
"output": {
"frame_rate": 30.0,
"label": "video_id_",
"total_bitrate_in_kbps": 3115,
"md5_checksum": null,
"channels": "2",
"audio_codec": "aac",
"duration_in_ms": 4225,
"video_codec": "h264",
"url": "http://my_url/597bd3592bf4a9d70f04dc676c44de6d.mp4",
"thumbnails": [{
"label": null,
"images": [{
"url": "http://my_url/_key__0000.png",
"format": "PNG",
"dimensions": "640x360",
"file_size_bytes": 482422
}]
}],
"video_bitrate_in_kbps": 3052,
"width": 640,
"format": "mpeg4",
"height": 360,
"audio_sample_rate": 44100,
"state": "finished",
"audio_bitrate_in_kbps": 63,
"id": 41424918,
"file_size_in_bytes": 1625847
},
"input": {
"frame_rate": 30.0,
"total_bitrate_in_kbps": 3867,
"md5_checksum": null,
"channels": "2",
"audio_codec": "aac",
"duration_in_ms": 4167,
"video_codec": "h264",
"video_bitrate_in_kbps": 3764,
"width": 640,
"format": "mpeg4",
"height": 360,
"audio_sample_rate": 44100,
"state": "finished",
"audio_bitrate_in_kbps": 103,
"id": 22371764,
"file_size_in_bytes": 2028809
},
"job": {
"created_at": "2012-07-14T22:25:08Z",
"test": true,
"updated_at": "2012-07-14T22:25:47Z",
"submitted_at": "2012-07-14T22:25:08Z",
"pass_through": null,
"state": "finished",
"id": 22377083
}
}
but something like: $video_file=$notification->output->url; doesn't.
What am I missing?
If you don't want to use the parseIncoming method...
Use:
$notification = json_decode(trim(file_get_contents('php://input')), true);
as apposed to:
$notification = $zencoder->notifications->parseIncoming();
The second paramater of 'true' will format the results as an array as apposed to an object. From there, you can access all the values like:
$notification['output']['file_size_in_bytes'];
The parseIncoming method will return a stdClass, so referencing values within it is done with:
$notification->key
I'm new to JSON and really struggling with this. I've read countless other posts and web pages but can't seem to figure it out.
I'm using PHP to output JSON (from data from the database) with this code:
header('Content-type: application/json');
echo json_encode($data);
Here is the JSON:
{
"x0": {
"id": "1",
"name": "Rob",
"online": "1",
"gender": "m",
"age": "29",
"height": "5'8''",
"build": "Average",
"ethnicity": "White",
"description": "Art geek person",
"looking_for": "Anything",
"image": "4fs5d43f5s4d3f544sdf.jpg",
"last_active": "29-06-11-1810",
"town": "Manchester",
"country": "UK",
"distance": 0.050973560712308
},
"x1": {
"id": "2",
"name": "Dave",
"online": "1",
"gender": "m",
"age": "29",
"height": "5'8''",
"build": "Average",
"ethnicity": "White",
"description": "Art geek person",
"looking_for": "Anything",
"image": "4fs5d43f5s4d3f544sdf.jpg",
"last_active": "29-06-11-1810",
"town": "Manchester",
"country": "UK",
"distance": 0.050973560712308
}
}
I think the problem I'm having is that the JSON is nested(might be wrong there)?.
This is the JQuery:
function fetchProfiles() {
var url='http://url.com/here';
var i = 0;
var handle = 'x'.i;
$.getJSON(url,function(json){
$.each(json.results,function(i,profile){
$("#profiles").append('<p><img src="'+profile.handle.image+'" widt="48" height="48" />'+profile.handle.name+'</p>');
i++;
});
});
}
Any ideas or suggestions appreciated!
Thanks!
i think that the problem is that you call $.each on json.results (if the json is exactly what you showed us).
you sholud do:
$.each(json,function(i,profile){
$("#profiles").append('<p><img src="'+profile.image+'" widt="48" height="48" />'+profile.name+'</p>');
});
look at the fiddle here: http://jsfiddle.net/ENcVd/1/ (it alerst the image property of you json object)