charlie
charlie

Reputation: 1384

put JS code in a PHP while loop

i have some JS Code for displaying graph data:

series: [{
                name: 'Year 1800',
                data: [107, 31, 635, 203, 2]
            }, {
                name: 'Year 1900',
                data: [133, 156, 947, 408, 6]
            }, {
                name: 'Year 2008',
                data: [973, 914, 4054, 732, 34]
            }]

i need to get the data to display in a while loop in PHP. I have tried this:

<?php
                $sql="SELECT *, COUNT(category) AS my_groupcount from tickets where deleted = '' and DAY(datetime) = '".$day."' and MONTH(datetime) = '".$month."' and YEAR(datetime) = '".$year."' group by category order by datetime ASC ";
                $rs=mysql_query($sql,$conn);
                while($result=mysql_fetch_array($rs))
                {
                    echo "name: 'Cat ".$result["category"]."',";
                    echo "data: [".$result["my_groupcount"]."]";
                    echo "}, {";
                }
                ?>

i need to group the category column in the tickets table and display the graphs for each category but its not working - i think its because in the while loop its ending with }, { but i need it to end with }]

how can i get round this - the amount of category items in the tickets table is changing all the time as users are able to add/delete categories.

Upvotes: 1

Views: 149

Answers (4)

Kamil Szot
Kamil Szot

Reputation: 17817

<?php
            $sql="SELECT *, COUNT(category) AS my_groupcount from tickets where deleted = '' and DAY(datetime) = '".$day."' and MONTH(datetime) = '".$month."' and YEAR(datetime) = '".$year."' group by category order by datetime ASC ";
            $rs=mysql_query($sql,$conn);
            $first = true;
            echo 'series: [{';
            while($result=mysql_fetch_array($rs))
            {
                if(!$first) {
                    echo "}, {";
                } else {
                    $first = false;
                }
                echo "name: 'Cat ".$result["category"]."',";
                echo "data: [".$result["my_groupcount"]."]";
            }
            echo '}]';
?>

Upvotes: 1

user771071
user771071

Reputation:

Why not do it like this:

<?php
    $sql = "[.. SQL Statement ..]";
    $rs = mysql_query($sql, $conn);
    $json = array();

    while($result = mysql_fetch_array($rs)) {
        $json[] = array( 
            'name' => 'Cat '. $result['category'],

            // This does assume that my_groupcount is an array with numbers
            // i.e. array(1, 34, 54, 345)
            // If not, you'll have to make it an array by doing:
            // explode(', ', $result['my_groupcount'])
            // This however does assume that the numbers are in 
            // the "12, 23" format
            'data' => $result['my_groupcount'],
        );
    }             

    echo json_encode($json);

Upvotes: 4

mic
mic

Reputation: 1273

try

$sql="SELECT *, COUNT(category) AS my_groupcount from tickets where deleted = '' and DAY(datetime) = '".$day."' and MONTH(datetime) = '".$month."' and YEAR(datetime) = '".$year."' group by category order by datetime ASC ";
$rs=mysql_query($sql,$conn);
$output = '[';
while($result=mysql_fetch_array($rs))
{
    $output .= "name: 'Cat ".$result["category"]."',";
    $output .= "data: [".$result["my_groupcount"]."]";
    $output .= "}, {";
}

$output = substr($output, 0, -3) . ']';

but as rich said you really shouldn't be writing JSON by hand.

Upvotes: 0

aynber
aynber

Reputation: 23011

Bookend the brackets instead, though it's better to build it then echo it so you can get rid of the last comma.

$string = '';
while($result=mysql_fetch_array($rs))
{
    string.= "{";
    string.= "name: 'Cat ".$result["category"]."',";
    string.= "data: [".$result["my_groupcount"]."]";
    string.= "},";
}
$string = trim($string,','); // gets rid of that last comma

echo "[$string]";

Upvotes: 0

Related Questions