使用JSON检索数据库列

时间:2013-06-25 09:29:50

标签: php jquery json

我有一个由4列组成的数据库(id-symbol-name-contractnumber)。所有带有数据的4列都使用JSON显示在用户界面上。

有一个功能可以响应将新列添加到数据库,例如(countrycode)。

coulmn已成功添加到数据库但未能在用户界面中显示新添加的coulmn。

下面是我显示列的代码。

你能帮助我吗?

table.php

        <!DOCTYPE html>
        <html lang="en">
        <head>

<link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
<script type="text/javascript" src="../../scripts/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
<script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script> 
<script type="text/javascript" src="../../jqwidgets/jqxgrid.filter.js"></script>        
<script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>   
<script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script>    
<script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script>   
<script type="text/javascript" src="../../scripts/gettheme.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    // prepare the data
    var theme = getDemoTheme();

    var source =
    {
        datatype: "json",
    datafields: [
                 { name: 'id' },
                 { name: 'symbol' },
                 { name: 'name' },

                 { name: 'contractnumber' }
            ],
        url: 'data.php',
        filter: function()
        {
            // update the grid and send a request to the server.
            $("#jqxgrid").jqxGrid('updatebounddata', 'filter');
        },
        cache: false
    };      
    var dataAdapter = new $.jqx.dataAdapter(source);

    // initialize jqxGrid
    $("#jqxgrid").jqxGrid(
    {       
        source: dataAdapter,
        width: 670,
        theme: theme,
        showfilterrow: true,
        filterable: true,
        columns: [
            { text: 'id', datafield: 'id', width: 200 },
            { text: 'symbol', datafield: 'symbol', width: 200 },
            { text: 'name', datafield: 'name', width: 100 },
            { text: 'contractnumber', filtertype: 'list', datafield: 'contractnumber' }
        ]
    });
    });
</script>
       </head>
           <body class='default'>
        <div id="jqxgrid"></div>
        </div>
            </body>
       </html>

data.php

      <?php
#Include the db.php file
include('db.php');
#Connect to the database
//connection String
$connect = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die('Could not connect: ' . mysql_error());
//Select The database
$bool = mysql_select_db($mysql_database, $connect);
if ($bool === False){
   print "can't find $database";
}

$query = "SELECT * FROM pricelist";

$result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
$orders = array();
// get data and store in a json array
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $orders[] = array(
        'id' => $row['id'],
        'symbol' => $row['symbol'],
        'name' => $row['name'],

        'contractnumber' => $row['contractnumber']

      );
}

echo json_encode($orders);
  ?>

3 个答案:

答案 0 :(得分:0)

向数据库添加新列时,您还必须以某种方式更新UI。为此,您必须将新数据字段添加到源对象的datafields数组中,并通过设置columns属性来更新Grid的列。

$(“#jqxgrid”)。jqxGrid({columns:newColumnsArray});

答案 1 :(得分:0)

您说“contrycode”列已正确附加到pricelist表吗?因此,在$order中获取列值并将它们显示为JSON并不是很困难。如果不考虑代码,我会从...开始......

# in your PHP
$orders[] = array(
    'countrycode' = $row['countrycode'],
    'id' => $row['id'],
  ...

# in your JS
    columns: [
        { text: 'countrycode', datafield: 'countrycode', width: 2 },
        { text: 'id', datafield: 'id', width: 200 },
  ...

答案 2 :(得分:0)

对于php方面,如果在内部使用循环,则应该很容易为动态字段制作数组。

$orders = array(); $i  = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $orders[$i] = array();
    foreach($row as $col => $value) {
        $orders[$i][$col] = $value; 
    }
    $i++;
}

但是对于jqgrid你必须找到自己的解决方案来显示动态列。