js擷取php(ecshop smarty模板)數組元素值,該如何解決

來源:互聯網
上載者:User
js擷取php(ecshop smarty模板)數組元素值
最近修改PHP東西(ecshop),遇到一個問題,誠懇求助各位高手:
1.在php頁面裡利用smarty模板賦值,部分代碼如下:

while ($row = $db->FetchRow($res))
{
if($cur==null)
$cur=$row['template_id'];
$modelrow[$row['template_id']]=$row['template_id'];
$modelcontent[$row['template_id']]=trim($row['template_content']);
$modelplates[$row['template_id']]=$row['template_subject']." -$row[content_category]-".$row['template_subject_second'];
// echo $modelcontent[$row['template_id']];

}
$smarty->assign('cur', $cur);
$smarty->assign('modelrow', $modelrow);
$smarty->assign('modelplates', $modelplates);
$smarty->assign('modelcontent',$modelcontent);
/*end*/

$modelcontent 數組傳遞到前台的dwt模板中 在js函數中:
1、顯示擷取一個變數值temp=$("#temp").val(); (這個變數值為1、2、3之類的,擷取沒有問題。)
2、var a='{$modelcontent.temp}';第二步利用變數擷取$modelcontent的一個元素值,但是為空白,不知道怎麼樣才能擷取到;我在測試的時候var a='{$modelcontent.1}'擷取數組下標為1的還是能夠擷取到。
現在問題是怎麼根據已知變數擷取到數組對應下標值?還是有其他解決的辦法,謝謝各位! ecshop smarty php

分享到:


------解決方案--------------------
換成
var a='';
alert(a);看看結果
------解決方案--------------------
var a='{$modelcontent.'+temp+'}';
alert(a);
這樣再試一下
------解決方案--------------------
http://www.zui88.com/blog/view-261.html
看看對你是否能有協助

引用
Smarty模版中,數組的鍵名是一個變數的值,如何顯示該鍵名對應的值
in: 後端程式
題目有點繞口,大概的意思是

php已經賦給模版一個數組,數組的資訊如下:

$config= array(

1=>'中山',

2=>'石岐'

);

資料庫儲存地區的欄位記錄的是該數組的鍵名,如1,現在要在模版上顯示:中山。

如果這樣寫會報錯:

{{$config.$row.region}}

模版上的正確的寫法是:{{$config[$row.region]}}

今天遇到的問題還更複雜一點,資料庫欄位儲存的是一些配置的序列化,所以在調取地區資訊時還需要進行還原序列化處理,中間必須有一個賦值的過程:

{{assign var=param value=$l.params
------解決方案--------------------
unserialize}}

然後$param.region就可以取得1這個值了



------解決方案--------------------
{lection}的時候不是有KEY嗎???

要麼傳質的時候多傳一列儲存KEY
------解決方案--------------------
smarty的迴圈會用嗎? 我說的是迴圈數組。不是有個key列麼。你便利數組的時候直接用
剛上面寫錯了
{section name=loop loop=$v}
{$v[loop].數組索引值}
{/section}

你把key列印出來 看是不是key是其他的數字導致按KEY取不到正確的數值
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.