没有数据&在IF语句中使用函数时没有错误

时间:2014-11-19 11:15:16

标签: php

在if语句中使用函数时,我无法使php脚本工作。

目前运行的代码,但没有给我任何信息,也没有任何错误,如果我删除了IF'它工作正常,但我正在处理的项目,这是必不可少的。

这是我的代码 - 我已经拿出SQL来节省空间..

if ($VAR === 'PK%') {


    CDB::UseDB('blah', 'blah', 'blah', 'blah');
    $sql = "blah blah
            ";

    $lrs = CDB::ExecuteQuery($sql);

    if ($lrs) {
        $jsonData = convert($lrs);

        function convert($lrs)
        {
            // RE-GET VARIABLE AS IT CAN'T GET IT FROM OUTSIDE OF FUNCTION
            $VAR = $_GET['VARIABLE'];
            $intermediate = array();
            while ($vals = CDB::GetAssoc($lrs)) {
                $key = $vals['VAR'];
                $y = $vals['MEASURE_1'];
                if (!isset($intermediate[$key])) $intermediate[$key] = array();
                $intermediate[$key][] = array('x' => count($intermediate[$key]), 'y' => $y);


            }


            $output = array();

            foreach ($intermediate as $key => $values) {
                $output[] = array(
                    "key" => $key,
                    'values' => $values
                );
            }

            return json_encode($output, JSON_NUMERIC_CHECK);


        }

    }
}

有人能说清楚我做错了吗?

1 个答案:

答案 0 :(得分:0)

You are defining your function in a conditional way。在这种情况下,必须在被调用之前处理它的定义。

我建议单独声明你的功能,然后使用它,如:

function convert($lrs) {
    // RE-GET VARIABLE AS IT CAN'T GET IT FROM OUTSIDE OF FUNCTION
    $VAR = $_GET['VARIABLE'];
    $intermediate = array();

    while ($vals = CDB::GetAssoc($lrs)) {
        $key = $vals['VAR'];
        $y = $vals['MEASURE_1'];
        if (!isset($intermediate[$key])) $intermediate[$key] = array();
            $intermediate[$key][] = array('x' => count($intermediate[$key]), 'y' => $y);
    }


    $output = array();

    foreach ($intermediate as $key => $values) {
        $output[] = array(
            "key" => $key,
            'values' => $values
        );
    }

        return json_encode($output, JSON_NUMERIC_CHECK);
}

if ($VAR === 'PK%') {
    CDB::UseDB('blah', 'blah', 'blah', 'blah');
    $sql = "blah blah";

    $lrs = CDB::ExecuteQuery($sql);

    if ($lrs) {
        $jsonData = convert($lrs);
    }
}