Does anybody have experience using Strapi.io and combine it with Codeigniter.
I have difficult when i need to make JSON in my controller same like strapi needed.
My JSON look like below
string(102) "{"id":"3","Name":"Paket Anak-anak","Description":"Loren ipsum","Price":"11000","Image":"Capture3.PNG"}"
Whilist what Strapi need to POST request
kind of like below
{
"data": [
{
"id": 2,
"attributes": {
"Name": "Paket Medium",
"Description": "Loren ipsum qqiqqfqqv",
"Price": 120000,
"Image": null,
"createdAt": "2022-09-17T05:44:18.713Z",
"updatedAt": "2022-09-17T05:44:20.723Z",
"publishedAt": "2022-09-17T05:44:20.720Z"
}
}
],
}
Related
I have a data set coming from an API. Data does not have an id attribute in it. When we want to show specific data on the base of its id in modal we declare a data-id attribute in html but if we do not have any id associated to data. How can we show the contents of it on modal?
See the data, I am showing name and have more-info button for modal. what should i do, when someone clicks on more-info button. It shows specific content?
There is also a next url in the data how can i paginate it in php?
{
"count": 37,
"next": "http://some-url/api/stars/?page=2",
"previous": null,
"results": [
{
"name": "Sentinel-class landing craft",
"model": "Sentinel-class landing craft",
"manufacturer": "Sienar Fleet Systems, Cyngus Spaceworks",
"cost_in_credits": "240000",
"length": "38",
"max_atmosphering_speed": "1000",
"crew": "5",
"passengers": "75",
"cargo_capacity": "180000",
"consumables": "1 month",
"hyperdrive_rating": "1.0",
"MGLT": "70",
"starship_class": "landing craft",
"pilots": [],
"films": [
"http://some-url/api/films/1/"
],
"created": "2014-12-10T15:48:00.586000Z",
"edited": "2014-12-22T17:35:44.431407Z",
"url": "http://some-url/api/stars/5/"
},
{
"name": "Death Star",
"model": "DS-1 Orbital Battle Station",
"manufacturer": "Imperial Department of Military Research, Sienar Fleet Systems",
"cost_in_credits": "1000000000000",
"length": "120000",
"max_atmosphering_speed": "n/a",
"crew": "342953",
"passengers": "843342",
"cargo_capacity": "1000000000000",
"consumables": "3 years",
"hyperdrive_rating": "4.0",
"MGLT": "10",
"starship_class": "Deep Space Mobile Battlestation",
"pilots": [],
"films": [
"http://some-url.co/api/films/1/"
],
"created": "2014-12-10T16:36:50.509000Z",
"edited": "2014-12-22T17:35:44.452589Z",
"url": "http://some-url.co/api/starships/9/"
},
$arrayData = json_decode($jsonData);
$i=0;
foreach($arrayData->results as $data)
{
$arrayData->results[$i]->name;
$arrayData->results[$i]->id=$i;
$i++;
}
I have read about json_encode but still lack the logic in using it for my needs on this particular JSON structure.
Assuming the JSON structure is as follows :
{
"_id": "23441324",
"api_rev": "1.0",
"type": "router",
"hostname": "something",
"lat": -31.805412,
"lon": -64.424677,
"aliases": [
{
"type": "olsr",
"alias": "104.201.0.29"
}
],
"site": "roof town hall",
"community": "Freifunk/Berlin",
"attributes": {
"firmware": {
"name": "meshkit",
"url": "http:k.net/"
}
}
}
Some of the values of the attributes will be taken from the database while some are going to be hardcoded(static) like "type","api_rev". I was thinking of just using concatenation to build the structure but learnt its a bad idea. So if i am to use json_encode how may i be able to handle this structure ? array dimensions etc.
My PHP code:
$obj = json_decode($data);
print $obj->{'name'};
While it works for non-arrays, I can't for the life of me figure out how to print all the values within the "Reviews" Array.
What I would like to do is to loop through this response, probably with forreach(), resulting in a list containing the rating and excerpt for each review in the response.
Any guidance / direction is greatly appreciated..
Below is the JSON I'm working with. (it is the response from the Yelp API).
{
"is_claimed": true,
"rating": 4.5,
"mobile_url": "http://m.yelp.com/biz/economy-paint-and-collision-riverside",
"rating_img_url": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png",
"review_count": 19,
"name": "Economy Paint & Collision",
"snippet_image_url": "http://s3-media3.ak.yelpcdn.com/photo/ZOzoahw0Go_DEPLvxCaP_Q/ms.jpg",
"rating_img_url_small": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png",
"url": "http://www.yelp.com/biz/economy-paint-and-collision-riverside",
"reviews": [
{
"rating": 3,
"excerpt": "The Good:\nDennis quoted me a price over the phone about 1 month before I took my wifes 2010 Escalade in for repairs and when I took it in he gave me the...",
"time_created": 1357010247,
"rating_image_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/34bc8086841c/ico/stars/v1/stars_3.png",
"rating_image_small_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/902abeed0983/ico/stars/v1/stars_small_3.png",
"user": {
"image_url": "http://s3-media3.ak.yelpcdn.com/photo/mIsU7ugYd88lLA-XL2q1Cg/ms.jpg",
"id": "V9MDZvEBv-tBTF4YIoc7mg",
"name": "Sydney H."
},
"rating_image_large_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/e8b5b79d37ed/ico/stars/v1/stars_large_3.png",
"id": "HfOhzLIlJoUKSKU8euclqA"
},
{
"rating": 5,
"excerpt": "Dennis and his team did an amazing job on the roof of my fiancee's 2002 Acura RSX after years of living by the beach in San Francisco had mostly rusted...",
"time_created": 1354741952,
"rating_image_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
"rating_image_small_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
"user": {
"image_url": "http://s3-media3.ak.yelpcdn.com/photo/ZOzoahw0Go_DEPLvxCaP_Q/ms.jpg",
"id": "kOqCnCjYn0EbAhtH1tfjcw",
"name": "Jason H."
},
"rating_image_large_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
"id": "YzZg1LX6zeRaurq9tYUcMw"
},
{
"rating": 5,
"excerpt": "It's been a year since I had my car painted here, and I gotta say: It still looks just as good as it did when I first picked it up. You would never know...",
"time_created": 1361043626,
"rating_image_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
"rating_image_small_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
"user": {
"image_url": "http://s3-media1.ak.yelpcdn.com/photo/58coTtu1x5riHSgFEAQsfw/ms.jpg",
"id": "kVrW3138d5VL-AZ97wFF4A",
"name": "Jeanne M."
},
"rating_image_large_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
"id": "r5WtlQVMXiIMBR6S3N7RZw"
}
],
"phone": "9517870227",
"snippet_text": "Dennis and his team did an amazing job on the roof of my fiancee's 2002 Acura RSX after years of living by the beach in San Francisco had mostly rusted...",
"image_url": "http://s3-media3.ak.yelpcdn.com/bphoto/kodoEcmgHRG61pPaWRndbw/ms.jpg",
"categories": [
[
"Body Shops",
"bodyshops"
],
[
"Auto Repair",
"autorepair"
]
],
"display_phone": "+1-951-787-0227",
"rating_img_url_large": "http://s3-media4.ak.yelpcdn.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png",
"id": "economy-paint-and-collision-riverside",
"is_closed": false,
"location": {
"city": "Riverside",
"display_address": [
"2548 Rubidoux Blvd",
"Riverside, CA 92509"
],
"geo_accuracy": 8,
"postal_code": "92509",
"country_code": "US",
"address": [
"2548 Rubidoux Blvd"
],
"coordinate": {
"latitude": 34.0132437,
"longitude": -117.3923804
},
"state_code": "CA"
}
}
You are probably having trouble because reviews is an array and you are trying to access it as a JSON object.
$obj = json_decode($data, TRUE);
for($i=0; $i<count($obj['reviews']); $i++) {
echo "Rating is " . $obj['reviews'][$i]["rating"] . " and the excerpt is " . $obj['reviews'][$i]["excerpt"] . "<BR>";
}
I'm not sure what exactly you want but I guess you want print it just for debugging right now. You can try with print_r($obj); and var_dump($obj); - they must print something, especially var_dump().
When you see the data, you can easily edit function a little bit, so you can do for instance print_r($obj->reviews) or print_r($obj['reviews']), depending if $obj is object or array.
You can use var_dump or print_r.
<?php
$decodedJSON = json_decode($jsonData);
// Put everyting to the screen with var_dump;
var_dump($decodedJSON);
// With print_r ( useful for arrays );
print_r($decodedJSON);
// List just review ratings with foreach;
foreach($decodedJSON['reviews'] as $review){
echo $review['rating'];
}
?>
Here using objects example (to read reviews...raiting):
$jsonObject = json_decode($data);
foreach ($jsonObject->reviews as $data) {
echo $data->rating;
}
I am trying to integrate the bryntum component(schedule) in php. I am not much aware in ext js.
Please see the images here
Here, Name fields are fetching properly, whereas Capacity is not accessing. These values are coming from Zoho CRM.
My code is like Click, whereas r-read.php file is the responsible file for fetching the record from CRM and store it in a json format. It is like
{
"success": true,
"total": 9,
"root": [{
"Id": 1,
"Name": "Sri Test",
"Capicity": "190.0"
}, {
"Id": 2,
"Name": "tester_test01",
"Capicity": "500.0"
}, {
"Id": 3,
"Name": "Tesing room 23",
"Capicity": "5000.0"
}, {
"Id": 4,
"Name": "Test for 6th product",
"Capicity": "5000.0"
}, {
"Id": 5,
"Name": "Banquet hall test-01",
"Capicity": "500.0"
}, {
"Id": 6,
"Name": "test room",
"Capicity": "1000.0"
}, {
"Id": 7,
"Name": "Grande Ballroom",
"Capicity": "4000.0"
}, {
"Id": 8,
"Name": "Cedar Room",
"Capicity": "1400.0"
}, {
"Id": 9,
"Name": "Maple Room",
"Capicity": "1200.0"
}]
}
In the capacity column, it will show like 190.0 , 500.0, 5000.0 etc like Name column.
I'm not familier with the Bryntum schedular component, but most of the time when you have problems like these it's because you didn't define the Capacity field in your model.
I saw you used the following model: Sch.model.Resource. Can it be that is only has the Name field and not Capacity? Your JSON response looks fine to me.
In the sample JSON above, Capacity is spelled Capicity.
See if the same spelling needs can be used everywhere. Maybe then the data will resolve properly.
Recently, our team is going to develop mobile(iphone, android platforms) applications for our existing website, let user can use the application to more easy to read our content via the application.
But our team have different views in JSON schema of the API return, below are the sample response.
Schema type 1:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": {
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
"user": {
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": {
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
},
"user": {
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/2"
},
"post_ts": "2012-12-01 18:15:00T0800"
}
]
}
}
Schema type 2:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": 100,
"user": 1,
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": 101,
"user": 2,
"post_ts": "2012-12-01 18:15:00T0800"
}
],
"category": [
{
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
{
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
}
],
"user": [
{
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
{
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/Hello_baby"
}
]
}
}
Some Developers claim that if using schema type 2,
can reduce data size if the category & user entities comes too much duplicated. it does really reduce at least 20~40% size of response plain text.
once if the data size come less, in parsing it to JSON object, the memory get less
categoey & user can be store in hash-map, easy to reuse
reduce the overhead on retrieving data
I have no idea on it if schema type 2 does really enhanced. Because I read so many API documentation, never seen this type of schema design. For me, it looks like a relational database. So I have few questions, because I have no experience on designing a web services API.
Does it against API design principle (Easy to read, Easy to use) ?
Does it really get faster and get less memory resource on parsing on IOS / Android platform?
Does it can reduce the overhead between client & server?
Thanks you.
When I do such an application for android, I parse JSON just one and put it in database. Later I'm using ContentProvider to access it. In Your case You could use 2nd schema but without user, category part. Use lazy loading instead but it will be good solution just in case categories and users repeat often.