使用JSON检索数据库列 [英] Retrieving database column using JSON

查看:112
本文介绍了使用JSON检索数据库列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a database consist of 4 columns (id-symbol-name-contractnumber). All 4 columns with their data are being displayed on the user interface using JSON.

有一个函数负责将新列添加到数据库中,例如(国家代码).

There is a function which is responisble to add new column to the database e.g (countrycode).

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

The coulmn is added successfully to the database BUT not able to show the new added coulmn in the user interface.

下面是我的显示列的代码.

Below is my code that is displaying the columns.

你能帮我吗?

table.php

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

推荐答案

对于php端,如果在while内使用循环,则应很容易为动态字段创建数组.

For php side it should be easy to make array for dynamic fields if you use loop inside 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,您必须为自己找到解决方案以显示动态列.

However for jqgrid You must find solution for yourself to show dynamic columns.

这篇关于使用JSON检索数据库列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆