在PHP中的字符串匹配,有时它工作,有时不,我错过了什么?

时间:2012-05-11 09:37:11

标签: php mysql

所以我有一个搜索表单,当搜索结果页面加载时,顶部有一些跳转菜单,允许用户缩小搜索范围。

跳转菜单显示所选搜索条件。

但是,在“端口”跳转菜单中,如果选择布里斯托尔或爱丁堡,当您登陆结果页面时,端口菜单不会将其显示为已选中。它适用于所有其他人......

这是我在邮轮搜索框中的搜索选择代码

<select name="port" id="port">
<option selected value="%">All Ports...</option>
<option value="Bristol Avonmouth">Bristol Avonmouth</option>
<option value="Edinburgh Leith">Edinburgh Leith</option>
<option value="Glasgow Greenock">Greenock</option>
<option value="Hull">Hull</option>
<option value="Liverpool">Liverpool</option>
<option value="London, Tilbury">London Tilbury</option>
<option value="Newcastle">Newcastle</option>
</select>

这是结果页面上的跳转菜单代码

<div id="cac">
    <h3>Sailing from...</h3>
    <p>&nbsp;</p>
    <p>
    <select name="jumpMenu3" id="jumpMenu3" onchange="MM_jumpMenu('parent',this,0)">
    <option value="">Select a port</option>
    <?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'] OR $_POST['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);
    }
    ?>
    </select>

我已经在数据库中检查了端口本身的拼写,大小写,间距......一切都很好。

也许我需要一个like或wildcard比较运算符?

doh doh doh

修正了 - 端口查找不是针对端口表,它与巡航表中为出发端口写的内容相反,并且有些巡航输入错误...

Rich:)

1 个答案:

答案 0 :(得分:0)

尝试更换:
if ($_GET['port'] == $row_cruises['fromport'] OR $_POST['port'] == $row_cruises['fromport'])

if ($_GET['port'] == urlencode($row_cruises['fromport']) OR $_POST['port'] == urlencode($row_cruises['fromport']))