В моем классе не установлен массив - PullRequest
1 голос
/ 24 августа 2011

Я устанавливаю класс поиска для своего сайта, и я столкнулся с чем-то, и я понятия не имею, почему он не работает.

Вот класс

<?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 ничего не устанавливается.Есть идеи?

Ответы [ 4 ]

6 голосов
/ 24 августа 2011

Попробуйте использовать

/*
construct method
accepts $location and $keywords
*/
public function __construct($location, $keywords='')
{
    $this->format_location($location);

}

вместо

$this->location = $this->format_location($location);

Поскольку format_location ничего не возвращает.

1 голос
/ 24 августа 2011
preg_match($zip_pattern,$str,$regs);
                                 ^---- S here

(!empty($reg[0])) ? $this->location['zip'] = $reg[0] : '';
            ^--no S                              ^-- no S
1 голос
/ 24 августа 2011

Как насчет изменения

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] : '';

на

preg_match($zip_pattern,$str,$reg);
// if we do have a zip code lets put it in the location array
(!empty($reg[0])) ? $this->location['zip'] = $reg[0] : '';

?

С уважением Макс

0 голосов
/ 24 августа 2011

Я бы попробовал привести $ swinput к строке при объявлении переключателя следующим образом:

switch((string)$swinput) {

Если это не сработает, проблема может быть в условиях до сбоя переключателя.Вы видите сообщение отладки 'hello', когда запускаете это?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...