我的程序中有这个代码:
<?php
session_start();
$_SESSION['user_id']=201102887;
$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = "SELECT * FROM courses
INNER JOIN transfer_student_courses
ON transfer_student_courses.course_number = courses.course_number
INNER JOIN transfered_courses
ON transfer_student_courses.sn = transfered_courses.sn
AND transfer_student_courses.student_ID = " . $_SESSION['user_id'];
$result = mysqli_query($con , $q);
if($result){
echo "<table>";
echo "<tr>";
echo "<th>equivalent</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["equivalent"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_select_db($con,"uoh");
$q = "SELECT * FROM courses
LEFT JOIN degree_plan
ON degree_plan.course_number = courses.course_number
LEFT JOIN student_record
ON courses.course_number = student_record.course_number
AND student_record.id = ". $_SESSION['user_id']."
WHERE degree_plan.major = 'COE'
ORDER BY term_no";
$result = mysqli_query($con , $q );
if($result){
echo "<table>";
echo "<tr>";
echo "<th>course</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
我在这段代码中有两个查询,每个查询都会给我一个课程列表。 如果课程出现在第一个列表中,我不希望它出现在第二个列表中。
如果你看到下面这段代码的输出,第一个查询会给我MATH 101
,第二个查询也会给我MATH 101
。
我希望MATH 101
不会出现在第二个课程列表中,因为它也出现在第一个列表中。
如何用PHP语言编写函数来做到这一点?
输出:
equivalent MATH 101 course PHYS 101 CHEM 101 PE 101 IAS 101 MATH 101 ENGL 101
答案 0 :(得分:0)
您可以将第一个查询的结果加载到数组中,然后当您显示第二个查询的结果时,在显示任何内容之前检查数组中是否存在结果,如果是,请跳过它。
所以在你第一次询问之后:
$equivalent = array(); // Setup a blank array to store the results
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["equivalent"]. "</td>";
echo "</tr>";
$equivalent[] = $row['equivalent']; // Add the result into your array
}
然后在你的课程部分代码:
while($row = mysqli_fetch_array($result))
{
if ( ! in_array($row['code'], $equivalent ) )
{
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "</tr>";
}
}
这应该停止显示在第二个中显示的两个结果集中的任何条目。
HTH
答案 1 :(得分:0)
首先,将所有equivalent
课程存储在一个数组中,比如$equivalent
数组。然后在第二个while
循环中使用in_array()
函数检查课程是否已在第一个表中打印过。
以下是参考资料:
所以你的代码应该是这样的:
<?php
session_start();
$_SESSION['user_id']=201102887;
$con = mysqli_connect('localhost', 'root', '');
if(!$con){
die("not ok");
}
mysqli_select_db($con,"uoh");
$q = "SELECT * FROM courses INNER JOIN transfer_student_courses ON
transfer_student_courses.course_number=courses.course_number INNER
JOIN transfered_courses ON transfer_student_courses.sn=transfered_courses.sn
AND transfer_student_courses.student_ID = " . $_SESSION['user_id'];
$result = mysqli_query($con , $q) ;
$equivalent = array();
if($result){
echo "<table>";
echo "<tr>";
echo "<th>equivalent</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
$equivalent[] = $row["equivalent"];
echo "<tr>";
echo "<td>" . $row["equivalent"]. "</td>";
echo "</tr>";
}
echo "</table>";
}
mysqli_select_db($con,"uoh");
$q = "SELECT * FROM courses
LEFT JOIN degree_plan ON degree_plan.course_number= courses.course_number
LEFT JOIN student_record ON courses.course_number= student_record.course_number
AND student_record.id= ". $_SESSION['user_id']."
WHERE degree_plan.major='COE' ORDER BY term_no";
$result = mysqli_query($con , $q ) ;
if($result){
echo "<table>";
echo "<tr>";
echo "<th>course</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
if(in_array($row["code"], $equivalent)){
continue;
}
echo "<tr>";
echo "<td>" . $row["code"]. "</td>";
echo "</tr>";
}
}
echo "</table>";
?>
答案 2 :(得分:0)
解决方案:in_array(&#34; word&#34;,$ array)
运行查询将结果输入数组:
while($row = mysqli_fetch_array($result))
{
arr1[]=$row["code"];
}
对于第二个查询,运行查询并按如下方式添加结果:
while($row = mysqli_fetch_array($result)){
if (in_array($row[code], $arr1))
{
//don't add to array2
}
else
{
arr2[] = $row["code"];
}
}
然后您可以根据需要echo
元素。