2011-07-04 70 views
3

我有一個SlickGrid的設置,它是從我的數據庫中讀取數據與PHP,我的問題是當我嘗試將數據保存到我的數據庫時,我正在嘗試使用JSON給我一個數組,然後我就可以用它來寫回數據庫,我有看到這個線程說明這一點:用php保存SlickGrid的變化

Saving changes in SlickGrid

所以我在我的代碼隱藏表單元素,並使用JSON編碼的我數據變量,將它賦值給表單上的數據隱藏輸入,這個表單發佈到一個名爲save_price.php的頁面,麻煩的是當我print_r或者var_dump數據變量時,我得到null作爲輸出,我認爲它可能會與我的關係如何我正在使用PHP將內容添加到數據變量中,要麼我正在做一些非常明顯的錯誤,希望你能看到問題出在哪裏,沒有大量關於檢索/保存到數據庫的文檔PHP,所以我有點卡在我的頭撞在牆上,這是我的代碼:這是我的代碼:

回答

4

好吧,所以我發現了問題,只要incase任何人都努力得到這一切工作,這裏是工作代碼,它就會從數據庫中的數據,然後將修改後的數據發送到處理另一個頁面,它孢改進一點點,一旦我得到了這一切實現,這將發生:

<?php 
include("includes/check_session.php"); 
require_once('includes/functions.php'); 
require_once('includes/config.php'); 

$data = ''; 
$i = 0; 

$query = " 
    SELECT * FROM `prices`"; 
$result = mysql_query($query); 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
    $data .= ' 
     data['.$i.'] = { 
      id: "'.$row['id'].'", 
      title: "'.$row['title'].'", 
      duration: "'.$row['duration'].'", 
      percentComplete: "'.$row['percentComplete'].'", 
      start: "'.$row['start'].'", 
      finish: "'.$row['finish'].'", 
      effortDriven: "'.$row['effortDriven'].'" 
     }; 
    '; 

    $i++; 

echo $data; 
} 

?> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset=utf-8> 

    <?php // include("includes/cms_head_scripts.php"); ?> 
    <link rel="stylesheet" href="css/slick.grid.css" type="text/css" media="screen" charset="utf-8" /> 
    <link rel="stylesheet" href="css/smoothness/jquery-ui-1.8.5.custom.css" type="text/css" media="screen" charset="utf-8" /> 
    <link rel="stylesheet" href="css/examples.css" type="text/css" media="screen" charset="utf-8" /> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
    <script language="javascript" src="js/jquery.json.js"></script> 

</head> 
<body> 
    <div id="content_cont"> 

     <div id="main"> 

       <div style="position:relative"> 
        <div style="width:600px;"> 
         <div id="myGrid" style="width:100%;height:500px;"></div> 
        </div> 
       </div> 



      pricing 

     </div><!-- #main --> 

    </div><!-- #content_cont --> 

     <script src="lib/firebugx.js"></script> 


     <script src="lib/jquery-ui-1.8.5.custom.min.js"></script> 
     <script src="lib/jquery.event.drag-2.0.min.js"></script> 

     <script src="slick.core.js"></script> 
     <script src="plugins/slick.cellrangeselector.js"></script> 
     <script src="plugins/slick.cellselectionmodel.js"></script> 
     <script src="slick.editors.js"></script> 
     <script src="slick.grid.js"></script> 


     <script type="text/javascript"> 
      var grid; 
      var data = []; 
      var columns = [ 
       {id:"title", name:"Title", field:"title", editor:TextCellEditor}, 
       {id:"duration", name:"Duration", field:"duration", editor:TextCellEditor}, 
       {id:"%", name:"% Complete", field:"percentComplete", editor:TextCellEditor}, 
       {id:"start", name:"Start", field:"start", editor:TextCellEditor}, 
       {id:"finish", name:"Finish", field:"finish", editor:TextCellEditor}, 
       {id:"effort-driven", name:"Effort Driven", field:"effortDriven", editor:TextCellEditor} 
      ]; 

      var options = { 
        editable: true, 
        enableCellNavigation: true, 
        asyncEditorLoading: false, 
        autoEdit: true 
       }; 

      $(function() { 

       <?php echo $data ?> 
       grid = new Slick.Grid($("#myGrid"), data, columns, options); 

      }) 



     </script> 

<form method="POST" action="save_price.php"> 
    <input type="submit" value="Save"> 
    <input type="hidden" name="data" value=""> 
</form> 
<script type="text/javascript"> 
    $(function() { 
    $("form").submit(
     function() { 
     $("input[name='data']").val($.JSON.encode(data)); 

     } 
    ); 
    }); 
</script> 

</body> 
</html> 
+1

編輯的問題是添加額外信息時的好主意。如果你已經找到了你的問題的答案,那麼只要它是一個解決方案就可以將它作爲答案添加。 – Abizern

+0

@tommy 我得到了同樣的觀點,但我不確定如何處理表單中的數據以更新服務器。你介意發佈你的save_price.php代碼嗎?謝謝。 – tito