0
的reserva_disponibilidade返回相同的陣列(29),當應該返回...... 29,30,31 ... 是有辦法解決這一問題?回到SQL查詢作爲數組
function estoque($data, $dias, $tipo) {
//Ajusta a data...
$inicio = strtotime($data);
$edia = date('d', $inicio);
$emes = date('m', $inicio);
$eano = date('Y', $inicio);
//Conecta ao db...
$db = new DBConfig();
$db->config();
$db->conn();
//Arrays
$smarty_array = array();
$smarty_data_array = array();
$query_quartos = mysql_query("SELECT * FROM quartos AS quartos
INNER JOIN tipos AS tipos
LEFT JOIN reservas AS reservas
ON quartos.quarto_tipo = tipos.tipo_id
AND quartos.quarto_numero = reservas.reserva_quarto_id
WHERE quartos.quarto_tipo = '".$tipo."'
GROUP BY quartos.quarto_id HAVING Count(*) >= 1") or die(mysql_error());
while($row = mysql_fetch_assoc($query_quartos)){
$quartoid = $row["quarto_id"];
while($i <= $dias) {
$today = strtotime(date('Y-m-d',mktime(0,0,0,date($emes),date($edia)+$i,date($eano))));
$query = mysql_query("SELECT * FROM quartos AS quartos
LEFT JOIN reservas AS reservas
ON quartos.quarto_numero = reservas.reserva_quarto_id
AND ".$today." BETWEEN reservas.reserva_checkin AND reservas.reserva_checkout
WHERE quartos.quarto_id = '".$quartoid."'
GROUP BY quartos.quarto_id HAVING Count(*) >= 1") or die(mysql_error());
while($rowe = mysql_fetch_assoc($query)){
if (empty($rowe["reserva_status"])) {
$rowe["reserva_status"] = "0";
}
$smarty_data_array[] = $rowe["reserva_status"];
}
$i++;
}
$row["reserva_disponibilidade"] = $smarty_data_array;
$smarty_array[] = $row;
}
$db->close();
return $smarty_array;
}
輸出
Smarty_Variable Object (3)
->value = Array (3)
0 => Array (16)
quarto_id => "12"
quarto_tipo => "1"
quarto_numero => "1"
quarto_descricao => "Quarto padrão"
tipo_id => "1"
tipo_nome => "Standard"
tipo_foto => "03f9efa3b682512c74a9275656622b03.jpg"
reserva_id => "2"
reserva_quarto_id => "1"
reserva_valor => "400"
reserva_status => "2"
reserva_cliente_id => "58"
reserva_checkin => "1303794000"
reserva_checkout => "1303966800"
reserva_obs => "Aguardando pagamento"
reserva_disponibilidade => Array (29)
0 => "0"
1 => "0"
2 => "0"
3 => "0"
4 => "0"
5 => "0"
6 => "0"
7 => "0"
8 => "0"
9 => "0"
10 => "0"
11 => "0"
12 => "2"
13 => "2"
14 => "2"
15 => "0"
16 => "0"
17 => "0"
18 => "0"
19 => "0"
20 => "0"
21 => "0"
22 => "0"
23 => "0"
24 => "0"
25 => "0"
26 => "0"
27 => "0"
28 => "0"
1 => Array (16)
quarto_id => "15"
quarto_tipo => "1"
quarto_numero => "5"
quarto_descricao => "Deluxe "
tipo_id => "1"
tipo_nome => "Standard"
tipo_foto => "03f9efa3b682512c74a9275656622b03.jpg"
reserva_id => null
reserva_quarto_id => null
reserva_valor => null
reserva_status => null
reserva_cliente_id => null
reserva_checkin => null
reserva_checkout => null
reserva_obs => null
reserva_disponibilidade => Array (29)
0 => "0"
1 => "0"
2 => "0"
3 => "0"
4 => "0"
5 => "0"
6 => "0"
7 => "0"
8 => "0"
9 => "0"
10 => "0"
11 => "0"
12 => "2"
13 => "2"
14 => "2"
15 => "0"
16 => "0"
17 => "0"
18 => "0"
19 => "0"
20 => "0"
21 => "0"
22 => "0"
23 => "0"
24 => "0"
25 => "0"
26 => "0"
27 => "0"
28 => "0"
2 => Array (16)
quarto_id => "26"
quarto_tipo => "1"
quarto_numero => "80"
quarto_descricao => "Quarto novo "
tipo_id => "1"
tipo_nome => "Standard"
tipo_foto => "03f9efa3b682512c74a9275656622b03.jpg"
reserva_id => null
reserva_quarto_id => null
reserva_valor => null
reserva_status => null
reserva_cliente_id => null
reserva_checkin => null
reserva_checkout => null
reserva_obs => null
reserva_disponibilidade => Array (29)
0 => "0"
1 => "0"
2 => "0"
3 => "0"
4 => "0"
5 => "0"
6 => "0"
7 => "0"
8 => "0"
9 => "0"
10 => "0"
11 => "0"
12 => "2"
13 => "2"
14 => "2"
15 => "0"
16 => "0"
17 => "0"
18 => "0"
19 => "0"
20 => "0"
21 => "0"
22 => "0"
23 => "0"
24 => "0"
25 => "0"
26 => "0"
27 => "0"
28 => "0"
->nocache = false
->scope = "Smarty root"
TL; DR;無論如何,這是什麼:'$ row [「reserva_disponibilidade」] = $ smarty_data_array;'不應該被顛倒?這種方式你assignig $行鍵整個smart_data_array..Sorry如果我錯了,我有點疲憊,困了LOL – 2011-04-14 20:50:01
我認爲這是正確的,數據正確返回,但重複... – Hbug 2011-04-14 20:55:13
有一件事我會說,把一個SQL查詢操作放在一個循環中?糟糕的主意,如果你可以避免它。同上您的時間戳生成邏輯。時間戳不會(或至少不應該)在循環中更改,因此您可以在進入循環之前生成一次。至於SQL查詢,使用IN而不是=表示您可以匹配字段的值列表而不是單個字段。您可以使用循環來構建列表,然後運行單個查詢來獲取所有數據。它幾乎肯定會快得多。 – GordonM 2011-04-14 20:55:54