am charts json from field to field - php

Part of the script says from field to field which is to do with this. Here's my JSON pulling info from my database, value 1 is amount, value 2 is time
[{
"date": "2014-12-09",
"value1": 367,
"value2": 03:06:00
}, {
"date": "2014-12-09",
"value1": 367,
"value2": 03:06:00
}]
That's my data, this is part of the script that wants me to map it? :S
dataSet.fieldMappings = [{
fromField: "valu1",
toField: "value2"
}, {
fromField: "volume",
toField: "volume"
}];
I only have 3 fields don't I? Sorry for a vague question, I just don't understand.

The fieldMapping maps the JSON-fields with the valueField specified in the graph-properties.
I guess what you want to do is something like this:
[{
"date": "2014-12-09 03:06:00",
"value": 367
}, {
"date": "2014-12-09 03:07:00",
"value": 673
}]
Note that i put the time into the date-field and changed the second date and value to be different from the first. (I think two similar datapoints won't work)
For this dataset your mapping should look like this:
dataSet.fieldMappings = [{
fromField: "value", // this is the JSON-field
toField: "valueMapped" // the graph will search for this field - see below
}];
To find the data, the graph needs to know the mapped field:
"graphs": [{
"valueField": "valueMapped"
}];
Now one last step for the correct use of the "date"-field:
"dataDateFormat": "YYYY-MM-DD JJ:NN:SS",
"categoryField": "date"

Related

How do I add a document to existing filled collection?

I am building an eShop for educational purposes and I need to handle the orders from a user. A user has a basket which is getting filled with products. If he decides buy another product I have insert a document into the existing collection of the card
Current MongoDB collection:
{
"_id": {
"$oid": "61f3d79c921000006000547d"
},
"username": "mike",
"products": {[
"number": "3",
"name": "Honduras",
"price": 7,
"stock": 10,
]},
"status": "UNPAID"
}
By adding another product, needs to be inserted in the existing collection in the field of products.
Expected to look like:
{
"_id": {
"$oid": "61f3d79c921000006000547d"
},
"username": "mike",
"products": {[
"number": "3",
"name": "Honduras",
"price": 7,
"stock": 10,
], [
"number": "4",
"name": "India",
"price": 10,
"stock": 11,
]},
"status": "UNPAID"
}
I am using PHP for the back end operations. The script that I wrote it is simple. I am searching if orders with user's username exist. If they exist then I lock the current order and make the operations needed.
I think that I am missing something in syntax of the update for the purpose described above:
PHP script:
if (isset($_POST['add'])){
// Ordered by name from URL
$username = $_GET['username'];
// Product info
$name = $_POST['add'];
// Finds the product selected from products
$product = $collection -> findOne(array("name" => "$name"));
// Serialize product to be added.
$json = MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($product));
// Searching for order from certain user
$collection = $db -> orders;
$exists = $collection -> findOne(array("username" => "$username"));
if (!is_null($exists)){
// The problem is here (maybe?)
$exists->updateOne(
array("products" => {}),array('$set'=>$json);
);
}
Any help and suggestions would be really appreciated!
Well, you need to use something like below
db.collection.update(
{find Condition},
{$push: {products : {key: value, key2: value 2}}
)
Here the catch is push. It adds an element to array. Here the element is an object.

I want a more consolidate json format

I am getting json array after getting applying query logic.
[
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-20"
},
{
"id": "3",
"diag_name": "LT Diagnostics",
"test_name": "CRP",
"booking_date": "2018-05-20"
},
{
"id": "4",
"diag_name": "Seepz Diagnostics",
"test_name": "Alk PO4",
"booking_date": "2018-05-21"
}
]
But i want a more justified json array written below.
[
{
"diag_name": "LT Diagnostics",
"test_name": [
{
"id": "3",
"name" : "Alk PO4"
},
{
"id": "3",
"name" : "CRP"
}
],
"booking_date": "2018-05-20"
},
{
"diag_name": "Seepz Diagnostics",
"test_name": [
{
"id": "4",
"name" : "Alk PO4"
}
],
"booking_date": "2018-05-21"
},
]
I am not getting it,How to do in php. I want a more consolidate json format.
Have you tried changing your SQL query to group by diag_name and booking_date? That would be the first step I’d employ to get the outer data.
Formatting the data in the nested manner you’re after could be a function of whatever record serializer you’re using — does it support nested JSON as a return type, or only flat JSON as your example return value shows?
If the record set -> JSON serializer only ever returns flat data, the comments above are correct that you will have to write your own formatter to change the shape of the JSON yourself...
The accepted answer of this other question may be of help:
Create multi-level JSON with PHP and MySQL
I'm not a PHP guy but this is a typical scenario to use functional programming by means of the monad Map.
Looking online I've found this article that could help you.
Changing datasource output is not always (seldom indeed) a viable option.
Enjoy coding

How to filter distinct data in CloudSearch:AWS?

I have a query in aws cloudsearch. I did the following things
1) Created domain
2) uploaded the data & created indexing
I have data fields like : user_id, user_name, user_details, etc
My objective is to get the grouped/distinct data of particular field & its total count. In Cloudsearch Group by / Distinct key words not supported. So, I went through the cloudsearch documentation & done it by adding facet.user_id={} in my query string.
But I need user_name field data along with user_id and count.** Please update me regarding this.
Here is my full query : ?q="Tamil Selvan"&facet.user_id={}
Here is my query result :
{
"status": {
"rid": "isTcmOYp+AEKhpbc",
"time-ms": 6
},
"hits": {
"found": 986,
"start": 0,
"hit": []
},
"facets": {
"user_id": {
"buckets": [{
"value": "5",
"count": 213
}, {
"value": "182",
"count": 197
}]
}
}
}
My expected result :
{
"status": {
"rid": "isTcmOYp+AEKhpbc",
"time-ms": 6
},
"hits": {
"found": 986,
"start": 0,
"hit": []
},
"facets": {
"user_id": {
"buckets": [{
"value": "5",
"user_name":"Tamil Selvan",
"count": 213
}, {
"value": "182",
"user_name":"Tamil Selvi",
"count": 197
}]
}
}
}
The proper solution would be to look up the user_names for the user_id facet values from your datastore (which CloudSearch is not, or at least should not be).
CloudSearch is a search solution; you shouldn't be trying to ask it which user_name belongs to some user_id, as that's a question for your data store.

Parsing JSON object changes order

For some odd reason, when I parse my JSON object being send via AJAX, it throws the object out of order.
$.post('get_notes', note_data, function(data){
var notes_obj = $.parseJSON(data);
});
When I console.log data, this is what is returned:
{"502":{"text":"First Response","user_name":"Admin","date":"11-12-2013 9:21"},
"509":{"text":"Second Response","user_name":"Admin","date":"11-12-2013 9:22"},
"508":{"text":"Third Response","user_name":"Admin","date":"11-12-2013 9:24"},
"504":{"text":"Fourth Response","user_name":"Admin","date":"11-12-2013 9:24"}}
This is the correct order. Notice the dates are properly ascending.
When I console.log notes_obj, this is what it returns:
502: Object
504: Object
508: Object
509: Object
For some reason, $.parseJSON() decided to re-order the output by id, and not by date which is what I need.
Any idea why this is happening?
The objects defined by JSON have no order to their properties at all, so it's perfectly acceptable for something to serialize the properties in any order.
The following are exactly identical objects in JSON:
{
"question": "Life, the Universe, and Everything",
"answer": 42
}
{
"answer": 42,
"question": "Life, the Universe, and Everything"
}
In the comments you asked
How do I fix it?
You stop relying on the order of something that is not defined to have any order. You could, for instance, reformat your response so it's an array, because arrays have order:
[
{ "key": "502", "text": "First Response", "user_name": "Admin", "date": "11-12-2013 9:21" },
{ "key": "504", "text": "Fourth Response", "user_name": "Admin", "date": "11-12-2013 9:24" },
{ "key": "508", "text": "Third Response", "user_name": "Admin", "date": "11-12-2013 9:24" },
{ "key": "509", "text": "Second Response", "user_name": "Admin", "date": "11-12-2013 9:22" }
]
Now instead of an object with various keys, you have an array of objects, where each object has a key property (and the other information).

bryntum component integration

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.

Categories