【发布时间】:2023-03-28 12:00:02
【问题描述】:
我正在为我的网站设置一个搜索类,但我遇到了一些问题,但我不知道为什么它不起作用。
这是课程
<?php
class search
{
public $location = array();
public $keywords = array();
/*
construct method
accepts $location and $keywords
*/
public function __construct($location, $keywords='')
{
$this->location = $this->format_location($location);
}
/*method format_location
accepts location value, determines its type... city, state, zip code
returns array of properly formatted location values
*/
public function format_location($input)
{
// check if location contains a zip code
$zip_pattern = "/\d{5}/";
$str = $input;
preg_match($zip_pattern,$str,$regs);
// if we do have a zip code lets put it in the location array
(!empty($reg[0])) ? $this->location['zip'] = $reg[0] : '';
// if we don't have a zip let's check for one of our pre=formatted locations, this will avoid unessesary regular expressions and database calls
if(!$this->location['zip']) {
$swinput = strtolower($input);
switch($swinput) {
case "chicago, il":
echo 'hello';
$this->location[] = array('zip' => 60601,
'city' => 'Chicago',
'state' => 'IL',
'lat' => '41.8781136',
'lon' => '-87.629798',
);
break;
case "naperville, il":
$this->location[] = array('zip' => 60564,
'city' => 'Naperville',
'state' => 'IL',
'lat' => '41.785863',
'lon' => '-88.147289',
);
break;
}
}
}
}
?>
然后我就是这样称呼它的
require("lib/search/class.search.php");
$lo = new search('chicago, il');
print_r($lo->location);
问题是在 location 属性中没有设置任何内容。有什么想法吗?
【问题讨论】:
-
我不知道它是否会导致您的错误,但
if (!$this->location['zip'])表示if ($this->location['zip'] == false)。您想知道是否设置了zip键,因此请使用if (isset($this->location['zip']))。