Home >Backend Development >PHP Tutorial >两个有关联的表的循环有关问题

两个有关联的表的循环有关问题

WBOY
WBOYOriginal
2016-06-13 10:15:39793browse

两个有关联的表的循环问题
我现在有两个数据库表
课程表subject
id subjectname
1 语文
2 数学
3 英语
班级所选课程表classsubject
id classid(班级表id) subjectid(课程表id)
1 1 1
2 1 2
3 1 1
我需要实现页面先以复选框的形式显示出课程表的的所有课程
然后,根据班级id,查询出相对应的课程id,相应的课程复选框就成为选中的状态

如果班级id为1
则页面显示如下

HTML code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><input type="checkbox" name="subject[]" value="1" checked>语文<input type="checkbox" name="subject[]" value="2" checked>数学<input type="checkbox" name="subject[]" value="3">英语


应该怎么实现,前辈们给看看啊



------解决方案--------------------
SQL code
SELECT s.*, c.subjectid FROM subject AS s LEFT JOIN classsubject AS c ON s.id=c.subjectid AND c.classid=1<br><font color="#e78608">------解决方案--------------------</font><br>用1楼查询找出班级所有已选课程数组,然后查询出所有课程数组,2个foreach循环嵌套(可能还有其他更好的办法)就能给所有课程数组标记出哪些是已选的,然后循环输出,已选标记的checked就可以了。<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn