Codeigniter选择具有多个id的数据

Bra*_*son 2 php codeigniter

这是我的代码示例和解释我要做的事情.

在代码的开头,我在数据库中选择了三个变量.然后我将它们与确定距离的函数进行比较,如果距离在给定距离内,则我具有该记录的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"}]}
Run Code Online (Sandbox Code Playgroud)

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

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"}]}
Run Code Online (Sandbox Code Playgroud)

关于如何获得距离的任何想法都被附加到每个结果的末尾?

        $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()));
    }
Run Code Online (Sandbox Code Playgroud)

Kem*_*lah 12

尝试使用 where_in()

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

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