ajax查询不返回json数组

时间:2012-02-14 02:45:14

标签: php ajax json function

我有以下由jquery ajax

调用的php脚本
    function report_range($time, $ht, $start_date, $end_date) {
        $query = mysql_query("SELECT type, count(*) FROM tracking WHERE htcode = '$ht' AND type IN ('viewed', 'shared', 'printed', 'emailed', 'used') AND date >= '$start_date' AND date <= '$end_date' GROUP BY type");
        while ($result = mysql_fetch_assoc($query)){
            switch ($result['type']){
                case 'viewed':
                    $viewed = $result['count(*)'];
                    break;
                case 'shared':
                    $shared = $result['count(*)'];
                    break;
                case 'used':
                    $used = $result['count(*)'];
                    break;
                case 'emailed':
                    $emailed = $result['count(*)'];
                    break;
                case 'printed':
                    $printed = $result['count(*)'];
                    break;
            }

        }
        //build the table rows
        return '<tr><td>'.$time.'</td><td>'.$viewed.'</td><td>'.$shared.'</td><td>'.$used.'</td><td>'.$printed.'</td><td>'.$emailed.'</td></tr>'; 
//this is where the problem is, i dont think i am returning the value right.

    }

//创建要发送到json的变量

    $stat_1 = report_range('Today', '1672627', date('Y-m-d'), date('Y-m-d'));
    $stat_2 = report_range('Yesterday', $_POST['htcode'], date('Y-m-d', strtotime('yesterday')), date('Y-m-d', strtotime('yesterday')));
    $stat_3 = report_range('Past 30 Days', $_POST['htcode'], date('Y-m-d', strtotime('30 days ago')), date('Y-m-d'));
    $stat_4 = report_range('All Time', $_POST['htcode'], date('Y-m-d', strtotime('3600 days ago')), date('Y-m-d'));

//创建json数组

echo json_encode(array(
        'stat_1'=>$live_1,
        'stat_2'=>$live_2,
        'stat_3'=>$live_3,
        'stat_4'=>$live_4
    ));

我似乎无法获得一个包含我在函数中构建的表行的四个变量的json数组。

jquery正在工作,因为它显示了空值。

1 个答案:

答案 0 :(得分:3)

$ live_1等不应该是$ stat_1吗?

echo json_encode(array(
        'stat_1'=>$stat_1,
        'stat_2'=>$stat_2,
        'stat_3'=>$stat_3,
        'stat_4'=>$stat_4
    ));