2013-02-27 27 views
0

我有一个表格路线,其中保存了所有行程。然后,根据$_SESSION值,我将行程表中的所有数据提取到复选框数组中。MySQL在查询2中查找结果query1

<?php 
session_start(); 
require("aacfs.php"); 
echo"<div class='box' style='display:inline-block;' > 
<table width='800'><tr><td> 
    <table width=100 align=left class='hovertable' style='display:inline-block;'><tr><th align=center bgcolor=silver><font face=consolas>Choose Itinerary</font></th></tr> 
    <tr> 
     <td></br> 
     <form method=post>"; 


$result=mysql_query("select * from itinerary where aircraft = '$_SESSION[rtyp]' group by location"); 
    $y=mysql_affected_rows(); 
    for($i=0;$i<$y;$i++) 
    {$row=mysql_fetch_array($result); 
     $pr=$row['icode']; 
     $n=$row['location']; 
     $l=$row['btime']; 
echo"<table border=1 align=center width=250> 
      <tr><td width=15> 
      <input type='checkbox' value='$n -> $l'> 
      </td><td>$n</td><td width=5>$l</td></tr> 
      </table>"; 

     } 
?> 

然后,我有另一张表,其中保存了以前选择的行程。我们称之为位置表。我想要选择那里的所有数据,在我以前的查询中找到它。如果找到,该复选框将被自动检查。

因此,例如我已经从行程表中的这些数据:

enter image description here

,然后我有从位置表中的这些数据:

enter image description here

从所述位置的值表格应该是检查。意思是Cebu - BoholBohol - Cebu数值在复选框上应该是。我只是不知道如何使用查询来做到这一点。请帮帮我。谢谢。

编辑:我可以做我想做什么使用这个代码,

$result=mysql_query("select * from itinerary where aircraft = '$_SESSION[rtyp]' group by location"); 
    while($row=mysql_fetch_array($result)) 
    { 
     $pr=$row['icode']; 
     $n=$row['location']; 
     $l=$row['btime']; 
     $res=mysql_query("select * from location where reservno = '$_SESSION[rno]'") or die(mysql_error()); 
     while($row1=mysql_fetch_array($res)) 
     { 
      $loc=$row1['location']; 
      if($n==$loc) 
      { 
       echo"<table border=1 align=center width=250> 
       <tr><td width=15> 
       <input type='checkbox' value='$n -> $l' checked='yes'> 
       </td><td>$n</td><td width=5>$l</td></tr> 
       </table>"; 

       $t=$_POST['prod']; 
      } 
     elseif($n!=$loc) 
     { 
      echo"<table border=1 align=center width=250> 
      <tr><td width=15> 
      <input type='checkbox' value='$n -> $l'> 
      </td><td>$n</td><td width=5>$l</td></tr> 
      </table>"; 

      $t=$_POST['prod']; 
     } 
    } 
    } 

但是,

enter image description here

,你可以看到,它呼应了行程的两倍。我怎么能一次回复行程?谢谢!

回答

0

取得它使用此代码:

$result=mysql_query("select * from itinerary where aircraft = '$_SESSION[rtyp]' group by location"); 
    while($row=mysql_fetch_array($result)) 
    { 
     $pr=$row['icode']; 
     $n=$row['location']; 
     $l=$row['btime']; 
     $res=mysql_query("select * from location where reservno = '$_SESSION[rno]'") or die(mysql_error()); 
     while($row1=mysql_fetch_array($res)) 
     { 
      $loc=$row1['location']; 
       if($n==$loc){$ctr="true"; break;} 
       else{$ctr="false";} 

     } 
     if($ctr=="true"){ 
       echo"<table border=1 align=center width=250> 
       <tr><td width=15> 
       <input type='checkbox' value='$n -> $l' checked='yes'> 
       </td><td>$n</td><td width=5>$l</td></tr> 
       </table>"; 

       $t=$_POST['prod']; 
      } 
      else 
      { 
       echo"<table border=1 align=center width=250> 
       <tr><td width=15> 
       <input type='checkbox' value='$n -> $l'> 
       </td><td>$n</td><td width=5>$l</td></tr> 
       </table>"; 

       $t=$_POST['prod']; 
      } 

    } 
0
select i.*, l.location is not null as selected 
from 
    itinerary i 
    left join 
    location l on i.location = l.location 
where aircraft = '$_SESSION[rtyp]' 
group by location 

现在检查是否在PHP代码中选择了true。

+0

谢谢回答,但'icode'不会在位置表存在。我使用“位置”(例如'宿雾 - 保和省)来比较它们。 'location'字段是唯一的。 – xjshiya 2013-02-27 11:51:03

+0

@xjshiya好吧,只是改变了比较到'位置' – 2013-02-27 11:52:57

+0

试过了,但它没有做我想要的。有没有像'如果query_result_location存在于query_result_itinerary然后','如果不存在然后'?我希望在PHP部分也有一些帮助。谢谢! – xjshiya 2013-02-27 11:56:49

0

您是否尝试过使用Inner Join

给人打预防针:

SELECT a.location, a.btime FROM table1 a 
INNER JOIN table2 b ON a.location = b.location 

顺便说一句,这里的碧瑶?

+0

感谢您的回答。我尝试过,但它没有做我想要的。有没有像'如果query_result_location存在于query_result_itinerary然后','如果不存在然后'?我希望在PHP部分也有一些帮助。我还要添加碧瑶。 :) – xjshiya 2013-02-27 11:45:51