XML / PHP - 如果搜索响应为空

时间:2012-11-08 12:37:10

标签: php xml

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

enter image description here

这样可以正常工作,直到我在地址搜索中添加“随机”内容,并且由于某种原因它仍会填充下拉菜单,即使搜索列表中没有结果(见下文):

enter image description here

从上面的搜索中,在firebug控制台中我收到以下响应:

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;

    }

}

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

1 个答案:

答案 0 :(得分:0)

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

如果没有结果,我认为xml数组不起作用,也许你应该把它添加到IF语句中。

我不确定这是否会起作用!值得一试