我不能在yii2中的registerJS的javascript代碼中使用foreach php代碼。瀏覽器顯示「PHP解析錯誤 - yii \ base \ ErrorException語法錯誤,意外'foreach'(T_FOREACH)」錯誤。這裏是我的代碼:如何在yii2的registerJS()javascript代碼中使用foreach php代碼?
<canvas id="bar-chart" width="500" height="150"></canvas>
<?php
$lang = Yii::$app->language;
$title = 'title_'.$lang;
$this->registerJs("
new Chart(document.getElementById('bar-chart'), {
type: 'bar',
data: {
labels: [
" . foreach ($models as $model) { . "
' " . $model->$title . " ',
" . } . "
],
datasets: [
{
label:'Label',
backgroundColor: ['#3e95cd', '#8e5ea2','#3cba9f', '#e7fc00'],
data: [479,1654,499, 2632]
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Title'
}
}
});
", yii\web\View::POS_READY);
什麼版本的PHP您使用的是? –
我正在使用PHP 5.5版本 –
我寫了正確的PHP代碼在JS代碼我的代碼? –