PHP在一个查询中检查值与另一个查询

时间:2015-03-06 18:41:33

标签: php mysql while-loop multiple-tables strcmp

我实际上是在尝试执行两个查询,一个用于获取给定程序(或主要)的要求,另一个用于获取学生已完成的课程。

我想要相互检查返回的字符串,如果用户已经采取了必修课程,请放置一个"复选框"在返回的要求旁边。如果他们没有采取必要的课程,请在要求旁边放置一个空盒子。

我几乎可以在那里使用复选框概念,但是当我使用两个while循环时,它会复制返回的需求(课程)。

当我使用while循环时,它会在第二个查询中获取数据并返回所有要求,但只是在找到第一个匹配后停止检查(例如,不会继续查看课程2或3或4是否已经过完成等)。

我还尝试过strcmp函数来检查$ studentsubject对$ programsubject(同上programnumber和studentnumber)。

如果有人可以提供如何使其工作的煽动,或提供替代方法,我将不胜感激。如果需要,我可以提供更多细节。

         <table class="table">
    <tr>
            <th>&nbsp;</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>&nbsp;</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表之间的主题名和数字匹配时,IDT 610和IDT 750才需要具有完整的复选框,并且所有其他程序要求都需要一个空的复选框。这有意义吗?到目前为止,我非常感谢所有的反馈,因为在最佳实践方面,我不是一个大师。如果适当的JOIN版本与上述结构一起呈现,我愿意重新审视查询

2 个答案:

答案 0 :(得分:3)

使用您的表格结构编辑: 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 }

我遇到了在第一个查询的fetch循环中进行第二次查询的问题。如果你真的不想将两个查询合并为一个,那么也许你可以重新安排你的逻辑:

// store info on all the courses this student has already taken $completed_courses = array(); $w = $db->query("SELECT * FROM user_courses WHERE uid = '".$_GET['id']."'"); while($student = $w->fetch()){ $completed_courses[]=$student['subject'].$student['number']; }

user_courses

// loop through all required courses $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()) {

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
}

}

答案 1 :(得分:0)

请参阅上面的Drew回复。通过将开始的课程放入一个数组中,然后循环完成所需的课程(与我一直在尝试的相反),while循环没有问题,这实际上就像一个魅力。谢谢你先生。德鲁,我很欣赏你提供的精彩煽动和最终解决方案。如果我有更多的声誉,我会举行投票!