2012-08-16 65 views
2

我的網站顯示一組div,每個div代表一個包含五種不同類型內容的集合。每個div顯示每個類型的內容在該集合中有多少項。如何從Smarty內部訪問Mysql結果集?

我目前得到每個類型的數量從數據庫中,像這樣:

{section name=res loop=$results} 

{assign var='wn' value=$db->num_rows($db->query("select * from content where type='1' and collection_id='{$results[res].collection_id}'"))} 
{assign var='nn' value=$db->num_rows($db->query("select * from content where type='2' and collection_id='{$results[res].collection_id}'"))} 

的問題是,我們正在做的5個數據庫查詢,對於每個DIV,我們想嘗試將它們合併爲一個。使用php,我們只需要做

$w=$db->query("select type, count(*) as number from content where collection_id='".$val['collection_id']."' GROUP by type"); 

然後用「while」循環和「mysql_fetch_array」將類型數量分配給變量。

是否有可能在Smarty中做類似的事情?是否有一個mysql_fetch_array替代Smarty來訪問結果集?

在此先感謝!

編輯:有人建議,我應該從PHP做到這一點,但我不清楚這將如何工作。在我的PHP文件,我有以下幾點:

<?php 


$c=$db->query("select * from ".USERS_PIN.""); 

$cdata = array(); 

while ($row = mysql_fetch_array($c)) 
    { 
     $cdata[] = $row; 
    } 

$smarty->assign("results", $cdata); 

$smarty->display('stack.tpl'); 

?> 
在stack.tpl

然後,我有以下幾點:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
</head> 
<body > 

<div id="ColumnContainer"> 

{if count($results) > 0} 

      {section name=res loop=$results} 

      {assign var='wn' value=$db->num_rows($db->query("select * from pinrest_users_pin where type='4' and board_id='{$results[res].board_id}'"))} 
      {assign var='nn' value=$db->num_rows($db->query("select * from pinrest_users_pin where type='5' and board_id='{$results[res].board_id}'"))} 
      {assign var='in' value=$db->num_rows($db->query("select * from pinrest_users_pin where (type='1' or type='2') and board_id='{$results[res].board_id}'"))} 
      {assign var='vn' value=$db->num_rows($db->query("select * from pinrest_users_pin where type='3' and board_id='{$results[res].board_id}'"))} 


<div class='item' style="height:70px;width:350px;float:left;border:2px solid aqua" data-id="{$results[res].id}"> 

    <div class="datadiv" > 

<div style="margin-top:3px;width:40%;float:left;margin-left:15px">{$nn} news {$vn} videos {$in} images {$wn} webpages</div> 

      </div> 

    </div> 

{/section} 

{/if}  

</div> 

</body> 
</html> 

我計算過,是我是從建築的div一次一個Smarty循環,我別無選擇,只能在循環中一次獲取每個div的mysql數據。有沒有辦法讓我在php文件中爲每個div獲取這些數據,然後在循環中將它分配並用於Smarty?

+0

對不起,我不是很清楚,你能告訴我們你的模板看起來像什麼,或者你想要它看起來像什麼? – Anas 2012-08-16 23:23:24

+0

Anas,我剛剛添加了php和tpl代碼。 – Phil 2012-08-17 00:06:40

+0

讓我們繼續聊天,以便我可以理解您的問題:http://chat.stackoverflow.com/rooms/15442/how-does-one-access-a-mysql-result-set-from-within-smarty – Anas 2012-08-17 00:36:22

回答

4

在您的Smarty模板中執行SQL會首先破壞使用模板系統的目的。在你的控制器文件中處理這個。

我對PHP的sql方法有點生疏,所以請使用正確的方法來調整它以實現。方案1:

$smarty->display('beginning_of_view.tpl'); 
$q = $db->query('your query for all the divs'); 

while($row = mysql_fetch_assoc($q)) 
{ 
    $smarty->assign('row', $row); 
    $smarty->display('my_div.tpl'); 
} 
$smarty->display('end_of_view.tpl'); 

解決方案2(可能是更好的方式去了解它):

$rows = array(); 
$q = $db->query('your query for all the divs'); 

while($row = mysql_fetch_assoc($q)) 
{ 
    $rows[] = $row; 
} 
$smarty->assign('rows', $rows); 
$smarty->display('template.tpl'); 

//In your template 
{foreach $rows as $row} 
{$row} 
{/foreach} 

希望這個跨有想法。

+0

問題是我需要訪問每個div的mysql數據 - 我使用Smarty節循環放置在頁面上。 – Phil 2012-08-16 22:56:56

+0

爲什麼不爲該div創建一個smarty模板,並在控制器中循環遍歷它,爲每個div渲染該模板一次?或者把整個事情變成一個多層次的數組,你可以稍後在模板中循環。 – sgrif 2012-08-16 23:02:32

+0

sgrif,多級陣列選項聽起來太可怕了:-)。但你的第一個選擇聽起來很有趣,我只是不能100%確定你的意思。你能否詳細說明/舉一個例子? – Phil 2012-08-16 23:15:46

2

您可以指定由mysql_fetch_array返回一個智者變量數組,然後你循環使用Smarty

下面是一個例子:

你的PHP文件:

<?php 
$arr = array(1000, 1001, 1002); 
$smarty->assign('myArray', $arr); 
?> 

您的Smarty的模板

{foreach from=$myArray item=foo} 
    {$foo} 
{/foreach} 

編輯:

爲了您的要求,您應該使用multimentional /嵌套的數組,看看這個問題: php smarty loop multidimensional array

+0

我試過這樣做,但似乎你不能把Smarty部分內的php代碼。我嘗試了{php} {/ php},頁面崩潰了。 – Phil 2012-08-16 22:52:56

+0

使用像smarty這樣的模板引擎的目的是將預置部分與邏輯部分分開!您應該:** 1。**在您的php文件中訪問您的mysql數據,** 2。**將其分配給smarty變量。 ** 3。**在你的smarty模板中,製作一個循環 – Anas 2012-08-16 22:56:35

+0

Anas,我不知道如何爲php中的每個div分配數據。我已經更新了問題以澄清問題。 – Phil 2012-08-16 23:13:38

0

在你的PHP,做4個查詢(那些從TPL)主查詢循環內,並且將結果添加爲$行的字段:

<?php 

$c=$db->query("select * from ".USERS_PIN."");   
$cdata = array(); 

while ($row = mysql_fetch_array($c)) 
      { 
      $d = $db->query("select * from pinrest_users_pin where type='4' and  board_id=".$row['board_id']); 
      $row['wn'] = mysql_fetch_array($d); // repeat 4X 
      $cdata[] = $row; 
      } 

$smarty->assign("results", $cdata);   
$smarty->display('stack.tpl');   
?> 

然後在tpl中使用$ results [res] .wn在您的div中。