I am working on a page for teachers that shows results of students' performance on a test.
To display a graph I want to use Highcharts JavaScript library.
So far I have a PHP script using PDO to create JSON data that later can be fed to Highcharts from a different page.
My problem:
How do I group all data from the same student in an array? See the last example for what I wish to achieve. Also: I wish to enclose all data in an overarching JSON array.
I want this:
[{
"student": "Andreas",
"level" : [4, 3]
}, {
"student": "Eivind",
"level" : [4, 5]
}, {
"student": "Ole",
"level" : [4, 3]
}]
This is what my PHP looks like:
<?php
require("config.inc.php");
$school = $_GET["school"];
$class = $_GET["class"];
//initial query
$query = 'SELECT student, taskid, level FROM task
WHERE school=' . '"' . $school . '"' . ' AND class=' . '"' . $class . '" ORDER BY student';
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
if ($rows) {
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["student"] = $row["student"];
$post["level"] = $row["level"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response, JSON_NUMERIC_CHECK);
} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}
?>
This is what I get from the PHP:
{
"posts": [
{
"student": "Andreas",
"level": 4
},
{
"student": "Andreas",
"level": 3
},
{
"student": "Eivind",
"level": 4
},
{
"student": "Eivind",
"level": 5
},
{
"student": "Ole",
"level": 4
},
{
"student": "Ole",
"level": 3
}
]
}
var data=[
{ "category" : "Search Engines", "hits" : 5, "bytes" : 50189 },
{ "category" : "Content Server", "hits" : 1, "bytes" : 17308 },
{ "category" : "Content Server", "hits" : 1, "bytes" : 47412 },
{ "category" : "Search Engines", "hits" : 1, "bytes" : 7601 },
{ "category" : "Business", "hits" : 1, "bytes" : 2847 },
{ "category" : "Content Server", "hits" : 1, "bytes" : 24210 },
{ "category" : "Internet Services", "hits" : 1, "bytes" : 3690 },
{ "category" : "Search Engines", "hits" : 6, "bytes" : 613036 },
{ "category" : "Search Engines", "hits" : 1, "bytes" : 2858 }
];
var res = alasql('SELECT category, sum(hits) AS hits, sum(bytes) as bytes \
FROM ? \
GROUP BY category \
ORDER BY bytes DESC',[data]);
You will get output as:
[{"category":"Search Engines","hits":13,"bytes":673684},
{"category":"Content Server","hits":3,"bytes":88930},
{"category":"Internet Services","hits":1,"bytes":3690},
{"category":"Business","hits":1,"bytes":2847}]
You can construct an array like this pretty easily by using the student names as array keys. After you have built the array, you can use array_values to convert the string keys back to numeric keys.
...
if ($rows) {
foreach ($rows as $row) {
$posts[$row['student']]['student'] = $row['student'];
$posts[$row['student']]['level'][] = $row['level'];
}
$response = array_values($posts);
echo json_encode($response, JSON_NUMERIC_CHECK);
} else { ...
This should give you the following $response:
[
{
"student": "Andreas",
"level": [4, 3]
},
{
"student": "Eivind",
"level": [4, 5]
},
{
"student": "Ole",
"level": [4, 3]
}
]
In php you can do this:
In your foreach
foreach ($rows as $row) {
$post = array();
$post["student"] = $row["student"];
$post["level"] = $row["level"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
you need to do something like this:
<?php
$response = array();
$response["posts"] = array();
$students = array("student1", "student2", "student3", "student1");
$tempArray = array();
foreach ($students as $student) {
$lvl = 1;
if(!isset($tempArray[$student])){
$tempArray[$student] = array("name" => $student, "level" => array($lvl));
}
else{
$tempArray[$student]["level"][] = $lvl;
}
}
// add it to the array
$response["posts"] = $tempArray;
// encode array
$response = json_encode($response);
echo "<br><br><br> ";
// example
// decode and make it a array for easier looping.
$response = (array)json_decode($response);
$responsePosts = (array) $response["posts"];
// foreach post
foreach($response["posts"] as $keyP => $valueP){
// convert to array same as above
$valueP = (array) $valueP;
// key that is kinda useless but made it the student name so i can easily see if it already exists
echo "key :".$keyP." <br />";
// name of the student
echo "name :".$valueP["name"]." <br />";
// all the levels
echo "levels: ";
foreach($valueP["level"] as $lvl){
echo $lvl." ";
}
echo "<br /><br />";
}
?>
This should work. Atm i use the student name as a key to see if it already exists otherwise you need to loop through all the arrays and do a "name" === $student most likely.
No push is required.
Related
The existing code looks like this:
function getList(){
$sql = 'SELECT DISTINCT `person`.`person_name` as name, `skill`.`skill_name` as
skill,`skill_linker`.`skill_score`;
$result = $GLOBALS['conn']->query($sql);
if ($result->num_rows > 0) {
$emptyArray = array();
while($row = $result->fetch_assoc()) {
$emptyArray[] = $row;
}
echo json_encode($emptyArray);
}
}
JSON Output from above function:
[{
"name":"Bob Sun",
"skill":"Dancing",
"skill_score":"3"
},
{
"name":"Bob Sun",
"skill":"Surfing",
"skill_score":"2"
},
{
"name":"Bob Sun",
"skill":"Swimming",
"skill_score":"5"
},
{
"name":"Joe Steel",
"skill":"Eating",
"skill_score":"2"
},
{
"name":"Joe Steel",
"skill":"Cooking",
"skill_score":"3"
}]
Instead, I would like the JSON output to look something like this:
[
{
"name": "Bob Sun",
"skills" : [
"Dancing": 3,
"Surfing": 2,
"Swimming": 5
]
},
{
"name": "Joe Steel",
"skills" : [
"Eating": 2,
"Cooking": 3
]
}
]
Can this be done? How can I have it display skills:skill_score for each person if the data coming from the SQL result set is separate? How can I change the code to restructure the JSON data to have semi-categories?
Just transform the array in your while cycle.
while($row = $result->fetch_assoc()) {
$emptyArray[$row['name']]['name'] = $row['name'];
$emptyArray[$row['name']]['skills'][$row['skill']] = $row['skill_score'];
}
Welcome to StackOverflow.
insted of applying json_encode on the final array ,
apply it on row
concatinate all rows
inset final concatinated string into array
this may solve the issue
hello i have json file which contain like this data
{
"data": [
{
"SN": "210477",
"name": "jane",
"stdcode": "446515",
"total": 611
},
{
"SN": "210474",
"name": "JOHN doe",
"stdcode": "446512",
"total": 610
},
{
"SN": "210475",
"name": "smith doe",
"stdcode": "446513",
"total": 610
},
{
"SN": "210476",
"name": "omar",
"stdcode": "446514",
"total": 610
}
]
}
as you can see there is duplicate in total in the last three data in total which is 610
i want to order this data according to "total" so it be printed like this
1.jane
2.JOHN doe
2.smith doe
2.omar
i have this code that order it but this not what i want
<?php
$get_records = #file_get_contents('./result.json',true);
$decode_records = json_decode($get_records);
$data = $decode_records->data;
usort($data, function($a, $b) {
return $a->total > $b->total ? -1 : 1;
});
for($x=0;$x<=count($data);$x++){
echo $x+1 .".".$data[$x]->name;
}
?>
the code output
1.jane
2.JOHN doe
3.smith doe
4.omar
Check whether the total has changed, and don't increment the rank if not.
$rank = 0;
$last_total = null;
foreach ($data as $d) {
if ($d->total !== $last_total) {
$rank++;
$last_total = $d->total;
}
echo "$rank. {$data->name}<br>";
}
Note that this means that the next index after all the duplicates will be ranked #3. If you want it to jump to 5 you need to use the array index as well.
$last_total = null;
foreach ($data as $i => $d) {
if ($d->total !== $last_rank) {
$rank = $i+1;
$last_total = $d->total;
}
echo "$rank. {$data->name}<br>";
}
I am using slimphp v2 and I have the following function
function gt($user) {
$sql = "select id, id as categoryId, mobile, task from actions where status=0";
try {
$db = newDB($user);
$stmt = $db - > prepare($sql);
$stmt - > execute();
$gs = $stmt - > fetchAll(PDO::FETCH_OBJ);
if ($gs) {
$db = null;
echo json_encode($gs, JSON_UNESCAPED_UNICODE);
} else {
echo "Not Found";
}
} catch (PDOException $e) {
echo '{"error":{"text":'.$e - > getMessage().
'}}';
}
}
The default json output looks like:
[{
"id": "1",
"categoryId": "1",
"mobile": "111",
"task": "test"
},
{
"id": "2",
"categoryId": "2",
"mobile": "222",
"task": "test2"
}]
and the output that i am trying to make. I need to generate an ID: 1_(id) then format it like this
[{
id: "1",
task: "test",
}, {
ID: "1_1", //generate this, add 1_id
categoryId: "1",
mobile: "00000",
}, {
id: "2",
task: "test2",
}, {
ID: "1_2", //generate this, add 1_id
categoryId: "2",
mobile: "11111"
}];
Is it possible?
Thanks
I'm not sure if this is exactly what your after but you can gain the desired JSON output by converting your original JSON into an associative array and then restructure the data on each iteration using a Foreach() loop into a new assoc array. After that, you can just convert it back to JSON using json_encode().
Code:
$json = '[{
"id": "1",
"categoryId": "1",
"mobile": "111",
"task": "test"
},
{
"id": "2",
"categoryId": "2",
"mobile": "222",
"task": "test2"
}]';
$jsonArr = json_decode($json, TRUE);
$newArr = [];
foreach ($jsonArr as $v) {
$newArr[] = ['id:'=>$v['id'], 'task:' => $v['task']];
$newArr[] = ['ID:'=>'1_' . $v['id'], 'categoryId' => $v['categoryId'], 'mobile'=>$v['mobile']];
}
$newJson = json_encode($newArr);
var_dump($newJson);
Output:
[{
"id:": "1",
"task:": "test"
}, {
"ID:": "1_1",
"categoryId": "1",
"mobile": "111"
}, {
"id:": "2",
"task:": "test2"
}, {
"ID:": "1_2",
"categoryId": "2",
"mobile": "222"
}]
Edit -- Updated Answer
As discussed in comments, your outputting your SQL array as an object. I've set the Fetch to output as an associative array using PDO::FETCH_ASSOC and changed the foreach() loop to reference the assoc array $gs. This should work but if not then output your results for $gs again using var_dump($gs). You will still need to encode to JSON if needed but this line has been commented out.
function gt($user) {
$sql = "select id, id as categoryId, mobile, task from actions where status=0";
try {
$db = newDB($user);
$stmt = $db->prepare($sql);
$stmt->execute();
$gs = $stmt->fetchAll(PDO::FETCH_ASSOC); //Fetch as Associative Array
if ($gs) {
$db = null;
$newArr = [];
foreach ($gs as $v) { //$gs Array should still work with foreach loop
$newArr[] = ['id:'=>$v['id'], 'task:' => $v['task']];
$newArr[] = ['ID:'=>'1_' . $v['id'], 'categoryId' => $v['categoryId'], 'mobile'=>$v['mobile']];
}
//$newJson = json_encode($newArr); //JSON encode here if you want it converted to JSON.
} else {
echo "Not Found";
}
} catch(PDOException $e) {
//error_log($e->getMessage(), 3, '/var/tmp/php.log');
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
suppose I have an Array like this :
$myArray =
[
{
"id": 86,
"name": "admin/login"
},
{
"id": 87,
"name": "admin/logout"
},
{
"id": 88,
"name": "admin/desktop"
}
]
Each element of array has json format. and now I want to get name of element that have id of 87 for example.
Firstly How can I found that is there element with this id then get name property of that?
Decode JSON string into array. Then use Laravel's array_first method.
<?php
$myArray = '[{"id": 86,"name": "admin/login"},{"id": 87,"name": "admin/logout"},{"id": 88,"name": "admin/desktop"}]';
// Decode into array
$array = json_decode($myArray, true);
// Find item with correct id
$result = array_first($array, function($key, $value){
return $value['id'] === 87;
}, false);
if ($result) {
echo 'Item found, it\'s name is: '.$result['name'];
}
If you have id you like to find in variable, you have to use use construct.
// ID to search for
$searchID = 87;
// Find item with correct id
$result = array_first($array, function($key, $value) use($searchID){
return $value['id'] === $searchID;
}, false);
Try using this:
$newArray = json_decode($myArray);
and you'll get type of array
[
[
"id"=> 86,
"name"=> "admin/login"
],.....
........
]
Your $myArray is incorrect so, assuming it is a json string you can do this in the following way :
At first json_decode it into an arry.
Loop through the elements to find out you desired value(87).
Keep a flag which will tell if you have actually found any value.
<?php
$myArray =
'[
{
"id": 86,
"name": "admin/login"
},
{
"id": 87,
"name": "admin/logout"
},
{
"id": 88,
"name": "admin/desktop"
}
]';
$myArray = json_decode($myArray , true);
$found = 0;
foreach($myArray as $anArray)
{
if($anArray['id'] == 87)
{
var_dump($anArray['name']);
$found = 1;
}
}
if($found == 1)
{
var_dump("Element found.");
}
else
{
var_dump("Element not found. ");
}
?>
I'm attempting to output my JSON in a format just like this: http://api.androidhive.info/json/movies.json and I am unsure of how to accomplish this through my PHP output code. Right now, It is displaying with "post" at the top and is a map, which I don't know how to successfully remove, seen here: http://shipstudent.com/complaint_desk/androidfriendsList.php?username=noah. Please let me know if you need more information.
PHP:
$rows = $stmt->fetchAll();
if ($rows) {
$response["posts"] = array();
foreach ($rows as $row)
{
$post = array();
$post["username"] = $row["username"];
$post["profile_picture"] = $row["profile_picture"];
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else
{
die(json_encode($response));
}
JSON:
{
"posts": [
{
"username": "noah",
"profile_picture": "https://shipstudent.com/animal/appphotos/978321177.jpg"
},
{
"username": "e",
"profile_picture": "https://shipstudent.com/complaint_desk/appphotos/owl.jpeg"
},
]
}
Desired format:
[{
"title": "Dawn of the Planet of the Apes",
"image": "http://api.androidhive.info/json/movies/1.jpg",
"rating": 8.3,
"releaseYear": 2014,
"genre": ["Action", "Drama", "Sci-Fi"]
},
{
"title": "District 9",
"image": "http://api.androidhive.info/json/movies/2.jpg",
"rating": 8,
"releaseYear": 2009,
"genre": ["Action", "Sci-Fi", "Thriller"]
},
$rows = $stmt->fetchAll();
$response = [];
foreach ($rows as $row) {
$post = [
"username" => $row["username"],
"profile_picture" => $row["profile_picture"]
];
$response[] = $post;
}
echo json_encode($response);
$response = array();
foreach ($rows as $row)
{
$post = array();
$post["title"] => // define title;
$post["image"] => // define image;
$post["rating"] => // define rating;
$post["releaseYear"] => // define releaseYear;
$post["genre"] => // define genre, must be array();
array_push($response, $post);
}