我通过 jquery.serialize()
为 发送了三次值
在 PHP 中,现在我想每次从发送中获取与 1
& 2
& 3
>$hotel_id$hotel_id
链接的数据(例如:1
或 2
或 3
) 来自数据库。但在下面的代码中,我只获取链接到最后一个 $hotel_id
(3
) 的数据。
我想要的是:
First: get data linked with
1
and echo they injson_encode
Second: (next 2) get data linked with2
and echo they injson_encode
Third: (next 3)get data linked with3
and echo they injson_encode
...
这是 jQuery 代码的输出 serialize()
:
hotel_id=1&hotel_id=2&hotel_id=3
这是我的 php 代码:
$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);
我该怎么办?
最佳答案
您不能将同一个参数传递三次。它只会看到 hotel_id 的最后一个参数,所以你只得到数字 3。PHP 允许通过在末尾用 [] 命名参数来传递数组,如 hotel_id[]=1&hotel_id[]=2 等,然后你可以得到php.ini 中的值数组这意味着 $hotel_id 的值将是一个值数组而不是单个值。由于它是一个数组,因此您需要使用逗号来内爆该数组,以便在 SQL 中使用:
$hotel_id = implode(',', $this->input->post('hotel_id'));
现在 $hotel_id 将看起来像“1,2,3”;
现在您的 SQL 需要更改为:
SELECT * FROM hotel_submits WHERE id IN ($hotel_id) ORDER BY id desc
希望有帮助。
关于php - 每次获取时都会从数据库中回显数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7567950/