• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-25) 16次浏览 已收录 0个评论
文章目录[隐藏]
<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::P<div>本文来*源gaodai^.ma#com搞#代!码网</div><pre>搞gaodaima代码

ARAM_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;

这几行代码是用来查询附近的人的,采用的是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>

这几行代码是用来查询附近的人的,采用的是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>

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:求大神优化一下下面的这几行代码,感觉好难看。但是又不知道怎么改进
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址