2011-03-29 91 views
0
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模板?如何从函数中为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

+0

你是什么意思?你不能只是做'$ tpl-> assign('estoque',estoque($ data,$ dias);'? – Czechnology 2011-03-29 20:34:42

+0

我需要先生成一个数组来分派smarty,我的函数几乎不会生成数组 – Hbug 2011-03-29 20:39:12

回答

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); 
+0

,但模板布局正在打破... – Hbug 2011-03-29 20:50:07

+1

现在你有了你的数据,修复你的模板是一个不同的问题,我建议你发布一个与模板显示有关的新问题 – 2011-03-29 20:57:32

+0

好的,谢谢支持:) – Hbug 2011-03-29 21:09:14

相关问题