Respuesta: Jqgrid Guardar cambios en la base de datos El codigo de con la funcion jqgrid es este.
Código:
jQuery("#navgrid").jqGrid({
url:'editing.php?q=1',
datatype: "xml",
colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Closed','Ship via','Notes'],
colModel:[
{name:'id',index:'id', width:55,editable:false,editoptions:{readonly:true,size:10}},
{name:'invdate',index:'invdate', width:80,editable:true,editoptions:{size:10}},
{name:'name',index:'name', width:90,editable:true,editoptions:{size:25}},
{name:'amount',index:'amount', width:60, align:"right",editable:true,editoptions:{size:10}},
{name:'tax',index:'tax', width:60, align:"right",editable:true,editoptions:{size:10}},
{name:'total',index:'total', width:60,align:"right",editable:true,editoptions:{size:10}},
{name:'closed',index:'closed',width:55,align:'center',editable:true,edittype:"checkbox",editoptions:{value:"Yes:No"}},
{name:'ship_via',index:'ship_via',width:70, editable: true,edittype:"select",editoptions:{value:"FE:FedEx;TN:TNT"}},
{name:'note',index:'note', width:100, sortable:false,editable: true,edittype:"textarea", editoptions:{rows:"2",cols:"20"}}
],
rowNum:10,
rowList:[10,20,30],
pager: '#pagernav',
sortname: 'id',
viewrecords: true,
sortorder: "desc",
caption:"Navigator Example",
editurl:"someurl.php",
height:210
});
jQuery("#navgrid").jqGrid('navGrid','#pagernav',
{}, //options
{height:350,reloadAfterSubmit:true}, // edit options
{height:350,reloadAfterSubmit:true}, // add options
{reloadAfterSubmit:true}, // del options
{} // search options
);
y el codio php que hace la iteracion con la base de datos es este. Código PHP: <?php
include("dbconfig.php");
$examp = $_REQUEST["q"]; //query number
$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) $sidx =1;
// search options
// IMPORTANT NOTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// this type of constructing is not recommendet
// it is only for demonstration
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$wh = "";
$searchOn = Strip($_REQUEST['_search']);
if($searchOn=='true') {
$fld = Strip($_REQUEST['searchField']);
if( $fld=='id' || $fld =='invdate' || $fld=='name' || $fld=='amount' || $fld=='tax' || $fld=='total' || $fld=='note' || $fld=='closed' || $fld=='ship_via') {
$fldata = Strip($_REQUEST['searchString']);
$foper = Strip($_REQUEST['searchOper']);
// costruct where
$wh .= " AND ".$fld;
switch ($foper) {
case "bw":
$fldata .= "%";
$wh .= " LIKE '".$fldata."'";
break;
case "eq":
if(is_numeric($fldata)) {
$wh .= " = ".$fldata;
} else {
$wh .= " = '".$fldata."'";
}
break;
case "ne":
if(is_numeric($fldata)) {
$wh .= " <> ".$fldata;
} else {
$wh .= " <> '".$fldata."'";
}
break;
case "lt":
if(is_numeric($fldata)) {
$wh .= " < ".$fldata;
} else {
$wh .= " < '".$fldata."'";
}
break;
case "le":
if(is_numeric($fldata)) {
$wh .= " <= ".$fldata;
} else {
$wh .= " <= '".$fldata."'";
}
break;
case "gt":
if(is_numeric($fldata)) {
$wh .= " > ".$fldata;
} else {
$wh .= " > '".$fldata."'";
}
break;
case "ge":
if(is_numeric($fldata)) {
$wh .= " >= ".$fldata;
} else {
$wh .= " >= '".$fldata."'";
}
break;
case "ew":
$wh .= " LIKE '%".$fldata."'";
break;
case "ew":
$wh .= " LIKE '%".$fldata."%'";
break;
default :
$wh = "";
}
}
}
// connect to the database
$db = mysql_connect($dbhost, $dbuser, $dbpassword)
or die("Connection Error: " . mysql_error());
mysql_select_db($database) or die("Error conecting to db.");
switch ($examp) {
case 1:
$result = mysql_query("SELECT COUNT(*) AS count FROM invheader a, clients b WHERE a.client_id=b.client_id ".$wh);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
if( $count >0 ) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit; //do not put $limit*($page - 1)
if ($start<0) $start = 0;
$SQL = "SELECT a.id, a.invdate, b.name, a.amount,a.tax,a.total,a.note,a.closed,a.ship_via FROM invheader a, clients b WHERE a.client_id=b.client_id".$wh." ORDER BY ".$sidx." ". $sord." LIMIT ".$start." , ".$limit;
$result = mysql_query( $SQL ) or die("Couldn t execute query.".mysql_error());
if ( stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml") ) {
header("Content-type: application/xhtml+xml;charset=utf-8"); } else {
header("Content-type: text/xml;charset=utf-8");
}
$et = ">";
$s = "<?xml version='1.0' encoding='utf-8'?$et\n";
$s .= "<rows>";
$s .= "<page>".$page."</page>";
$s .= "<total>".$total_pages."</total>";
$s .= "<records>".$count."</records>";
// be sure to put text data in CDATA
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$s .= "<row id='". $row[id]."'>";
$s .= "<cell>". $row[id]."</cell>";
$s .= "<cell>". $row[invdate]."</cell>";
$s .= "<cell><![CDATA[". $row[name]."]]></cell>";
$s .= "<cell>". $row[amount]."</cell>";
$s .= "<cell>". $row[tax]."</cell>";
$s .= "<cell>". $row[total]."</cell>";
$s .= "<cell>". $row[closed]."</cell>";
if( $row[ship_via] == 'TN') $s .= "<cell>TNT</cell>";
else if( $row[ship_via] == 'FE') $s .= "<cell>FedEx</cell>";
else $s .= "<cell></cell>";
$s .= "<cell><![CDATA[". $row[note]."]]></cell>";
$s .= "</row>";
}
$s .= "</rows>";
echo $s;
break;
}
mysql_close($db);
function Strip($value)
{
if(get_magic_quotes_gpc() != 0)
{
if(is_array($value))
if ( array_is_associative($value) )
{
foreach( $value as $k=>$v)
$tmp_val[$k] = stripslashes($v);
$value = $tmp_val;
}
else
for($j = 0; $j < sizeof($value); $j++)
$value[$j] = stripslashes($value[$j]);
else
$value = stripslashes($value);
}
return $value;
}
function array_is_associative ($array)
{
if ( is_array($array) && ! empty($array) )
{
for ( $iterator = count($array) - 1; $iterator; $iterator-- )
{
if ( ! array_key_exists($iterator, $array) ) { return true; }
}
return ! array_key_exists(0, $array);
}
return false;
}
?> Importante: este codigo es el que viene con los ejemplos del jqgrid. |