【问题标题】:PHP Checking Values in One Query Against AnotherPHP 检查一个查询中的值与另一个查询
【发布时间】:2015-03-06 18:41:32
【问题描述】:

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

我想将返回的字符串相互检查,如果用户已经学习了必修课程,请在返回的要求旁边放置一个“复选框”。如果他们没有参加必修课程,请在要求旁边放置一个空框。

我已经差不多到了复选框概念起作用的地方,但是当我使用两个 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 版本,我愿意重新访问查询

【问题讨论】:

  • 为什么不进行JOIN 查询?
  • 我同意@JayBlanchard——但从技术上讲,OP 使用的是隐式连接......我认为查询可以改进。
  • 你能用表定义或结构更新你的问题吗?也许是几行样本数据?
  • 另外,在 mysql 中运行查询并复制/粘贴结果以更新您的问题。 (不必放置所有数据,只需了解您的结果集是什么样子即可)
  • 也许您应该在 while 之外执行第二个查询以提高性能... =)

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


【解决方案1】:

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

主题|数量|课程学分|专业|职称|学位|catalog_year|credits_earned -------|------|--------------|------|-----|------| ------------|-------------- IDT |600 |3 |医生|ID&T |PHD |2015 |{NULL} IDT |610 |3 |医生|ID&T |PHD |2015 |2.75

-- 学生所学的课程将具有 credits_earned 值,您可以以此为基础显示逻辑

然后在您的逻辑中执行类似的操作 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']; }

// 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
}

}

?>

【讨论】:

  • 见上面我的补充。感谢您的意见。
  • 非常感谢.....我一直在认真地主演这几天,试图找出合适的逻辑。先生,你就是男人!
【解决方案2】:

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

【讨论】:

  • 如果您将 Drew 的答案标记为已接受,这将非常有帮助,因为他的解决方案为您解决了问题。
猜你喜欢
  • 1970-01-01
  • 2014-08-18
  • 2014-11-25
  • 1970-01-01
  • 1970-01-01
  • 2016-04-22
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多