开发者

Echo data from database in each times get

I send three times values 1 & 2 & 3 by jquery.serialize() for $hotel_id in PHP, now开发者_如何转开发 i want get in the each times from send, data linked with $hotel_id(like: 1 or 2 or 3) from database. but in the following code i just get data that linked to last $hotel_id(3).

I want as:

First: get data linked with 1 and echo they in json_encode

Second: (next 2) get data linked with 2 and echo they in json_encode

Third: (next 3)get data linked with 3 and echo they in json_encode

...

This is output serialize() from jQuery code:

hotel_id=1&hotel_id=2&hotel_id=3

This is my php code:

$hotel_id = $this->input->post('hotel_id');
$query_r  = $this->db->query("SELECT * FROM hotel_submits WHERE id LIKE '$hotel_id' ORDER BY id desc");
$data     = array();
foreach ($query_r->result() as $row) {
    $data_s  = json_decode($row->service, true);
    $data_rp = json_decode($row->address, true);
    $data[]  = array(
        'name' => $row->name,
        'star_type' => $row->star . '-' . $row->type,
        'site' => $row->site,
        'service' => $data_s,
        'address' => $row->address
    );
}
echo json_encode($data);

How do i do?


You can't pass in the same argument three times. It will only see the final parameter of hotel_id and so you just get number 3. PHP does allow for passing Arrays by naming the parameter with [] at the end like hotel_id[]=1&hotel_id[]=2, etc and then you can get the array of values in php. That means your value of $hotel_id would be an array of values instead of a single value. Since it is an array you would need to implode the array with a comma to use in your SQL:

$hotel_id = implode(',', $this->input->post('hotel_id'));

Now $hotel_id will look like '1,2,3';

Now your SQL will need to change to:

SELECT * FROM hotel_submits WHERE id IN ($hotel_id) ORDER BY id desc

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜