Raghavendra
Raghavendra

Reputation: 3580

Script not printing with php

when i am using like this in html is script is working fine

{category: "Auto", measure: 6600},

    {category: "Best Car", measure: 22200},

    {category: "Car Auction", measure: 5400},

    {category: "Car Audio", measure: 60500},

    {category: "Car Battery", measure: 6600},

but when i am trying to print this with php like this

<?php foreach($rows as $row){ ?>    
        {category: "<?php echo $row['ad_group']; ?>", measure: <?php echo $row['volume']; ?>},
    <?php } ?>

it is not working.

Upvotes: 0

Views: 121

Answers (1)

srain
srain

Reputation: 9082

You can put all the data into a $list and then use json_encode() to encode it. This will also make sure your values are properly escaped.

<?php
$list = array();
foreach ($rows as $row)
{
    $data = array();
    $data['category'] = $row['ad_group'];
    $data['measure'] = $row['volume'];
    $list[] = $data;
}
echo json_encode($list);
?>

See also: http://php.net/json_encode

Upvotes: 2

Related Questions