【发布时间】:2012-11-02 01:54:37
【问题描述】:
如何将查询的结果放入数组中,结果应该比较每个结果并合并到一个数组中。
我有 3 个查询,我不知道我的编码有什么问题,我最好的还不够,所以请大家帮助我,尤其是 sql 和 php 专家。
这是我的代码:
<?php
$a = $p['id'];
$query1 = mysql_query("SELECT id FROM prereservation where '$arrival' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
$rows1 = mysql_fetch_array($query1);
$query2 = mysql_query("SELECT id FROM prereservation where '$departure' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
$rows2 = mysql_fetch_array($query2);
$query3 = mysql_query("SELECT id FROM prereservation where arrival > '$arrival' and departure < '$departure' and room_id ='$a' and status = 'active'");
$rows3 = mysql_fetch_array($query3);
$sample = array_unique(array_merge($rows1, $rows2, $rows3));
?>
显示是这样的:
<select id="select" name="qty[]" style=" width:50px;" onchange="checkall()">
<option value="0"></option>
<? $counter = 1; ?>
<? while ($counter <= ($p['qty']) - $????????) { ?>
<option value="<?php echo $counter ?>"><?php echo $counter ?></option>
<? $counter++;
}?>
</select>
我想要的例如输出是这样的:
$rows1 = array(id= 48, 55, 51, 53)
$rows2 = array(id= 48, 49, 51, 52)
$rows3 = array(id= 48, 49, 50, 51)
$sample = array_unique(array_merge($rows1, $rows2, $rows3))
所以样本的输出是这样的:
$sample = array(id= 48, 49, 50, 51, 52, 53)
那么在表格预订中,列是:'id', 'arrival', 'departure', 'room_id'、'数量'和'状态'。
在我的餐桌预订中,每个 id 都有 不同 *数量*,所以我想要 sum 个,共 qty 个,每个 id 的样本如下:
id | qty
48 | 2
49 | 1
50 | 1
51 | 3
52 | 1
53 | 3
$total = sum(qty); which is 11
你们能帮我如何创建一个查询来获取每个 qty 的 sum id 的?还请检查前 3 个查询,因为我知道每个查询都有一个 错误。获取 array_merge 和 array_unique。 非常感谢你们。
<select id="select" name="qty[]" style=" width:50px;" onchange="checkall()">
<option value="0"></option>
<? $counter = 1; ?>
<? while ($counter <= ($p['qty']) - $total) { ?>
<option value="<?php echo $counter ?>"><?php echo $counter ?></option>
<? $counter++;
}?>
</select>
这里是我的数据库:
table rooms
id | type | qty
---|---------|-----
11 | single | 50
12 | double | 50
13 | deluxe | 20
table preservation
id | arrival | departure | room_id | qty | status
---|-------------|-------------|---------|-----|-------
48 | 05/11/2012 | 11/11/2012 | 11 | 2 | active
49 | 06/11/2012 | 11/11/2012 | 11 | 1 | active
50 | 06/11/2012 | 08/11/2012 | 11 | 1 | active
51 | 05/11/2012 | 07/11/2012 | 11 | 3 | active
52 | 06/11/2012 | 09/11/2012 | 11 | 1 | active
53 | 07/11/2012 | 07/11/2012 | 11 | 3 | active
所以在我的显示中,我有一个来自 table rooms(id=11) 的选择标签,选项是 qty 和 deducted by preservation(room_id=11)
中 qty 的strong>总和【问题讨论】:
-
为什么不做一个 SQL 查询并用 OR 连接 where 子句?