使用JSON检索数据库列


Retrieving database column using JSON

我有一个由4列组成的数据库(id符号名称contractnumber)。所有4列及其数据都使用JSON显示在用户界面上。

有一个函数负责向数据库中添加新列,例如(countrycode)。

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

下面是我显示列的代码。

你能帮我吗?

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

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

$("#jqxgrid").jqxgrid({columns:newColumnsArray});

您说过"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 },
  ...

对于php端,如果在while内部使用循环,那么应该可以很容易地为动态字段创建数组。

$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,您必须为自己找到显示动态列的解决方案。