未定义的变量,不能声明变量?

时间:2013-08-18 11:18:48

标签: php variables undefined

我得到未定义的变量,我不知道如何将其声明为变量?

Notice: Undefined variable: [$scores_round0],
[$scores_round1],
[$scores_round2],
[$scores_final]

由于

这是我正在使用的功能: -

public function bracketScores($tournID) {

    $maxRounds = $this -> maxRounds($tournID);
    $scores_template = false;

    for($i = 0; $i < $maxRounds - 1; $i++) {
        $scores_template.='[$scores_round'.$i.'],<br>';
    }
    $scores_template.='[$scores_final]';

    return $scores_template;
}

来自班级档案:

$scores_template = $_brackets -> bracketScores($tournID);

从模板文件:

<script type="text/javascript">var autoCompleteData = 
{
  teams : [
    {$brackets}
  ],
  results : 
  [ 
   [ 
    {$$scores_template}
   ]
  ]
}
</script>
<div id="brackets"><div id="autoComplete"></div></div>

1 个答案:

答案 0 :(得分:1)

您不应该提出自己的输出JSON的解决方案 - 只需在数组结构上使用json_encode

function getAutoCompleteData(/* whatever params you need */) {
    // build your structure here
    $data = array(
        'teams'   => array(),
        'results' => array(),
    );

    return $data;
}

然后像这样使用:

<script>
var autoCompleteData = <?php echo json_encode(getAutoCompleteData(/* params */)); ?>;
</script>

或者对于Smarty,将getAutoCompleteData()的结果分配给变量并使用如下:

<script>
var autoCompleteData = {$autoCompleteData|@json_encode};
</script>