通过表中的自定义变量值创建json字符串

时间:2013-09-30 14:35:22

标签: php json

我已经检查了类似的答案。但我在这里有不同的问题!

创建googlce char我通过从表中获取数据来创建json字符串。 表只有两行。 以下是json字符串的构造方式:

 $result = $mysqli->query('SELECT * FROM view_name');

   $p= $mysqli->query('SELECT Index_val FROM view_name where ind_type=pcount');
 //echo "$p";

  $rows = array();
  $table = array();
  $table['cols'] = array(
    array('label' => 'pcount', 'type' => 'string'),
    array('label' => 'ncount', 'type' => 'number')
);
    /* Extract the information from $result */
    foreach($result as $r) {
      $temp = array();
      $temp[] = array('v' => (string) $r['ind_type']); 
      $temp[] = array('v' => (int) $r['Index_val']); 
      $rows[] = array('c' => $temp);
    }

$table['rows'] = $rows;

此处我只需要从数据库pcountncount获取两个值。我不想使用上述程序。

我想要的是手动从表中获取pcountncount的值,存储在变量中并构造json字符串。

像这样: $p= $mysqli->query('SELECT Index_val FROM view_name where ind_type=pcount'); //如果错误,请更正 以同样的方式得到ncount

现在如何从中创建上面的json字符串? 结果字符串将是这样的:

{
    "cols":[
        {"label":"pcount","type":"string"},
        {"label":"ncount","type":"number"}],
    "rows":[
        {"c":
            [
            {"v":"pcount"},
            {"v":179}  // 179 is pcount
            ]
        },
        {"c":
            [
            {"v":"ncount"},
            {"v":285} //285 is ncount
            ]
        }
        ]
}

0 个答案:

没有答案