0

我有一个功能,当您搜索邮政编码或机场时,它会在文本框中显示一组结果,如下所示(它应该如何工作):

在此处输入图像描述

这工作正常,直到我在地址搜索中放置一些“随机”的东西,并且由于某种原因,即使搜索列表中没有结果,它仍然会填充下拉菜单(见下文):

在此处输入图像描述

从上面的搜索中,在萤火虫控制台中,我得到以下响应:

No results found, please try a different post code.)

以及以下 XML 响应:

XML Parsing Error: syntax error Location: moz-nullprincipal:{90e67c6c-c3a7-497c-a4df-6045472afde6} Line Number 1, Column 1:

No results found, please try a different post code.

下面是代码:

public static function getAddressByPostcode($postcode) {

    $addressArray = array();

    $uri = 'http://[ipaddress]:[port]/?pc='.$postcode; // Send to Computer

    $response = CurlController::request($uri);

    if ($response != false) {

        // Clean html
        $response = strip_tags($response);

        $deserializedAddressArray = unserialize($response); 

        // Get result set count
        self::set_resultcount(count($deserializedAddressArray));

        if (self::get_resultcount() > 0) {

            foreach ($deserializedAddressArray as $address) {

                if(!isset($address['company']) || $address['company'] == '') {
                    $address['company'] = '';
                    $addr_cat = 1;
                } else {
                    $addr_cat = 2;
                }

                if(!isset($address['no'])) $address['no'] = '';
                if(!isset($address['street'])) $address['street'] = '';
                if(!isset($address['locality'])) $address['locality'] = '';
                if(!isset($address['town'])) $address['town'] = '';
                if(!isset($address['postcode'])) $address['postcode'] = '';

                if(!isset($address['Longitude'])) {
                    $address['Longitude'] = '';
                } else {
                    $address['Longitude'] = ($address['Longitude'] / 1000) / 3600;
                }

                if(!isset($address['Latitude'])) {
                    $address['Latitude'] = '';
                } else {
                    $address['Latitude'] = ($address['Latitude'] / 1000) / 3600;
                }



                $addressObject = new Address($addr_cat,$address['company'],$address['no'],$address['street'],
                                             $address['locality'],$address['town'],$address['postcode'],
                                             $address['Latitude'],$address['Longitude']);

                ## Swapped lat and long around as field spec on response is wrong

                ## TODO - Add validation for address object

                // Add address into addresses array
                array_push($addressArray,$addressObject);

            }

            self::getAddressesAsXML($addressArray);

        } else {

            self::$text_response .= "No results found, please try a different post code. \r\n";
            echo self::$text_response;

        }

    } else {

        self::$text_response .= "Address server is taking too long to respond, please try again later. \r\n";
        echo self::$text_response;

    }

}

如果有人可以帮助我,将不胜感激。我似乎无法弄清楚为什么它填充一个空数组而不显示错误警报消息。

4

1 回答 1

0

您检查是否有任何结果: if (self::get_resultcount() > 0) { } 当您关闭该 IF 语句时,您有 self::getAddressesAsXML($addressArray);

当你没有结果时,我认为 xml array thingy 不起作用,也许你应该将它添加到 IF 语句中。

我不确定这是否可行!值得一试

于 2012-11-08T12:51:45.337 回答