如何从函数生成一个smarty数组?

时间:2011-03-29 20:30:15

标签: php html smarty

    function estoque($data, $dias) {
        $inicio = strtotime($data);
        $edia = date('d', $inicio); 
        $emes = date('m', $inicio);
        $eano = date('Y', $inicio);
        $db = new DBConfig();
        $db->config();
        $db->conn();

        $data = array();
        while($i <= $dias) {
            $today = strtotime(date('Y-m-d',mktime(0,0,0,date($emes),date($edia)+$i,date($eano))));
            //echo "<br/>".date("d-m-Y", $today)."<br/>";
            $query = 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
                                  AND ".$today." BETWEEN reservas.reserva_checkin AND reservas.reserva_checkout
                                  GROUP BY quartos.quarto_id HAVING Count(*) >= 1") or die(mysql_error());
            while($row = mysql_fetch_array($query)){
                if (empty($row["reserva_status"])) {
                    $row["reserva_status"] = "0";
                }
                //echo $row["reserva_status"]."<br/>";
                $tmp = $i++;
            $data[$tmp] = $row;
            }
            $i++;
        }
        $db->close();
        return $data; 
    }

如何返回一个数组来解析为smarty模板?

我的输出模板...... enter image description here

我想要的......

enter image description here

差不多...... 我的模板代码是:

                      <table class="table-filtro">
                            <thead>
                                  <tr>
                                        <th class="nome-quarto">Tipo</th>
                                        <th>Nº Quarto</th>
                                        <th>Label</th>
                                        <th class="th-periodo">9</th>
                                  </tr>
                            </thead>
                            <tbody>
                                {foreach from=$listar item="estoque"}
                                  <tr>
                                        <td class="nome-quarto">{$estoque.tipo_nome}</td>
                                        <td>{if $estoque.quarto_numero|count_characters eq '1'}0{$estoque.quarto_numero}{else}{$estoque.quarto_numero}{/if}</td>
                                        <td>{$estoque.quarto_descricao}</td>
                                        <td><img src="http://{$smarty.server.SERVER_NAME}/reservas/images/cubos/{if $estoque.reserva_status eq '3'}vermelho{elseif $estoque.reserva_status eq '2'}amarelo{else}verde{/if}.jpg" /></td>
                                  </tr>
                                {/foreach}

                            </tbody>
                      </table>

输出ss

enter image description here

1 个答案:

答案 0 :(得分:2)

使用mysql_fetch_assoc()并返回数组。

function estoque($data, $dias) {
  // ...

  // Inside the function, already performed query...
  $smarty_array = array();
  while($row = mysql_fetch_assoc($query)){
    // Add the current row onto $smarty_array
    $smarty_array[] = $row;
  }

  // Finish up your other stuff in the function
  // Return
  return $smarty_array;
}

// Call your function
$output_array = estoque($data, $dias);
// Assign the array to smarty
$smarty->assign('smartyarrayname' $output_array);