I have a data base named nominator which has some fields
it have a in field named 'noinatedUnder' how can i show the number of inS to that field
this is the Raw text of
SELECT FROM Nominator
{
"result": [
{
"#type": "d",
"#rid": "#73:2",
"#version": 2,
"#class": "Nominator",
"isTemp": "true",
"id": "JJRXW",
"Email": "testuser#test.com",
"Name": "nijeesh",
"Phone": "7894561234",
"school": "#65:2",
"in_noinatedUnder": [
"#161:1",
"#162:1",
"#163:1",
"#164:0",
"#165:0",
"#166:0",
"#167:0",
"#168:0",
"#161:0",
"#162:0",
"#163:0"
],
"#fieldTypes": "school=x,in_noinatedUnder=g"
},
{
"#type": "d",
"#rid": "#74:0",
"#version": 1,
"#class": "Nominator",
"isTemp": "true",
"id": "SU7SV",
"Email": "npms#school.com",
"Name": "pon muthu",
"Phone": "7778455215",
"school": "#65:2",
"#fieldTypes": "school=x"
},
{
"#type": "d",
"#rid": "#75:1",
"#version": 1,
"#class": "Nominator",
"isTemp": "true",
"id": "4DZ86",
"Email": "sivaraj#testschool.com",
"Name": "sivaraj",
"Phone": "7788899445",
"school": "#65:2",
"#fieldTypes": "school=x"
},
{
"#type": "d",
"#rid": "#76:1",
"#version": 1,
"#class": "Nominator",
"isTemp": "true",
"id": "HFQJ1",
"Email": "klndk#knvd.com",
"Name": "dbhbsd",
"Phone": "8656548745",
"school": "#65:2",
"#fieldTypes": "school=x"
}
],
"notification": "Query executed in 6.12 sec. Returned 4 record(s)"
}
in this on the first entry there is 11 entrys in the in->nominated under area and 0 on everything else how to select this number using sql for each field
that is it should print
-----------
count | ----------
11
0
0
0
i did got the result using php
$query = "select * from `Nominator`";
$result = runquery($query);
$a = array();
for($i=0;$i<sizeof($result);$i++)
{
echo '<p>';
echo isset($result[$i]->oData->in_noinatedUnder)?sizeof($result[$i]->oData->in_noinatedUnder):0;
echo '</p>';
}
function runquery($query)
{
$client = new PhpOrient();
$client->hostname = 'localhost';
$client->port = 2424;
$client->username = 'root';
$client->password = 'hello';
$client->connect();
$client->dbOpen('tabe');
$result = $client->query($query);
$json = json_decode(json_encode($result));
if (sizeof($json) > 0) {
return $json;
} else {
return false;
}
}
So is there any way to get the count directly from sql it self, like select count(*) from nominator gives count of nominators
To get the number of vertexes in in try this query:
select in().size() from <class-name>
Hope it helps.
Regards
Related
I have this json code
"result": [
{
"update_id": 74783732,
"message": {
"message_id": 852,
"from": {
"id": ---,
"is_bot": false,
"first_name": "---",
"username": "---",
"language_code": "en"
},
"chat": {
"id": ---,
"first_name": "---",
"username": "---",
"type": "private"
},
"date": 1646306224,
"text": "#username",
"entities": [
{
"offset": 0,
"length": 16,
"type": "mention"
}
]
}
}
]
I can get content from update_id , message, first_name etc.
but I want to get "mention" from type how can i do it?
my code is
here i decode json and get arrays from json and put them in variable and use it in my queries but I cant get mention from entities...
$update = file_get_contents("php://input");
$update_array = json_decode($update, true);
if( isset($update_array["message"]) )
{
$text = $update_array["message"]["text"];
$chat_id = $update_array["message"]["chat"]["id"];
}
if(isset($update_array["message"]["entities"]["type"]) and $update_array=="mention")
{
$get_username = $text;
show_users_by_username($get_username);
}
tnx for helping
$update_array will never be equal to "mention" but $update_array["message"]["entities"]["type"] yes.
Change your condition.
Essentially I have the following script with the following response:
<?php
header('Content-Type: application/json');
$stmt = $pdo->prepare('
SELECT
`tablelist`.`id`,
`tablelist`.`content`
FROM `tablelist `
');
$stmt->execute([
]);
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
$rowcount = $stmt->rowCount();
if ($rowcount < 1) {
$response["error"] = TRUE;
echo json_encode($response);
}else{
echo json_encode($row);
}
?>
Current Response:
[
{
"id": 1,
"content": "Reason 1"
},
{
"id": 2,
"content": "Reason 2"
},
{
"id": 3,
"content": "Reason 3"
},
{
"id": 4,
"content": "Reason 4"
}
]
I would instead like to present this array as the following (no square brackets):
{
"error": false,
"content": {
"1": "Reason 1",
"2": "Reason 2",
"3": "Reason 3",
"4": "Reason 4"
}
I know I need to do the following:
$response["error"] = FALSE;
$response["content"][$row[id]] = $row[content];
But using this method I am not getting any values from the array.
How can present the values from the array the way?
Look at array_column function and Example #2 at https://www.php.net/manual/en/function.array-column.php
You can transform your rows like this but you must be aware of the uniqueness
$content = '[
{
"id": 1,
"content": "Reason 1"
},
{
"id": 2,
"content": "Reason 2"
},
{
"id": 3,
"content": "Reason 3"
},
{
"id": 4,
"content": "Reason 4"
}
]';
$from = json_decode($content, true);
$to = json_encode(array_column($from, 'content', 'id'), JSON_PRETTY_PRINT);
echo $to;
will result to
{
"1": "Reason 1",
"2": "Reason 2",
"3": "Reason 3",
"4": "Reason 4"
}
via https://3v4l.org/WL29a
You can do it very easily with PDO using the PDO::FETCH_KEY_PAIR fetch mode. It will use the first column from SQL as a key and the second as the value.
<?php
header('Content-Type: application/json');
$stmt = $pdo->prepare('
SELECT
`tablelist`.`id`,
`tablelist`.`content`
FROM `tablelist `
');
$stmt->execute();
$dataFromDB = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
if (!$dataFromDB) {
echo json_encode(['error' => true]);
} else {
echo json_encode(['error' => false, 'content' => $dataFromDB]);
}
This will produce an output similar to this if there were any rows returned from the SELECT:
{
"error": false,
"content": {
"1": "Reason 1",
"2": "Reason 2",
"3": "Reason 3",
"4": "Reason 4"
}
}
I'm trying to parse data returned by the Jotform API.
I'm successfully echoing the data but I'm also getting unnecessary additional lines of text.
My PHP code is:
include "JotForm.php";
$jotformAPI = new JotForm("myapikey");
$submissions = $jotformAPI->getFormSubmissions("myformid");
var_dump($submissions );
foreach ($submissions as $data) {
$detail = $jotformAPI->getSubmission($data['id']);
foreach ($detail as $d) {
echo $d[1]['answer']['first'] . '<br>';
}
}
result of var_dump($submissions );
{
"responseCode": 200,
"message": "success",
"content": [{
"id": "237955080346633702",
"form_id": "31751954731962",
"ip": "123.123.123.123",
"created_at": "2013-06-25 03:38:00",
"updated_at": "2013-06-27 04:58:00",
"status": "ACTIVE",
"new": "1",
"answers": {
"1": {
"text": "Name",
"type":"control_fullname",
"answer": {
"first": "LeBron",
"last": "James"
},
"prettyFormat": "LeBron James"
},
"2": {
"text": "Your Message",
"type": "control_textarea",
"answer":"¡Ay, caramba!"
}
}],
}
And here is the result I'm getting:
1
0
0
0
C
0
LeBron
I had to repair your invalid json string...
Code: (Demo)
$json = '{
"responseCode": 200,
"message": "success",
"content": [{
"id": "237955080346633702",
"form_id": "31751954731962",
"ip": "123.123.123.123",
"created_at": "2013-06-25 03:38:00",
"updated_at": "2013-06-27 04:58:00",
"status": "ACTIVE",
"new": "1",
"answers": {
"1": {
"text": "Name",
"type":"control_fullname",
"answer": {
"first": "LeBron",
"last": "James"
},
"prettyFormat": "LeBron James"
},
"2": {
"text": "Your Message",
"type": "control_textarea",
"answer":"¡Ay, caramba!"
}
}}]
}';
foreach (json_decode($json, true)['content'] as $set) {
echo "{$set['answers'][1]['answer']['first']} {$set['answers'][1]['answer']['last']}\n";
}
Output:
LeBron James
I'm a little fuzzy on the action of the jot functions, but maybe it's supposed to be like this:
foreach ($submissions as $data) {
$detail = $jotformAPI->getSubmission($data['id']);
echo $detail['answers'][1]['answer']['first'] . '<br>';
}
I have following JSON file:
[{
"id": 9,
"title": "Birthday",
"description": "Debut Party",
"start": "2017-07-25T15:01",
"end": "2017-08-03T00:01",
"url": "https:\/\/bd.com"
}, {
"id": 11,
"title": "Anniversary",
"description": "First Ann",
"start": "2017-06-28T15:00",
"end": "2017-07-08T02:58",
"url": "https:\/\/anniversary.com\/"
}, {
"id": 5,
"title": "Anniversary",
"description": "First Ann",
"start": "2017-06-28T15:00",
"end": "2017-07-08T02:58",
"url": "https:\/\/anniversary.com\/"
}]
I want them to be sorted by id like 5,9,11 using PHP. How Can I do that?
First decode the JSON
$array = json_decode($json,true);
and then use usort to sort by id or something else.
function sortById($a, $b) {
return $b->id - $a->id;
}
usort($array,"sortById");
SELECT * FROM test ORDER BY `id` DESC;
use this query in you php file.
example :
function getPosts()
{
$connection = mysqli_connect("localhost", "root", "", DatabaseManager::DATABASE_NAME);
$sqlQuery = "SELECT * FROM posts order by 'id' desc;";
$result = $connection->query($sqlQuery);
$postsArray = array();
if ($result->num_rows > 0) {
for ($i = 0; $i < $result->num_rows; $i++) {
$postsArray[$i] = $result->fetch_assoc();
}
}
echo json_encode($postsArray);
}
I currently have the following table set up:
StartTime EndTime Performer Event Day Location
-----------------------------------------------------
1:00pm 2:00pm Test Test 0 1
11:00pm 12:00am Test Test 0 0
2:00pm 2:30pm Test Test 1 0
11:00pm 12:00am Test Test 2 1
The JSON output looks something like this:
{
"day0": {
"item1": {
"StartTime": "1:00pm",
"EndTime": "2:00pm",
"Performer": "Test",
"Event": "Test",
"Location": 1
},
"item2": {
"StartTime": "11:00pm",
"EndTime": "12:00am",
"Performer": "Test",
"Event": "Test",
"Location": 0
}
},
"day1": {
"item1": {
"StartTime": "2:00pm",
"EndTime": "2:30pm",
"Performer": "Test",
"Event": "Test",
"Location": 0
}
},
"day2": {
"item1": {
"StartTime": "11:00pm",
"EndTime": "12:00am",
"Performer": "Test",
"Event": "Test",
"Location": 1
}
}
}
Since I'm still learning PHP, I wrote some sloppy code by making 3 queries to the database, each time selecting all data where the day was 1, 2, and 3.
Here's an example of code for fetching data for day=0, which is repeated for day=1 and day=2:
echo '{ "day0" : {';
$sql = "select * from table WHERE day = 0";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$jsonData = array();
$rowCount = $result->num_rows;
$index = 1;
while($row =mysqli_fetch_assoc($result))
{
echo '"item'.$index.'":';
echo json_encode(array("StartTime" => $row['StartTime'], "EndTime" => $row['EndTime'], "Performer" => $row['Performer'], "Event" => $row['Event'], "Location" => intval($row['Location'])));
if ($rowCount != $index)
{
echo ',';
}
++$index;
}
echo ' }';
// Repeated code for day=1
// Repeated code for day=2
echo ' }';
I feel as though this can be achieved with just one query, but being that I'm new, I'm not sure how to implement it.
I started to do something like this:
$sql = "select * from table";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
$jsonData = array();
$numOfRows = $result->num_rows;
$count = 1;
while($row = mysqli_fetch_assoc($result))
{
$outerIndex = 'day'.$row['day'];
if ($row['day'] == '1')
{
// Do something, not sure
}
if ( !isset( $jsonData[$outerIndex] ) )
{
$innerIndex = 'item'.$count.'';
$jsonData[$outerIndex][$innerIndex] = $row;
}
++$count;
}
echo json_encode($jsonData);
However, I just got stuck, and not really sure how to approach it further.
SQL:
$sql = "SELECT * FROM table ORDER BY Day";
Further down on code:
$result_object = [];
$item = 1;
while ($row = $result->fetch_assoc()) {
if(isset($result_object['day'.$row['Day']]))
{
$result_object['day'.$row['Day']]['item'.$item] = $row;
$item++;
}
else
{
$result_object['day'.$row['Day']]['item1'] = $row;
$item = 2;
}
}
You can then output it with:
echo json_encode($result_object, JSON_PRETTY_PRINT); //JSON_PRETTTY_PRINT is not necessary...
You may disagree with me, but I don't think indexing items with item0, item1 and so on.... or day0, day1 ... is a GOOD idea. I personally prefer that the looping through the result would be:
while ($row = $result->fetch_assoc()) {
if(isset($result_object[$row['Day']]))
{
$result_object[$row['Day']]->items[] = $row;
}
else
{
$result_object[$row['Day']] = (object)['day'=>$row['Day'], 'items'=>[$row]];
}
}
In this case, the result would be an array of objects. ie:
[
{
"day": "0",
"items": [
{
"StartTime": "07:23:56",
"EndTime": "17:24:04",
"Performer": "Performer1",
"Event": "Event1",
"Day": "0",
"Location": "1"
},
{
"StartTime": "09:24:30",
"EndTime": "01:04:37",
"Performer": "Performer2",
"Event": "Event2",
"Day": "0",
"Location": "1"
}
]
},
{
"day": "1",
"items": [
{
"StartTime": "10:25:22",
"EndTime": "11:25:29",
"Performer": "Performer2",
"Event": "Event3",
"Day": "1",
"Location": "2"
}
]
},
{
"day": "2",
"items": [
{
"StartTime": "12:26:08",
"EndTime": "13:26:12",
"Performer": "Performer3",
"Event": "Event4",
"Day": "2",
"Location": "1"
}
]
}
]
The reason: you can easily iterate through each values(an array) in whatever language you're going to use.