0

I have the following php script that is called by jquery ajax

    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.

    }

//create the variables to send to 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'));

//create the json array

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

i can not seem to get a json array with the four variables containing the table rows i am building in the function.

The jquery is working, as it shows the null values.

2
  • can you post your jquery funciton. Also change your $live_x vars to $stat_x vars Commented Feb 14, 2012 at 2:48
  • You're not declaring the $live* variables. Commented Feb 14, 2012 at 2:49

1 Answer 1

3

Shouldn't that $live_1 etc var be $stat_1?

echo json_encode(array(
        'stat_1'=>$stat_1,
        'stat_2'=>$stat_2,
        'stat_3'=>$stat_3,
        'stat_4'=>$stat_4
    ));
Sign up to request clarification or add additional context in comments.

1 Comment

well crap.. am i embarrassed.. I have been doing this too long today. Thanks for that.... Let that be a lesson to the rest of you. When you reformat your code, check your variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.