最近修改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的还是能够获取到。
=======================================解决办法=========================================
上面的传递过去之后,在js里用alert(typeof({$modelcontent })),,得到的是function,应该不是我们想要的值,所以利用$modelcontent [temp],也无法获得相应的元素值,
$smarty->assign('modelcontent',js_encode($modelcontent));,这样就可以得到数组,利用变量temp获取到$modelcontent [temp]的对应值。