用php保存SlickGrid中的变化 [英] Saving Changes in SlickGrid with php

查看:164
本文介绍了用php保存SlickGrid中的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个SlickGrid,它使用PHP从我的数据库中读取数据,当我尝试将数据保存回数据库时,出现问题,我尝试使用JSON为我提供一个数组,然后使用回写到数据库,我已经看到这个线程解释这个:





因此,我在我的代码中隐藏了表单元素,并使用JSON对数据进行编码变量,将它赋值给表单上的数据隐藏输入,这个表单发布到一个名为save_price.php的页面,麻烦的是当我print_r或者var_dump数据变量时,我得到null作为输出,我想这可能是与我如何使用PHP将内容添加到数据变量有关,或者我正在做一些非常明显错误的事情,希望您可以看到问题所在,没有大量关于检索的文档/保存到一个数据库与PHP,所以我有点卡在我的头撞在墙上,这是我的代码: 好的所以我发现了这个问题,只是为了避免任何人努力使这一切工作,这里是工作代码,它从数据库获取数据,然后将更改后的数据发送到另一个页面进行处理,它需要一点改进,一旦完全实现,就会发生:

 <?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 =stylesheethref =css / examples.csstype =text / cssmedia =screencharset =utf-8/>
< script src =http://code.jquery.com/jquery-latest.js>< / script>
< script language =javascriptsrc =js / jquery.json.js>< / script>

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

< div id =main>

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



定价

< / 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: TextCellEditor},
{id:%,name:%Complete,field:percentComplete,编辑:TextCellEditor},
{id: 开始,名称:开始,字段:开始,编辑器:TextCellEditor,
{id:finish,名称:Finish,字段:finish,编辑器:TextCellEditor,
{id:努力驱动,名称:Effort Driven,字段:effortDriven,编辑:TextCellEditor}
];

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

$(function(){

<?php echo $ data?>
grid = new Slick.Grid($(#myGrid) ,数据,列,选项);

})



< / script>

< form method =POSTaction =save_price.php>
< input type =submitvalue =Save>
< input type =hiddenname =datavalue =>
< / form>
< script type =text / javascript>
$(function(){
$(form)。submit(
function(){
$(input [name ='data'])。val ($ .JSON.encode(data));

}
);
});
< / script>

< / body>
< / html>


I have a SlickGrid set up, it is reading data from my database with PHP, my problem is arising when i try to save the data back to my database, I am trying to use JSON to give me an array that I can then use to write back to the database, i have see this thread explaining this:

Saving changes in SlickGrid

So I have the hidden form element in my code, and am using JSON to encode the data variable, the assign it to the data hidden input on the form, this form posts to a page called save_price.php, the trouble is when I print_r, or var_dump the data variable, I get null as an output, I think it might be something to do with how I am using PHP to add the content into the data variable, either that or I am doing something really obviously wrong, hopefully you can see what the problem is, there isn't a great deal of documentation online about retrieving/saving to a db with PHP, so I'm kinda stuck banging my head against the wall on this one, here's my code:

解决方案

Ok so I found the problem, just incase anyone is struggling to get this all to work, here is the working code, it gets data from a database, then sends the changed data to another page for processing, it nees a little bit of refinements, that will happen once I've got it all implemented:

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

这篇关于用php保存SlickGrid中的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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