求大神优化一下下面的这几行代码,感觉好难看。但是又不知道怎么改进

WBOY
Release: 2016-06-06 20:45:10
Original
1062 people have browsed it

<code class="lang-php">$sql=' SELECT userid FROM   tbl_myr_refresh
               WHERE geohash
               IN (:geohash1, :geohash2, :geohash3, geohash4, geohash5, geohash6, geohash7, geohash8, geohash9)';
        $geohashCode=Yii::app()->session['geohash'];
        $geohash=new MGeohash();
        $neighbors = $geohash->neighbors($geohashCode);
        array_push($neighbors, $geohashCode);
        $connection=  Yii::app()->db->connection;
        $command=$connection->createCommand($sql);
        $command->bindParam(":geohash1",$neighbors[0],PDO::PARAM_STR);
        $command->bindParam(":geohash2",$neighbors['top'],PDO::PARAM_STR);
        $command->bindParam(":geohash3",$neighbors['bottom'],PDO::PARAM_STR);
        $command->bindParam(":geohash4",$$neighbors['right'],PDO::PARAM_STR);
        $command->bindParam(":geohash5",$neighbors['left'],PDO::PARAM_STR);
        $command->bindParam(":geohash6",$neighbors['topleft'],PDO::PARAM_STR);
        $command->bindParam(":geohash7",$neighbors['topright'],PDO::PARAM_STR);
        $command->bindParam(":geohash8",$neighbors['bottomright'],PDO::PARAM_STR);
        $command->bindParam(":geohash9",$neighbors['bottomleft'],PDO::PARAM_STR);
        $result=$command->queryAll();
        return $result;

</code>
Copy after login
Copy after login

这几行代码是用来查询附近的人的,采用的是GEOHASH。具体的业务就不说了,主要是这段代码怎么才能变得更好看呢?
我觉得太丑了。好像YII CDbCriteria可以优化一些,但不知道咋做!求大神优化代码

回复内容:

<code class="lang-php">$sql=' SELECT userid FROM   tbl_myr_refresh
               WHERE geohash
               IN (:geohash1, :geohash2, :geohash3, geohash4, geohash5, geohash6, geohash7, geohash8, geohash9)';
        $geohashCode=Yii::app()->session['geohash'];
        $geohash=new MGeohash();
        $neighbors = $geohash->neighbors($geohashCode);
        array_push($neighbors, $geohashCode);
        $connection=  Yii::app()->db->connection;
        $command=$connection->createCommand($sql);
        $command->bindParam(":geohash1",$neighbors[0],PDO::PARAM_STR);
        $command->bindParam(":geohash2",$neighbors['top'],PDO::PARAM_STR);
        $command->bindParam(":geohash3",$neighbors['bottom'],PDO::PARAM_STR);
        $command->bindParam(":geohash4",$$neighbors['right'],PDO::PARAM_STR);
        $command->bindParam(":geohash5",$neighbors['left'],PDO::PARAM_STR);
        $command->bindParam(":geohash6",$neighbors['topleft'],PDO::PARAM_STR);
        $command->bindParam(":geohash7",$neighbors['topright'],PDO::PARAM_STR);
        $command->bindParam(":geohash8",$neighbors['bottomright'],PDO::PARAM_STR);
        $command->bindParam(":geohash9",$neighbors['bottomleft'],PDO::PARAM_STR);
        $result=$command->queryAll();
        return $result;

</code>
Copy after login
Copy after login

这几行代码是用来查询附近的人的,采用的是GEOHASH。具体的业务就不说了,主要是这段代码怎么才能变得更好看呢?
我觉得太丑了。好像YII CDbCriteria可以优化一些,但不知道咋做!求大神优化代码

<code>$geohash = new MGeohash();
$geohashes['center'] = Yii::app()->session['geohash'];
$geohashes = array_merge($geohashes, $geohash->neighbors($geohashes['center']));

$sql = "SELECT `userid` FROM `tbl_myr_refresh` WHERE `geohash` IN (%s)";
$sql = sprintf($sql, implode(",", $geohashes));
$db = Yii::app()->db->connection;
$result = $db->createCommand($sql)->queryAll();
</code>
Copy after login

只是要从neighbors里面找到对应的点,没必要一个一个按顺序来吧?

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template