有没有一种方法,我们可以从另一个函数获取变量控制器中的当前函数和用户设置函数在相应的视图文件中使用?Cakephp从另一个函数变量继承控制器变量功能
对不起你们我的意思是通过查看文件的变量另一种观点的文件...下面是代码...
function get_random_color()
{ $ C = “”; ($ i = 0; $ i < 6; $ i ++) {c。= dechex(rand(0,15)); } return“#$ c”; } $ i = 0; // debug($ trips); foreach($ trip为$ trip){ $ color = get_random_color();
$numItems = count($trip['trip']['coords']);
if($numItems > 3){
$x = 0;
echo 'var flightPlanCoordinates'.$i.' = [';
foreach($trip['trip']['coords'] as $coords) {
if($x+1 == $numItems) {
echo 'new google.maps.LatLng('.$coords['latitude'].','.$coords['longitude'].') ';
}
else {
echo 'new google.maps.LatLng('.$coords['latitude'].','.$coords['longitude'].'), ';
}
$x++;
}
/*
for($x = 0; $x<sizeof($trip['trip']['coords']); $x++) {
echo 'new google.maps.LatLng('.$trip['trip']['coords']['latitude'].','.$trip['trip']['coords']['longitude'].'), ';
}*/
echo ']; ';
echo 'var flightPath'.$i.' = new google.maps.Polyline({
path: flightPlanCoordinates'.$i.',
strokeColor: "'.$colour.'",
strokeOpacity: 1.0,
strokeWeight: 4
});';
echo 'flightPath'.$i.'.setMap(map);';
$i++;
}
}
在视图中执行许多逻辑东西并不是很好的风格,因为这会打破MVC模式。只要在控制器或模型中尝试执行它,并尽可能简化视图。 – Young
你想在这里把php变量传入javascript吗?为什么不做你需要在控制器中做的操作,构建一个json对象并将其传递给视图并使用javascript解析它? – Matt