在MongoDB中显示文档的子数组作为php中的表[关闭]
问题描述:
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.
i正在使用MongoDB作为数据库在PHP中开发Web应用程序。 p>
\ n
目前我在MongoDB数据库中有以下文档。 p>
{
_id:1,
name:“Rahul”,
STD:“10th”,
标记:[
{
subject:“ 英语“,
mark_secured:”75“,
等级:”B +“
},
{
subject:”Science“,
mark_secured:”84“,
等级:”A“
},
{
subject:“Mathematics”,
mark_secured:“65”,
等级:“B”
}
]
},
{
_id:2,
name :“James”,
STD:“9th”,
标记:[
{
subject:“English”,
mark_secured:“83”,
等级:“A”
},
{
subject:“Science”,
mark_secured:“94”,
等级:“A +”
},
{
subject:“Mathematics”,
mark_secured:“78”,
等级 :“B +”
}
]
}
code> pre>
数据库名称:学生
收藏名称:stdMark
我已将学生和班级的名字列为表格中的超链接。 当我点击任何学生时,我想在另一页中以下列格式列出学生在表格中的标记。 p>
|| ======= ================================ ||
|| 主题| Mark Secured | 等级||
|| -------------- | --------------- | -------- ||
|| 英语| 83 | A ||
|| 科学| 94 | A + ||
|| 数学| 78 | B + ||
|| ======================================= ||
code> pre>
请帮我提取并显示子数组值,并以PHP格式以表格格式显示。 p>
div>
答
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);