0

这是查询,它返回对我的游轮和票价表的搜索:

SELECT *, MIN(fares.offered) FROM cruises,fares 
WHERE cruises.departs > CURDATE() AND (cruises.destination LIKE %s OR 
  cruises.second_destination LIKE %s) AND 
  EXTRACT(YEAR_MONTH from cruises.departs) LIKE %s 
  AND cruises.fromport LIKE %s 
  AND cruises.ship LIKE %s AND cruises.live = 'Y' 
  AND fares.cruise_id = cruises.id 
GROUP BY fares.cruise_id ORDER BY cruises.departs, cruises.fromport"

我有一些跳转菜单,以便用户可以缩小搜索范围,例如上面的查询返回 2012 年 7 月的所有游轮,有一些,一些从伦敦出发,一些从利物浦出发。

我的港口选择菜单填充了所有航次,像这样

London
Liverpool
London
London
Liverpool

7 月份每返回一次航行记录。

我只想要

London
Liverpool

这是选择代码:

<select name="jumpMenu3" id="jumpMenu3" onchange="MM_jumpMenu('parent',this,0)">
    <option value="">Select a port</option>
    <?php
    $port = ''; 
    mysql_data_seek($cruises, 0);
    while ($row_cruises = mysql_fetch_assoc($cruises)) { 
        if ($row_cruises['fromport'] != $port) {
            $port = $row_cruises['fromport'];
    ?>
    <option value="index.php?subj=2&destination=<?php 
       echo urlencode($row_cruises['destination']);
    ?>&departs=<?php 
      echo date('Ym',strtotime($row_cruises['departs']));
    ?>&port=<?php 
      echo urlencode($port);?>"<?php 
    if ($_GET['port'] == $row_cruises['fromport']) {
      echo "selected=\"selected\"";
    } 
    ?>><?php echo $port; ?></option>
    <?php } ;
    }
    if(mysql_num_rows($cruises) > 0) {
        mysql_data_seek($cruises, 0);
        $row_cruises = mysql_fetch_assoc($cruises);
    }
    ?>
    </select>

我想过 GROUP BY 但我不能在我的港口搜索查询中使用它,因为显然每个港口都有不止一个航次,也许我需要单独查询月份选择选项 - 或者我可以用 php 分组?

4

1 回答 1

2

最好的方法是另一个查询来检索端口名称,如下所示:

SELECT DISTINCT fromport FROM cruises WHERE cruises.departs > CURDATE()

您可以添加其他应适用的条件。

第二种方法可以在 PHP 中完成:

$ports = array(); 
mysql_data_seek($cruises, 0);
while ($row_cruises = mysql_fetch_assoc($cruises)) { 
    if (!in_array($row_cruises['fromport'], $ports)) {
        $ports[] = $row_cruises['fromport'];
?>
<option value="index.php?subj=2&destination=<?php echo urlencode($row_cruises['destination']);?>&departs=<?php echo date('Ym',strtotime($row_cruises['departs']));?>&port=<?php echo urlencode($row_cruises['fromport']);?>"<?php if ($_GET['port'] == $row_cruises['fromport']) {echo "selected=\"selected\"";}?>><?php echo $row_cruises['fromport']; ?></option>
<?php
    }
}
if(mysql_num_rows($cruises) > 0) {
    mysql_data_seek($cruises, 0);
    $row_cruises = mysql_fetch_assoc($cruises);
}
于 2012-05-10T12:44:29.810 回答