Here i have one JSON response, in that response i am getting values like companyId,and divisionName etc..., now i want find companyId one means i want to find what is the company name, so written one function and also i got the company name, my problems is in this name i am not able to append same object
protected function getStates(){
// Cross validation if the request method is GET else it will return "Not Acceptable" status
if($this->get_request_method() != "GET"){
$response_array['status']='fail';
$response_array['message']='GET method only accept';
$response_array['data']='';
$this->response($this->json($response_array), 405);
}else{
$info_array = array(
"fields"=>"stateId,stateName,companyId",
"where"=>"stateStaus !='Inactive'"
);
$state_data = $this->GetRecords("state",$info_array);
if(count($state_data)>0) {
$companyId = $state_data['companyId'];
$check_info = array(
'fields'=>'companyName',
'where'=>'companyId = "'.$companyId.'" '
);
$companyName = $this->GetNameById('company',$check_info);
//$state_data['companyName'] = $companyName; //Company Name display
$response_array['status']='success';
$response_array['message']='Total '.count($state_data).' record(s) found.';
$response_array['total_record']= count($state_data);
$response_array['data']=$state_data;
$this->response($this->json($response_array), 200);
} else {
$response_array['status']='fail';
$response_array['message']='Record not found.';
$response_array['data']='';
$this->response($this->json($response_array), 506);
}
}
}
$this->response($this->json($response_array), 200); (i am getting response)
{
"status": "success",
"message": "Total 3 record(s) found.",
"total_record": 3,
"data": [
{
"stateId": "3",
"stateName": "Odisha",
"companyId": "1"
},
{
"stateId": "2",
"stateName": "Tamil Nadu",
"companyId": "1"
},
{
"stateId": "1",
"stateName": "Karnadaka",
"companyId": "1"
}
]
}
For finding companyName i wrote one function $companyName = $this->GetNameById('company',$check_info); here i am getting comapany name (Cibla).
My expected results
{
"status": "success",
"message": "Total 3 record(s) found.",
"total_record": 3,
"data": [
{
"stateId": "3",
"stateName": "Odisha",
"companyId": "1",
"companyName" : "Cibla"
},
{
"stateId": "2",
"stateName": "Tamil Nadu",
"companyId": "1",
"companyName" : "Cibla"
},
{
"stateId": "1",
"stateName": "Karnadaka",
"companyId": "1",
"companyName" : "Cibla"
}
]
}
Simple add your company name to division data array
$companyName = $this->GetNameById('company',$check_info);
$response_array['status']='success';
$response_array['message']='Total '.count($division_data).' record(s) found.';
$response_array['total_record']= count($division_data);
$division_data[0]['companyName'] = $companyName; //<--------------Add this line
$response_array['data']=$division_data;
$this->response($this->json($response_array), 200);
EDIT
As you have multiple division_data you need to loop over them to add companyName
change
$division_data[0]['companyName'] = $companyName;
to
foreach($division_data as $key=>$divisions)
{
$division_data[$key]['companyName'] = $companyName;
}
Related
I am trying to increment my Cloud Firestore using the REST API, however the documentation is unclear as to how this works. Here is my current implementation:-
I am trying to increment the integer value 'c' by 2, so if server value stored is 10 I want it to become 12 with this. Please help.
{
"writes": [
{
"currentDocument": {
"exists": true
}
},
{
"transform": {
"document": "projects/project_name/databases/(default)/documents/collection_name/user_id",
"fieldTransforms": [
{
"increment": {
"c": {
"integerValue": "2"
}
}
}
]
}
}
]
}
Error I keep getting:-
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"\": Root element must be a message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"\": Root element must be a message."
}
]
}
]
}
}
Edit: Here's the error after fixing my payload:
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"writes\" at 'document': Cannot find field.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "document",
"description": "Invalid JSON payload received. Unknown name \"writes\" at 'document': Cannot find field."
}
]
}
]
}
}
As per the FieldTransform documentation, you must set a fieldPath on your JSON representation. Here's the list of possible types for union field transform_type:
{
"fieldPath": string,
// Union field transform_type can be only one of the following:
"setToServerValue": enum (ServerValue),
"increment": {
object (Value)
},
"maximum": {
object (Value)
},
"minimum": {
object (Value)
},
"appendMissingElements": {
object (ArrayValue)
},
"removeAllFromArray": {
object (ArrayValue)
}
// End of list of possible types for union field transform_type.
}
You should remove the fieldPath which is c inside the increment:
"fieldTransforms": [
{
"increment": {
"c": {
"integerValue": "2"
}
}
}
]
and change it to this structure:
"fieldTransforms": [
{
"fieldPath": "<FieldName>",
"increment": {
"integerValue": "2"
}
}
]
And also, just want to note that currentDocument and transform should be both inside the objects of writes. For reference, here's the full JSON representation based on your given JSON representation above:
{
"writes": [
{
"currentDocument": {
"exists": true
},
"transform": {
"document": "projects/project_name/databases/(default)/documents/collection_name/user_id",
"fieldTransforms": [
{
"fieldPath": "c",
"increment": {
"integerValue": "2"
}
}
]
}
}
]
}
Here's the result by using the Postman:
For more information, you may want to check Firebase REST API: Write.
My JSON Data
{
"users": [
{
"userid": 1,
"username": "Admin",
"usermail": "admin#admin.com",
"authority": [
{
"authorityid": 1,
"authoritytext": "Admin",
"mission": [
{
"permission": "add"
},
{
"permission": "delete"
},
{
"permission": "move"
}
]
},
{
"authorityid": 1,
"authoritytext": "Super Admin",
"mission": [
{
"permission": "no add"
},
{
"permission": "no delete"
},
{
"permission": "only moderate"
}
]
}
]
}
]
}
MY PHP
foreach($result->users as $ok) {
foreach($ok->authority as $okey) {
foreach($okey->mission as $okeyokey) {
$test = $okeyokey->permission;
echo $test;
}
}
}
How to make this?
I want show parse json only authority{0} -> misson{0} show "permission" "add" please help me .
Maybe look ScreenShot >>>>>
I want filter {0}{1}{2} and select 0 -> show parse json
enter image description here
Is this what you are looking for?
<?php
$jsonData = '{
"users": [
{
"userid": 1,
"username": "Admin",
"usermail": "admin#admin.com",
"authority": [
{
"authorityid": 1,
"authoritytext": "Admin",
"mission": [
{
"permission": "add"
},
{
"permission": "delete"
},
{
"permission": "move"
}
]
},
{
"authorityid": 1,
"authoritytext": "Super Admin",
"mission": [
{
"permission": "no add"
},
{
"permission": "no delete"
},
{
"permission": "only moderate"
}
]
}
]
}
]
}';
$data = json_decode($jsonData);
echo($data->users[0]->authority[0]->mission[0]->permission);
You are getting objects and arrays confused
foreach($result->users as $currentUser){
$auths = $currentUser->authority;
foreach($auths as $currentAuth){
foreach($currentAuth->mission as $permission){
foreach($permission as $positionLabel => $permissionValue){
if($permissionValue == "add"){
// code here
}
}
}
}
}
Make sure you are using good labels. Dummy placeholders can be fine, but it makes tracking bugs really hard. I assume you want to check if they hold permission using a database, using PDO?
// array
$arr = ["this", "is", "an", "array"];
echo $arr[0] // prints out the word this.
$json = { "attribute": "value"}
echo $json->attribute // prints out the word value.
You can have arrays in JSON objects, and JSON Objects in arrays. How you access them are different.
I am trying to use geo_point for distance but it always shows location type double not geo_point how can I set location mapped to geo_point .
Actually I have to find all records within 5km sorted.
"pin" : {
"properties" : {
"location" : {
"properties" : {
"lat" : {
"type" : "double"
},
"lon" : {
"type" : "double"
}
}
},
"type" : {
"type" : "string"
}
}
},
and when I am trying searches with this query below to find result within 5km of delhi lat long :
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"pin": {
"distance": "5",
"distance_unit": "km",
"coordinate": {
"lat": 28.5402707,
"lon": 77.2289643
}
}
}
}
}
}
It shows me error query_parsing_exception and No query registered for [pin]. I cannot figure out the problem. It always throws this exception
{
"error": {
"root_cause": [
{
"type": "query_parsing_exception",
"reason": "No query registered for [pin]",
"index": "find_index",
"line": 1,
"col": 58
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "find_index",
"node": "DtpkEdLCSZCr8r2bTd8p5w",
"reason": {
"type": "query_parsing_exception",
"reason": "No query registered for [pin]",
"index": "find_index",
"line": 1,
"col": 58
}
}
]
},
"status": 400
}
Please help me to figure out this problem. how can I set geo_point and solve this exception error and status 400 and all_shards_failed error
You simply need to map your pin type like this, i.e. using the geo_point data type:
# 1. first delete your index
DELETE shouut_find_index
# 2. create a new one with the proper mapping
PUT shouut_find_index
{
"mappings": {
"search_shouut": {
"properties": {
"location": {
"type": "geo_point"
},
"type": {
"type": "string"
}
}
}
}
}
Then you can index a document like this
PUT shouut_find_index/search_shouut/1
{
"location": {
"lat": 28.5402707,
"lon": 77.2289643
},
"type": "dummy"
}
And finally your query can look like this
POST shouut_find_index/search_shouut/_search
{
"query": {
"filtered": {
"filter": {
"geo_distance": {
"distance": "5km",
"location": {
"lat": 28.5402707,
"lon": 77.2289643
}
}
}
}
}
}
I would know, why JSON encoded data (from MongoDB via PHP) presented as:
{
"53a6e02221360e263e000000": {
"_id": {
"$id": "53a6e02221360e263e000000"
},
"title": "1"
},
"53a6e02221360e263e000001": {
"_id": {
"$id": "53a6e02221360e263e000001"
},
"title": "2"
}
}
is bad, but:
[
{
"id": "53a6e02221360e263e000000",
"title": "1"
},
{
"id": "53a6e02221360e263e000001",
"title": "2"
}
]
is good?
Is any idea? Both valid JSON.
PHP code example:
// MongoDB init before it
$cursor = $mongo->db->collection->find();
// For not large data
$result = iterator_to_array( $cursor );
$json = json_encode($result);
var_dump($json);
I have a index called publications_items and type called "publication".
I'd like to have the 10 most recent added publications. (some matchAll principle)
I'm using ElasticSearch PHP (http://www.elasticsearch.org/guide/en/elasticsearch/client/php-api/current/_quickstart.html)
Basically i'm just doing a default get but i would not know how to do this in ElasticSearchPHP.
In sense.qbox.io i do :
POST /publications_items/_search { "query": {
"match_all": {} } }
and it works fine
mapping:
PUT /publications_items/ { "mappings": {
"publication": {
"properties": {
"title": {
"type": "string"
},
"url": {
"type": "string"
},
"description": {
"type": "string"
},
"year": {
"type": "integer"
},
"author": {
"type": "string"
}
}
} } }
You need to enable "_timestamp" mapping:
PUT /test/doc/_mapping
{
"_timestamp": {
"enabled": "true",
"store": "true"
}
}
And in your search query you need to sort by it and retrieve the first 10 documents:
GET /test/_search
{
"sort" : {
"_timestamp" : { "order" : "desc" }
},
"from" : 0, "size" : 10
}
And specifically in Elasticsearch PHP:
mappings change:
require 'vendor/autoload.php';
$client = new Elasticsearch\Client();
$params = array();
$params2 = [
'_timestamp' => [
'enabled' => 'true',
'store' => 'true'
]
];
$params['index']='test';
$params['type']='doc';
$params['body']['doc']=$params2;
$client->indices()->putMapping($params);
query:
require 'vendor/autoload.php';
$client = new Elasticsearch\Client();
$json = '{
"sort" : {
"_timestamp" : { "order" : "desc" }
},
"from" : 0, "size" : 10
}';
$params['index'] = 'test';
$params['type'] = 'doc';
$params['body'] = $json;
$results = $client->search($params);
echo json_encode($results, JSON_PRETTY_PRINT);