ผมเพิ่มข้อมูลลง database โดยใช้ jqgrid ไม่ได้ครับ
ไฟล์ demo.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>jQuery Demo 2</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.8.6.custom.css" />
<script src="js/jquery.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
</head>
<body>
<table id="single"></table>
<div id="psingle" ></div>
</body>
</html>
<script type="text/javascript">
jQuery("#single").jqGrid
({ url:'test_jq.php?q=2',
datatype: "json",
height: 255,
width: 600,
colNames:['Index','Name', 'Code'],
colModel:[ {name:'item_id',index:'item_id', width:65, sorttype:'int'},
{name:'item_name',index:'item_name', width:150, editable:true},
{name:'item_code',index:'item_code', width:100} ],
rowNum:50, rowTotal: 2000, rowList : [20,30,50],
loadonce:true,
/*mtype: "GET",*/
rownumbers: true,
rownumWidth: 40,
gridview: true,
pager: '#psingle',
sortname: 'item_id',
editurl: 'localset.php',
viewrecords: true, sortorder: "asc",
caption: "Single search on local data"
}
);
//jQuery("#single").jqGrid('navGrid','#psingle',{del:false,add:false,edit:false});
jQuery("#single").jqGrid('navGrid','#psingle',{});
</script>
ไฟล์ test_jq.php
<?php
// connect to the database
$db = mysql_pconnect("localhost","root", "12345")
or die("Connection Error: " . mysql_error());
mysql_select_db("test_table") or die("Error conecting to db.");
if(isset($_GET['edit'])){
$compile = ture;
}
if(!$compile){
$page = $_REQUEST['page']; // get the requested page
$limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
$sidx = $_REQUEST['sidx']; // get index row - i.e. user click to sort
$sord = $_REQUEST['sord']; // get the direction if(!$sidx)
if(!$sidx) $sidx =1;
$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
if($totalrows) {
$limit = $totalrows;
}
//populateDBRandom();
$result = mysql_query("SELECT COUNT(item_id) AS counts FROM items");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['counts'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else { $total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
//$SQL = "SELECT item_id, item_name, item_code FROM items";
$SQL = "SELECT item_id, item_name, item_code FROM items ".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn?t execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]['id']=$row[item_id];
$responce->rows[$i]['cell']=array($row[item_id],$row[item_name],$row[item_code]);
$i++;
} echo json_encode($responce);
mysql_close($db);
}
if(isset($_GET['edit'])){
mysql_query("UPDATE items SET item_name='".$_REQUEST['item_name']."' WHERE item_id='".$_REQUEST['item_id']."'");
}
?>
ผมไม่ทราบว่า jqgrid มันไม่ส่งค่า GET เข้าฐานข้อมูลไม่ได้ ถ้าใครมีตัวอย่าง ผมขอตัวอย่างมาศึกษาหน่อยครับ ขอบคุณครับ
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>jQuery Demo 2</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/ui-lightness/jquery-ui-1.8.6.custom.css" />
<script src="js/jquery.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
</head>
<body>
<table id="single"></table>
<div id="psingle" ></div>
</body>
</html>
<script type="text/javascript">
jQuery("#single").jqGrid
({ url:'test_jq.php?q=2',
datatype: "json",
height: 255,
width: 600,
colNames:['Index','Name', 'Code'],
colModel:[ {name:'item_id',index:'item_id', width:65, sorttype:'int'},
{name:'item_name',index:'item_name', width:150, editable:true},
{name:'item_code',index:'item_code', width:100} ],
rowNum:50, rowTotal: 2000, rowList : [20,30,50],
loadonce:true,
/*mtype: "GET",*/
rownumbers: true,
rownumWidth: 40,
gridview: true,
pager: '#psingle',
sortname: 'item_id',
editurl: 'localset.php',
viewrecords: true, sortorder: "asc",
caption: "Single search on local data"
}
);
//jQuery("#single").jqGrid('navGrid','#psingle',{del:false,add:false,edit:false});
jQuery("#single").jqGrid('navGrid','#psingle',{});
</script>
ไฟล์ test_jq.php
<?php
// connect to the database
$db = mysql_pconnect("localhost","root", "12345")
or die("Connection Error: " . mysql_error());
mysql_select_db("test_table") or die("Error conecting to db.");
if(isset($_GET['edit'])){
$compile = ture;
}
if(!$compile){
$page = $_REQUEST['page']; // get the requested page
$limit = $_REQUEST['rows']; // get how many rows we want to have into the grid
$sidx = $_REQUEST['sidx']; // get index row - i.e. user click to sort
$sord = $_REQUEST['sord']; // get the direction if(!$sidx)
if(!$sidx) $sidx =1;
$totalrows = isset($_REQUEST['totalrows']) ? $_REQUEST['totalrows']: false;
if($totalrows) {
$limit = $totalrows;
}
//populateDBRandom();
$result = mysql_query("SELECT COUNT(item_id) AS counts FROM items");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['counts'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else { $total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
if ($limit<0) $limit = 0;
$start = $limit*$page - $limit; // do not put $limit*($page - 1)
if ($start<0) $start = 0;
//$SQL = "SELECT item_id, item_name, item_code FROM items";
$SQL = "SELECT item_id, item_name, item_code FROM items ".$where." ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn?t execute query.".mysql_error());
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$i=0;
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$responce->rows[$i]['id']=$row[item_id];
$responce->rows[$i]['cell']=array($row[item_id],$row[item_name],$row[item_code]);
$i++;
} echo json_encode($responce);
mysql_close($db);
}
if(isset($_GET['edit'])){
mysql_query("UPDATE items SET item_name='".$_REQUEST['item_name']."' WHERE item_id='".$_REQUEST['item_id']."'");
}
?>
ผมไม่ทราบว่า jqgrid มันไม่ส่งค่า GET เข้าฐานข้อมูลไม่ได้ ถ้าใครมีตัวอย่าง ผมขอตัวอย่างมาศึกษาหน่อยครับ ขอบคุณครับ
ผมอัดไฟล์ที่จำเป็นไว้ ตามเว็บนี้ครับ
http://www.mediafire.com/?7i5fcwoe3zur3o2