我本质上是在尝试执行两个查询,一个是获取给定程序(或专业)的要求,另一个是获得学生已经完成的课程。
我想互相检查返回的字符串,如果用户参加了必修课,请在返回的要求旁边放一个"复选框"。如果他们没有参加必修课,请在要求旁边放一个空盒子。
我几乎已经达到了复选框概念的作用,然而,当我使用两个while循环时,它会重复返回的需求(课程)。
当我使用一个while循环时,它会获取第二个查询的数据并返回所有要求,但在找到第一个匹配项后就停止检查(例如,不继续查看课程2、3或4是否已经完成,等等)。
我还尝试了strcmp函数来检查$studentobject与$programsubject(programnumber和studentnumber同上)。
如果有人能提供如何实现这一点的煽动,或提供一种替代方法,我将不胜感激。如果需要,我可以提供更多细节。
<table class="table">
<tr>
<th> </th>
<th>Class</th>
<th>Term</th>
<th>Credits</th>
</tr>
<?php
$q = $db->query("SELECT * FROM `program_courses` a, `programs` b, `user_details` c WHERE a.pid = b.pid AND c.uid = '".$_GET['id']."' AND c.major = b.major");
while($program = $q->fetch()) {
$w = $db->query("SELECT * FROM `user_courses` WHERE uid = '".$_GET['id']."'");
$student = $w->fetch(); { // have also tried using a while loop here
$programsubject=$program['subject'];
$programnumber=$program['number'];
$studentsubject=$student['subject'];
$studentnumber=$student['number'];
?>
<?php
if ($studentsubject==$programsubject && $studentnumber==$programnumber) {
$checkbox = 'src="http://www.clipartbest.com/cliparts/ncX/jL6/ncXjL6rcB.png" width="25px" height="25px"';
} elseif ($studentsubject!=$programsubject || $studentnumber!=$programnumber) {
$checkbox = 'src="http://www.clker.com/cliparts/e/q/p/N/s/G/checkbox-unchecked.svg" width="25px" height="25px"';
}
?>
<?php
//check off the requirement if the student has completed the course
echo '
<tr style="background-color:#E9FFD2">
<td> <img '.$checkbox.'> </td>
<td>'.$programsubject.' '.$programnumber.'</td>
<td> </td>
<td>3</td>
</tr>';
?>
<?php
//End our conditionals
}
}
?>
</table>
编辑:提供了包括这些相应表格的表格结构在内的其他信息。
我希望避免更改两个相应的查询,但下面是每个查询返回的内容。我想将数据分开的原因是为了在未来引入更复杂的条件(例如,分数最低的课程可以计算在内,或者只有学分精确的课程才能计算在内,等等)。
对于用户1001,以下内容将转储。。。
SELECT *
FROM `program_courses` a, `programs` b, `user_details` c
WHERE a.pid = b.pid AND c.uid = '1001' AND c.major = b.major
id, pid, subject, number, credits, pid, major, title, degree, college, catalog, credits_req, id, uid, major, college, degree, catalog_year, credits, gpa, academic_standing, advisor, holds, id
'3','1','IDT','600','3','1','4574','Instructional Design & Technology','PHD','45','201408','72','1','1001','4574','45','Doctor of Philosophy','201408','52','3.44','Good Standing','Terence A','01'
'4','1','IDT','610','3','1','4574','Instructional Design & Technology','PHD','45','201408','72','1','1001','4574','45','Doctor of Philosophy','201408','52','3.44','Good Standing','Terence A','01'
'5','1','IDT','693J','3','1','4574','Instructional Design & Technology','PHD','45','201408','72','1','1001','4574','45','Doctor of Philosophy','201408','52','3.44','Good Standing','Terence A','01'
'6','1','IDT','750','3','1','4574','Instructional Design & Technology','PHD','45','201408','72','1','1001','4574','45','Doctor of Philosophy','201408','52','3.44','Good Standing','Terence A','01'
'7','1','IDT','790','3','1','4574','Instructional Design & Technology','PHD','45','201408','72','1','1001','4574','45','Doctor of Philosophy','201408','52','3.44','Good Standing','Terence A','01'
'8','1','IDT','691','3','1','4574','Instructional Design & Technology','PHD','45','201408','72','1','1001','4574','45','Doctor of Philosophy','201408','52','3.44','Good Standing','Terence A','01'
'21','1','IDT','931','3','1','4574','Instructional Design & Technology','PHD','45','201408','72','1','1001','4574','45','Doctor of Philosophy','201408','52','3.44','Good Standing','Terence A','01'
'22','1','IDT','660','3','1','4574','Instructional Design & Technology','PHD','45','201408','72','1','1001','4574','45','Doctor of Philosophy','201408','52','3.44','Good Standing','Terence A','01'
对于具有..的同一用户。。
SELECT * FROM `user_courses` WHERE id = '1001'
id, cid, uid, subject, number, credits, enroll_status, id, id
'1', '1', '1001', 'IDT', '610', '3', 'complete'
'3', '86903', '1001', 'IDT', '750', '3', 'complete'
因此,根据我的基本逻辑——只有当user_courses和program_courses表之间的subjectname和number匹配时,IDT 610和IDT 750才需要有一个完整的复选框,而所有其他程序需求都需要一个空的复选框。这有道理吗?到目前为止,我真的很感谢所有的反馈,因为在最佳实践方面,我不是一个大师。如果提供具有上述结构
使用您的表结构编辑:
SELECT a.subject, a.number, a.credits as course_credits,
b.major, b.title, b.degree,
c.catalog_year,
d.credits as credits_earned
FROM program_courses a
join programs b on a.pid = b.pid
join user_details c on c.major = b.major and c.uid = '".$_GET['id']."'
left join user_courses d on a.pid = d.cid -- not sure that cid from table d is a match to pid in table a, adjust this as appropriate
subject|number|course_credits| major|title|degree|catalog_year|credits_earned -------|------|--------------|------|-----|------|------------|-------------- IDT |600 |3 |Doctor|ID&T |PHD |2015 |{NULL} IDT |610 |3 |Doctor|ID&T |PHD |2015 |2.75
-- Courses that the student has taken will have a value for credits_earned and you can base your display logic off of that
然后按照你的逻辑
if($results['credits_earned'] > 1.5){ // or whatever threshhold is considered passing
// Yes, this student has completed this course
}else{
// Has not completed
}
我在从第一个查询获取循环的中间执行第二个查询时遇到了问题。如果你真的不想把这两个查询合并为一个,那么也许你可以这样重新排列你的逻辑: // store info on all the courses this student has already taken
$completed_courses = array();
$w = $db->query("SELECT * FROM //循环浏览所有必修课程$q=$db->query("SELECT*FROM } ?>
user_courses
WHERE uid='".$_GET['id']."');while($student=$w->fetch()){$completed_curses[]=$student["主题"]$学生[人数];}program_courses
a,programs
b,user_details
c WHERE a.pid=b.pid AND c.uid='".$_GET['id']."'AND c.major=b.major");while($program=$q->fetch()){if(in_array($program['subject'].$program['number'], $completed_courses)){
// Yes the student has completed this course
}else{
// no, the student has not completed this course
}
请参阅上面Drew的回复。通过在一开始将所学课程放入一个数组中,然后循环通过所需课程(与我一直在尝试的相反),while循环没有问题,事实上这就像一种魅力。谢谢你,先生。德鲁,我很感激你的精彩煽动和最终的解决方案。如果我有更多的声誉,我会投赞成票!