这是我的代码的示例,并解释了我要做什么。

在代码的开头,我在数据库中选择了三个变量。然后,我将它们与确定距离的函数进行比较,如果该距离在给定距离之内,那么我将获得该记录的ID。这将导致多个ID。可以是五个,也可以是五个,取决于给定的变量。

我需要知道的是,一旦确定需要使用的ID,就需要将其放入变量或其他可管理的变量中。基本上,我需要获取列表并再次查询数据库以从数据库中获取详细信息列表。该列表将返回对象,这些对象以后将被移至json_encode进行处理。

我需要知道的是将每个ID放入变量并将其传递给数据库查询并获得所需结果的最佳方法是什么。

我尝试使用$dbStations->where('_id', '34,33,45'),但它仅返回第一个值。如果可能,我需要类似WHERE _id = 34 AND 33 AND 45的东西。

我正在为我的框架使用Codeigniter,但我查阅了文档,但没有找到解决方案。

编辑:现在,我已经从数据库中选择了数据,我需要获取要在检索到的每个记录的末尾显示的距离。

Example of json: {"details":[{"country":"United States","_id":"3892","lat":"39.954559","lng":"-82.837608","admin_level_1":"Ohio","locale":"Columbus"}]}


这就是它的需要,请记住,距离不在数据库中,它是即时计算的。

Example of json: {"details":[{"country":"United States","_id":"3892","lat":"39.954559","lng":"-82.837608","admin_level_1":"Ohio","locale":"Columbus", "distance": "1.2 Mi"}]}


关于如何获得距离的任何想法都可以计算并附加到每个结果的末尾?

        $dbStations->select('lat');
        $dbStations->select('lng');
        $dbStations->select('_id');
        $stations = $dbStations->get('stDetails');
        foreach ($stations->result() as $station) {
            $lat2 = $station->lat;
            $lng2 = $station->lng;
            $stId = $station->_id;
            $distance = $this->distance->gpsdistance($lat1, $lng1, $lat2, $lng2, "M");
                if ($distance <= $rad) {
                //at this point the code has determined that this id is within
                //the preferred distance.
                $stationArr[] = $stId;
            }
        }
            $dbStations->select('country, _id, lat, lng, admin_level_1, locale');
            $dbStations->where_in('_id', $stationArr);
            $stations = $dbStations->get('stationDetails');
        echo json_encode(array('stations' => $stations->result()));
    }

最佳答案

尝试使用where_in()

$ids = array(33, 34, 45);
$dbStations->where_in('id', $ids);
// Produces: WHERE id IN (33, 34, 45)


这将返回ID为33、34、45的记录

关于php - Codeigniter选择具有多个ID的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7605884/

10-11 09:21
查看更多