Codeigniter选择具有多个ID的数据 [英] Codeigniter selecting data with multiple id's

查看:87
本文介绍了Codeigniter选择具有多个ID的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码示例和解释我想做的。

Here is an example of my code and an explanation what I am trying to do.

在代码的开头,我选择三个变量在我的数据库。然后我将它们与确定距离的函数进行比较,如果距离在给定距离内,则我有该记录的id。这将导致多个ID。可能是五个或没有,取决于给定的变量。

At the beginning of the code I am selecting three variables in my database. I then compare them to a function that determines the distance, if the distance is within the given distance then I have the id of that record. This will result in multiple id's. Could be five or none, depends on the variables given.

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

What I need to know is once I have determined that id I need to use I need to put into either a variable or something else that is manageable. Basically I need to take the list and query the database again to get the list of details from the database. The list will return objects that will later be past to json_encode for processing.

我需要知道的是什么是最好的方式把每个id放入一个变量,并将其传递给数据库查询,并获得我需要的结果。

What I need to know is what is the best way to put each id into a variable and pass it to a database query and get the results I need.

我尝试过使用 $ dbStations-> where('_ id','34,33,45')它只返回第一个值。我需要像 WHERE _id = 34 AND 33 AND 45

I've tried using $dbStations->where('_id', '34,33,45') but it only returns the first value. I need something like WHERE _id = 34 AND 33 AND 45 if possible.

我使用Codeigniter我已经过去的文档,我还没有找到一个解决方案。

I am using Codeigniter for my framework and I have went over the documentation and I have not found a solution.

编辑:现在我有从数据库中选择的数据我需要得到的距离

Now that I have the the data selected from the database I need to get the distance to be displayed at the end of each record retrieved.

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

这是它需要的,记住距离不在数据库中,

This is what it needs to be, keep in mind that distance is not in the database, it is calculated on the fly.

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"}]}

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

Any ideas on how to get ths distance that is caculcated to be appended to the end of each result?

        $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的记录

This will return records that have the ID of 33, 34, 45

这篇关于Codeigniter选择具有多个ID的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆