i am developing a web application in PHP with MongoDB as Database.
Currently I have the following Document in the MongoDB database.
{
_id : 1,
name:"Rahul",
STD:"10th",
marks:[
{
subject:"English",
mark_secured:"75",
Grade:"B+"
},
{
subject:"Science",
mark_secured:"84",
Grade:"A"
},
{
subject:"Mathematics",
mark_secured:"65",
Grade:"B"
}
]
},
{
_id : 2,
name:"James",
STD:"9th",
marks:[
{
subject:"English",
mark_secured:"83",
Grade:"A"
},
{
subject:"Science",
mark_secured:"94",
Grade:"A+"
},
{
subject:"Mathematics",
mark_secured:"78",
Grade:"B+"
}
]
}
Database name : Student
Collection name : stdMark
I have listed the names of the students and class as an hyperlink in a table. when i click on any student, i want to list the marks of the student in a table in the following format in another page.
||=======================================||
|| Subject | Mark Secured | Grade ||
||--------------|---------------|--------||
|| English | 83 | A ||
|| Science | 94 | A+ ||
|| Mathematics | 78 | B+ ||
||=======================================||
Please help me to fetch and display the sub array values and display it in table format in PHP.
Try this:
$id = $_GET['student_id']
$cursor = Student.stdMark.find({_id : $id})
$studentData = array();
foreach($cursor as $row){
foreach($row->marks as $item){
$result = array();
$result ['subject'] = $item->subject;
$result ['mark_secured'] = $item->mark_secured;
$result ['Grade'] = $item->Grade;
$studentData[] = $result;
}
}
echo json_encode($studentData);