-1
<?php
$link = mysqli_connect("localhost", "root", "", "college");
$query = "SELECT branch, year,semester, COUNT(*) count FROM students GROUP BY branch,year,semester;";
$result = mysqli_query($link,$query);
$rows= mysqli_fetch_all($result,MYSQLI_ASSOC);
echo json_encode($rows);

?>

this is my output:

[{"branch":"cse","year":"1","semester":"1","count":"4"},{"branch":"cse","year":"2","semester":"1","count":"1"},{"branch":"cse","year":"2","semester":"2","count":"2"},{"branch":"cse","year":"2","semester":"4","count":"1"},{"branch":"cse","year":"3","semester":"2","count":"1"},{"branch":"cse","year":"3","semester":"3","count":"2"},{"branch":"cse","year":"4","semester":"3","count":"1"},{"branch":"cse","year":"4","semester":"4","count":"2"}, {"branch":"ece","year":"1","semester":"4","count":"2"},{"branch":"ece","year":"2","semester":"2","count":"2"},{"branch":"ece","year":"2","semester":"3","count":"1"},{"branch":"ece","year":"3","semester":"1","count":"1"},{"branch":"ece","year":"3","semester":"3","count":"2"},{"branch":"ece","year":"4","semester":"2","count":"2"},{"branch":"ece","year":"4","semester":"4","count":"2"}, {"branch":"eee","year":"1","semester":"1","count":"1"},{"branch":"eee","year":"1","semester":"4","count":"1"},{"branch":"eee","year":"2","semester":"1","count":"1"},{"branch":"eee","year":"2","semester":"2","count":"3"},{"branch":"eee","year":"2","semester":"3","count":"2"},{"branch":"eee","year":"2","semester":"4","count":"1"},{"branch":"eee","year":"3","semester":"1","count":"1"},{"branch":"eee","year":"3","semester":"2","count":"1"},{"branch":"eee","year":"3","semester":"3","count":"4"},{"branch":"eee","year":"3","semester":"4","count":"3"},{"branch":"eee","year":"4","semester":"2","count":"3"},{"branch":"eee","year":"4","semester":"3","count":"2"},{"branch":"eee","year":"4","semester":"4","count":"1"}]

but i want output like this:

{"cse":

[{"year":"1","semester":"1","count":"4"},
{"year":"2","semester":"1","count":"1"},
{"year":"2","semester":"2","count":"2"},
{"year":"2","semester":"4","count":"1"},
{"year":"3","semester":"2","count":"1"},
{"year":"3","semester":"3","count":"2"},
{"year":"4","semester":"3","count":"1"},
{"year":"4","semester":"4","count":"2"}],

"ece":

[{"year":"1","semester":"4","count":"2"},
{"year":"2","semester":"2","count":"2"},
{"year":"2","semester":"3","count":"1"},
{"year":"3","semester":"1","count":"1"},
{"year":"3","semester":"3","count":"2"},
{"year":"4","semester":"2","count":"2"},
{"year":"4","semester":"4","count":"2"}],

"eee":

[{"year":"1","semester":"1","count":"1"},
{"year":"1","semester":"4","count":"1"},
{"year":"2","semester":"1","count":"1"},
{"year":"2","semester":"2","count":"3"},
{"year":"2","semester":"3","count":"2"},
{"year":"2","semester":"4","count":"1"},
{"year":"3","semester":"1","count":"1"},
{"year":"3","semester":"3","count":"4"},
{"year":"3","semester":"4","count":"3"},
{"year":"4","semester":"2","count":"3"},
{"year":"4","semester":"3","count":"2"},
{"year":"4","semester":"4","count":"1"}]}

suggest any other ways to display above output

1

1 Answer 1

1

Rather than do a mysqli_fetch_all, it may be easier (and might be quicker) to fetch each row at a time and put it into the correct format in one operation...

$result = mysqli_query($link,$query);
$output = [];
while ( $rows= mysqli_fetch_assoc($result)) {
    $output [$rows['branch']][] = ["year" => $rows['year'], 
                   "semester" => $rows['semester'], 
                   "count" => $rows['count']];
}
echo json_encode($output);
Sign up to request clarification or add additional context in comments.

4 Comments

thanks...its working bro...is there any way display output in more attractive way in json format?
You can use json_encode($output,JSON_PRETTY_PRINT)
no difference with json_encode($output,JSON_PRETTY_PRINT)
@MaheshRamineedi please define "attractive" then...what do you mean by that exactly? What are you wanting to see? JSON is just data for transmission or storage, it's not really meant for displaying nicely, so beyond the "pretty print" setting (and that might not have much effect, depending exactly what client you're using to receive the data) there aren't really any options.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.