将SQL查询作为数组返回

时间:2011-04-14 20:44:25

标签: php

reserva_disponibilidade 返回相同的数组(29),何时返回... 29,30,31 ... 有办法解决这个问题吗?

    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"

1 个答案:

答案 0 :(得分:1)

这里的问题是在为$ quartoid分配值时。如果你只看这段代码:

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++;
        }

while($i <= $dias) {开头的第二个while循环在其中具有完全相同的SQL查询,执行$ dias次。在此迭代期间,变量的值永远不会改变,因为$ today和$ quartoid将是相同的。我希望能回答你的问题,对不起如果我完全错过了这个问题,你的行命名对我来说有点模糊:)