Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将JS代码放入PHP while循环中_Javascript_Php - Fatal编程技术网

Javascript 将JS代码放入PHP while循环中

Javascript 将JS代码放入PHP while循环中,javascript,php,Javascript,Php,我有一些显示图形数据的JS代码: series: [{ name: 'Year 1800', data: [107, 31, 635, 203, 2] }, { name: 'Year 1900', data: [133, 156, 947, 408, 6] }, { name: 'Year

我有一些显示图形数据的JS代码:

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]
            }]
我需要在PHP中获取数据以在while循环中显示。我试过这个:

<?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 "}, {";
                }
                ?>

将括号括起来,不过最好先构建它,然后再回显它,这样就可以去掉最后一个逗号

$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]";

将括号括起来,不过最好先构建它,然后再回显它,这样就可以去掉最后一个逗号

$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]";

为什么不这样做:

<?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);

为什么不这样做:

<?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);
试试看

但正如rich所说,你真的不应该手工编写JSON。

试试看

<?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 '}]';
?>
但正如rich所说,您真的不应该手工编写JSON。


<?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 '}]';
?>


查看PHPs
json\u encode
。你真的不应该手工编写json——如果类别被称为
New和/或'chic'
,就像我最近所说的那样……看看PHPs
json\u encode
。你真的不应该手工编写json——如果类别被称为
New和/或'chic'
,就像我最近所说的那样……我想在这里补充一点:你在例子中显示的json是无效的,因为它在json解析器中无法很好地验证。如果您想自己检查JSON验证,请看这里:我想在这里添加一个简短的注释:您在示例中显示的JSON无效,因为它在JSON解析器中无法很好地验证。如果您想自己检查JSON验证,请查看以下内容: