【问题标题】:Retrieving database column using JSON使用 JSON 检索数据库列
【发布时间】:2013-06-25 09:29:49
【问题描述】:

我有一个包含 4 列的数据库(id-symbol-name-contractnumber)。所有 4 列及其数据都使用 JSON 显示在用户界面上。

有一个函数负责向数据库添加新列,例如(国家代码)。

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

下面是我显示列的代码。

你能帮帮我吗?

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>

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

【问题讨论】:

    标签: php jquery json


    【解决方案1】:

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

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

    【讨论】:

    • 你能指导我怎么做
    【解决方案2】:

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

    【讨论】:

    • 你能查看我的代码吗?该库已成功添加到数据库中,但无法在用户界面中显示新添加的库。
    【解决方案3】:

    对于 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,您必须自己找到解决方案才能显示动态列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多