pagination with this json data using php - php

i am sending this to the API and receiving this in return
URL/list?limit=10&page=1
{
"code": 1,
"data": {
"current_page": 1,
"data": [
{
"id": 30,
"first_name": "jack",
"last_name": null,
},
{
"id": 31,
"first_name": "Bean",
"last_name": null,
},
{
"id": 32,
"first_name": "Stock",
"last_name": null,
},
{
"id": 33,
"first_name": "Tangled",
"last_name": null,
},
{
"id": 34,
"first_name": "Anna",
"last_name": null,
},
{
"id": 104,
"first_name": "Tom",
"last_name": "M",
}
],
"from": 1,
"last_page": 2,
"last_page": 2,
"next_page_url": "URL/list?page=2",
"path": "URL/list",
"per_page": "10",
"prev_page_url": "NULL",
"to": 10,
"total": 19
},
"msg": "Success"
}
now what i want to do is do a simple pagination but i do not know how to go about it using this particular data can some one please help me out what i think should be used is a bit of PHP jquery(AJAX) and html
but i can no seem to get it i am very new to this and if some one could help me out it would be great
Please note i have no control over what the JSON Data thus i can not change it

Cannot post as a comment due to low reputation but I do want to say this.
StackOverflow isn't a place where you can just pawn off your dirty work on others here. You have to at least give your best shot and tell us where you're stuck with in order for people to help you.
Here's a few resources on learning Pagination with PHP as well as several other answers on this site that help others with Pagination.
http://www.phpfreaks.com/tutorial/basic-pagination
Simple PHP Pagination script
http://codingcyber.org/simple-pagination-script-php-mysql-5882/
Most of these scripts you find will include MySQL, but for your array you would use array_split() in your case. Unless the data you're providing is also from MySQL then these will work great for you.

Related

PHP - Retrieve JSON data and values [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I'm new into PHP and JSON and I have a problem, I want to retrieve a item and value from a JSON:
{
"status": true,
"webhook_type": 100,
"data": {
"product": {
"id": "lSEADIQ",
"attachment_id": null,
"title": "Registration",
"description": null,
"image": null,
"unlisted": false,
"type": "service",
"price": 1,
"currency": "EUR",
"email": {
"enabled": false
},
"stock_warning": 0,
"quantity": {
"min": 1,
"max": 1
},
"confirmations": 1,
"custom_fields": [
{
"name": "Forum username",
"type": "text",
"required": true
}
],
"gateways": [
"Bitcoin"
],
"webhook_urls": [],
"dynamic_url": "",
"position": null,
"created_at": "2018-10-01 12:51:12",
"updated_at": "2018-10-01 12:55:46",
"stock": 9223372036854776000,
"accounts": []
},
"order": {
"id": "8e23b496-121a-4dc6-8ec4-c45835680db2",
"created_at": "Tue, 02 Oct 2018 00:54:56 +0200",
"paid_at": null,
"transaction_id": null,
"confirmations": 1,
"required_confirmations": 3,
"received_amount": 0,
"crypto_address": "1NeNQws7JLbTr6bjekfeaXSV7XiyRsv7V8",
"crypto_amount": "0.4815",
"quantity": 1,
"price": 19.99,
"currency": "EUR",
"exchange_rate": "1.21",
"gateway": "BTC",
"email": "webhook#site.gg",
"ip_address": "123.456.789.111",
"agent": {
"geo": {
"ip": "214.44.18.6",
"iso_code": "US",
"country": "United States"
},
"data": {
"is_mobile": false,
"is_table": false,
"is_desktop": true,
"browser": {
"name": "Chrome",
"version": "63.0.3239.132"
}
}
},
"custom_fields": [
{
"name": "user_id",
"value": 184191
}
],
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3)"
}
}
}
I want to retrieve items from data -> order, for example "id" or "ip_address".
Thank you for read this, I hope someone can help me in this, because I'm lost, I started to code very recently and I'm trying to learn a lot.
Regards!
Where test.json is the json you uploaded, place it in a file named test.json and ensure its placed in the same directory.
<?php
$load = file_get_contents("test.json") or die("JSON load failed");
$json_a = json_decode($load, true);
print $json_a['data']['order']['ip_address'] . "\n";
?>
Gives:
123.456.789.111
My answer reads the JSON from a file as were it dumped directly in your code, which indeed it could be, it would make the code less readable and your file more messy.
If you dont want to place the file in the same directory, simply specify the full file path. E.g. file_get_contents("this/dir/here/test.json");
You can read about how json_decode works here, its essential we pass it the true parameter to make our arrays associative.
You can extract your need array from JSON data. You can use a loop too to read all your data inside the order array.
$array = json_decode($json, true);
$verbose = $array['data'];
$orderArray = $verbose['order'];
print_r($orderArray);
echo $orderArray['id'];
echo $orderArray['ip_address'];

How can I iterate though this JSON using php/laravel

I need to be access each level. I am using laravel blade and php. I have been trying a few different way with no success, here is an example of what I tried:
#foreach($exercisePlan as $al)
#if(isset($al->exercises))
<li>{{$al->exercise->description}}</li>
#endif
#endforeach
Here is my Json, I would be very grateful if someone could help me out here. Thanks in advance!
{
"id": 1,
"userID": 1,
"exercisePlanID": 1,
"updated_at": null,
"created_at": null,
"exercises": [
{
"id": 1,
"planID": 1,
"trainerID": "1",
"day": "Monday",
"exercise": [
{
"id": 1,
"name": "Concentration Curls",
"bodyPart": "Arm",
"muscle": "Bicep",
"description": "While sitting on a bench with your feet firmly on the floor, place the back of your left upper arm on the inside of your thigh. Keep your arm on your thigh throughout. Put your right hand on the right knee for stability. Do your curls on the left side, then repeat on the right side. ",
"video": "https:\/\/www.youtube.com\/watch?v=ebqgIOiYGEY",
"created_at": null,
"updated_at": null
}
],
"reps": "10",
"sets": "5",
"priority": "1",
"weight": "10kg",
"created_at": null,
"updated_at": null
},
{
"id": 2,
"planID": 1,
"trainerID": "2",
"day": "Monday",
"exercise": [
{
"id": 2,
"name": "Preacher Curls",
"bodyPart": "Arm",
"muscle": "Bicep",
"description": "Using a regular preacher bench, grab an EZ Curl bar with both hands using an underhand grip (palms facing upwards). Slowly curl the bar up to the top and bring it a few inches from your chin. Return the weight back down with a slow and controlled tempo to the starting position, allowing some resistance (negative) on the way back down. Repeat the movement for the desired number of repetitions.",
"video": "https:\/\/www.youtube.com\/watch?v=fIWP-FRFNU0",
"created_at": null,
"updated_at": null
}
],
"reps": "5",
"sets": "5",
"priority": "2",
"weight": "15kg",
"created_at": null,
"updated_at": null
}
]
}
Edit now getting error:Undefined variable: exercise:
#foreach($exercisePlan as $al)
#if(isset($al->exercises))
#foreach($al->$exercises as $exercise)
<li>{{$exercise->description}}</li>
#endforeach
#endif
#endforeach
Got it:
#foreach($exercisePlan as $plan)
#foreach($plan->exercises as $exercises)
#foreach($exercises->exercise as $exercise)
{{$exercise->name}}
#endforeach
#endforeach
#endforeach

Using blog post tags with JSON

I have some JSON as below
"tags":
{
"business-school":
{
"name": "Business School",
"slug": "business-school",
"parent": 0,
"description": "",
"post_count": 13,
"id": 169072,
"taxonomy": "post_tag"
},
"canterbury":
{
"name": "Canterbury",
"slug": "canterbury",
"parent": 0,
"description": "",
"post_count": 37,
"id": 5349,
"taxonomy": "post_tag"
I have managed to display the relevant tags that were entered with the posts. On other blogs though I have seen the tags as hyperlinks and was wondering how this was implemented. what would they be linked to?. Is it something that is possible within a JSON framework with PHP bearing in mind I am not that familiar with JSON or is it something that is best left to blogging sites.
If you can keep the relationship of your data like below then you can easily implement that TAG with HYPERLINKS technique with your blog post also.
Format for TAGS Where each Tag will be associated with an ID
So your anchor link will be like -
https://blogs.canterbury.ac.uk/cafa/tag/555/ https://blogs.canterbury.ac.uk/cafa/tag/777/
"tags":
{
"business-school":
{
"name": "Business School",
"slug": "business-school",
"parent": 0,
"description": "",
"post_count": 13,
"id": 169072,
"taxonomy": "post_tag",
"tag_id": 555
},
"canterbury":
{
"name": "Canterbury",
"slug": "canterbury",
"parent": 0,
"description": "",
"post_count": 37,
"id": 5349,
"taxonomy": "post_tag",
"tag_id": 777
}
}
Format for Blog Post in respect to those TAG ID
When user click on any of those hyperlinks you'll get the TAG ID & from those TAG ID you can respective POSTS tagged with those ID. Ex- TAG ID 555 is associated with 2 POSTS.
{
"555": [{
"blog_id": 111,
"blog_excerpt": "your blog excerpt",
"date": "2017-04-15"
}, {
"blog_id": 222,
"blog_excerpt": "your blog excerpt",
"date": "2017-04-16"
}],
"777": [{
"blog_id": 333,
"blog_excerpt": "your blog excerpt",
"date": "2017-04-17"
}, {
"blog_id": 444,
"blog_excerpt": "your blog excerpt",
"date": "2017-04-18"
}]
}
Hope you'll get my Point.

JSON API Plugin for wordpress not returning the right things

I create a mobile application for my site in static html, now i want use ajax to take my posts, pages, titles, images, etc.. from my wordpress original site.
For this, the best solution i think is use the JSON API plugin -> https://wordpress.org/plugins/json-api/
I already installed, and it's working good in the pages, except for the main page, it's not sending the right thinks for my mobile app.
Anyone knows what is the problem? This is a problem with my wordpress site or maybe a plugin problem?
It's sending this:
{
"status": "ok",
"count": 1,
"count_total": 1,
"pages": 1,
"posts": [
{
"id": 1689,
"type": "post",
"slug": "teste",
"url": "http://myurlsite.com/teste/",
"status": "publish",
"title": "Teste",
"title_plain": "Teste",
"content": "<p>[123-contact-form i523156]</p>\n",
"excerpt": "<p>[123-contact-form i523156]</p>\n",
"date": "2013-12-10 20:11:14",
"modified": "2013-12-10 20:11:14",
"categories": [
{
"id": 1,
"slug": "sem-categoria",
"title": "Sem categoria",
"description": "",
"parent": 0,
"post_count": 1
}
],
"tags": [],
"author": {
"id": 1,
"slug": "admin",
"name": "admin",
"first_name": "Administrador",
"last_name": "Wemaster",
"nickname": "admin",
"url": "",
"description": ""
},
"comments": [],
"attachments": [],
"comment_count": 0,
"comment_status": "open",
"custom_fields": {
"cro_sidebar": [
"1"
],
"cro_sidebarsel": [
"0"
],
"cro_readmore": [
"1"
],
"cro_showpromo": [
"1"
]
}
}
]
}
If u observe, it's returning a post, not a page:
"type": "post",
"slug": "teste",
"url": "http://myurlsite.com/teste/",
"status": "publish",
"title": "Teste",
"title_plain": "Teste",
I delete the test post and now it~s returning this:
{"status":"ok","count":0,"count_total":0,"pages":0,"posts":[]}
Try accessing the post via this url:
http://www.yourdomain.com/api/get_page/?slug=<page-slug>
or use the page id instead:
http://www.yourdomain.com/api/get_page/?id=<page-id>
I've tested both and both work with that plug-in.
For more information read this information by the developer(s).

Why can't my facebook fql query more than 2 facebook values?

Trying to create a facebook app just to learn and coming across a strange phenomenon that I'm not sure what to make of.
I understand all the JSON conversions but it seems I can't query more than 2 facebook values at a time when supposedly I'm supposed to be able to...(??? sorry if that was confusion). Below is my code taken out of context.
$query="query".$i;
$idarray["$query"]="SELECT+pic,name+FROM+user+WHERE+uid=".$friend_id[$i];
When I try to query more than pic and name, it only gives me two. It will not let me query something like "pic,name,username" etc. Does anyone know the reason why? And if so, how can I solve this to query more than 2?
Thank you in advance!
This works fine for me, i can't determine what you're doing that's causing this to not work:
select uid, name, first_name, middle_name, last_name, sex, locale, pic_small_with_logo, pic_big_with_logo, pic_square_with_logo, pic_with_logo, username from user where uid in (4,5,6,7)
Return is:
{
"data": [
{
"uid": 4,
"name": "Mark Zuckerberg",
"first_name": "Mark",
"middle_name": "",
"last_name": "Zuckerberg",
"sex": "male",
"locale": "en_US",
"pic_small_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDIpcVgo2Fz4U4e&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F157340_4_3955636_t.jpg&logo&v=5",
"pic_big_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCa1XUcpDZryPMM&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F49942_4_1525300_n.jpg&logo&v=5",
"pic_square_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBYWqMD88h5CkD3&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F157340_4_3955636_q.jpg&logo&v=5",
"pic_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDqZdUCq_U_senP&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F157340_4_3955636_s.jpg&logo&v=5",
"username": "zuck"
},
{
"uid": 5,
"name": "Chris Hughes",
"first_name": "Chris",
"middle_name": "",
"last_name": "Hughes",
"sex": "male",
"locale": "en_US",
"pic_small_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBm5Un8ZmN8OMxy&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F211833_5_64370680_t.jpg&logo&v=5",
"pic_big_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQB0gVdhHSj4v6gN&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F211833_5_64370680_n.jpg&logo&v=5",
"pic_square_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBTEKDI6lBvbtcE&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F211833_5_64370680_q.jpg&logo&v=5",
"pic_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCYybISnGDBbBfO&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F211833_5_64370680_s.jpg&logo&v=5",
"username": "ChrisHughes"
},
{
"uid": 6,
"name": "Dustin Moskovitz",
"first_name": "Dustin",
"middle_name": "",
"last_name": "Moskovitz",
"sex": "male",
"locale": "en_US",
"pic_small_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQAq87DIy_gbhxVb&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F173132_6_1123895199_t.jpg&logo&v=5",
"pic_big_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBtlc6qP74cEMpQ&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F173132_6_1123895199_n.jpg&logo&v=5",
"pic_square_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQAumnkA5BT4KUVk&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F173132_6_1123895199_q.jpg&logo&v=5",
"pic_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQBJZfp6ui5GWXRZ&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F173132_6_1123895199_s.jpg&logo&v=5",
"username": "moskov"
},
{
"uid": 7,
"name": "Arie Hasit",
"first_name": "Arie",
"middle_name": "",
"last_name": "Hasit",
"sex": "male",
"locale": "en_US",
"pic_small_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQAVKTiIJr8Yf9KR&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F23251_7_6318_t.jpg&logo&v=5",
"pic_big_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQDonksAfqN0W_GS&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F23161_7_4183_n.jpg&logo&v=5",
"pic_square_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQAP10kv_ZDVA6XF&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F23251_7_6318_q.jpg&logo&v=5",
"pic_with_logo": "https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCL96KpL_7TLRar&url=https\u00253A\u00252F\u00252Ffbcdn-profile-a.akamaihd.net\u00252Fhprofile-ak-snc4\u00252F23251_7_6318_s.jpg&logo&v=5",
"username": "arie.hasit"
}
]
}

Categories