2013-06-25 88 views
0

我有一個由4列(id-symbol-name-contractnumber)組成的數據庫。所有4列及其數據都使用JSON顯示在用戶界面上。使用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); 
    ?> 

回答

0

當您添加一個新列到你的數據庫,你將不得不以某種方式更新UI,太。爲此,您必須向源對象的datafields數組添加一個新的數據字段,並通過設置columns屬性來更新Grid的列。 (「#jqxgrid」)。jqxGrid((columns:newColumnsArray});

+0

你能指導我怎麼做 – arokia

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 }, 
    ... 
+0

你可以看看我的代碼。coulmn被成功添加到數據庫,但不能顯示新的用戶界面中添加coulmn。 – arokia

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您必須找到自己的解決方案,以顯示動態列。