Advertisement
Guest User

Edit/Update Options

a guest
Mar 26th, 2015
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.32 KB | None | 0 0
  1. <?php
  2. /**
  3.  * PHP Grid Component
  4.  *
  5.  * @author Abu Ghufran <gridphp@gmail.com> - http://www.phpgrid.org
  6.  * @version 1.5.2
  7.  * @license: see license.txt included in package
  8.  */
  9.  
  10. // include db config
  11. include_once("../../config.php");
  12.  
  13. // set up DB
  14. mysql_connect(PHPGRID_DBHOST, PHPGRID_DBUSER, PHPGRID_DBPASS);
  15. mysql_select_db(PHPGRID_DBNAME);
  16.  
  17. // include and create object
  18. include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
  19.  
  20. // you can customize your own columns ...
  21.  
  22. $col = array();
  23. $col["title"] = "Id"; // caption of column
  24. $col["name"] = "id"; // grid column name, must be exactly same as returned column-name from sql (tablefield or field-alias)
  25. $col["width"] = "10";
  26. # $col["hidden"] = true; // hide column by default
  27. $cols[] = $col;    
  28.  
  29. $col = array();
  30. $col["title"] = "Client";
  31. $col["name"] = "name";
  32. $col["width"] = "100";
  33. $col["editable"] = true; // this column is not editable
  34. $col["align"] = "center"; // this column is not editable
  35. $col["search"] = false; // this column is not searchable
  36. $cols[] = $col;
  37.  
  38. $col = array();
  39. $col["title"] = "Company";
  40. $col["name"] = "company";
  41. $col["width"] = "100";
  42. $col["editable"] = true; // this column is not editable
  43. $col["align"] = "center"; // this column is not editable
  44. $col["search"] = false; // this column is not searchable
  45. $cols[] = $col;
  46.  
  47.  
  48. $col = array();
  49. $col["title"] = "Note";
  50. $col["name"] = "note";
  51. # $col["width"] = "300"; // not specifying width will expand to fill space
  52. $col["sortable"] = false; // this column is not sortable
  53. $col["search"] = false; // this column is not searchable
  54. $col["editable"] = true;
  55. $col["edittype"] = "textarea"; // render as textarea on edit
  56. $col["editoptions"] = array("rows"=>2, "cols"=>20); // with these attributes
  57. $col["editrules"] = array("edithidden"=>true);
  58. $cols[] = $col;
  59.  
  60. $col = array();
  61. $col["title"] = "Date";
  62. $col["name"] = "invdate";
  63. $col["width"] = "50";
  64. $col["editable"] = true; // this column is editable
  65. $col["editoptions"] = array("size"=>20); // with default display of textbox with size 20
  66. $col["editrules"] = array("required"=>true); // required:true(false), number:true(false), minValue:val, maxValue:val
  67. $col["formatter"] = "datetime"; // format as date
  68. $cols[] = $col;
  69.        
  70.        
  71. $col = array();
  72. $col["title"] = "Total";
  73. $col["name"] = "total";
  74. $col["width"] = "50";
  75. $col["editable"] = true;
  76. // default render is textbox
  77. $col["editoptions"] = array("value"=>'10');
  78.  
  79. $cols[] = $col;
  80.  
  81. $col = array();
  82. $col["title"] = "Closed";
  83. $col["name"] = "closed";
  84. $col["width"] = "50";
  85. $col["editable"] = true;
  86. $col["edittype"] = "checkbox"; // render as checkbox
  87. $col["editoptions"] = array("value"=>"Yes:No"); // with these values "checked_value:unchecked_value"
  88. $cols[] = $col;
  89.  
  90.  
  91. $g = new jqgrid();
  92.  
  93. // $grid["url"] = ""; // your paramterized URL -- defaults to REQUEST_URI
  94. $grid["rowNum"] = 10; // by default 20
  95. $grid["sortname"] = 'id'; // by default sort grid by this field
  96. $grid["sortorder"] = "desc"; // ASC or DESC
  97. $grid["caption"] = "Invoice Data"; // caption of grid
  98. $grid["autowidth"] = true; // expand grid to screen width
  99. $grid["multiselect"] = true; // allow you to multi-select through checkboxes
  100. $grid["scroll"] = true; // allow you to multi-select through checkboxes
  101. $g->set_options($grid);
  102.  
  103. $g->set_actions(array( 
  104.                         "add"=>true, // allow/disallow add
  105.                         "edit"=>true, // allow/disallow edit
  106.                         "delete"=>true, // allow/disallow delete
  107.                         "rowactions"=>true, // show/hide row wise edit/del/save option
  108.                         "export"=>true, // show/hide export to excel option
  109.                         "autofilter" => true, // show/hide autofilter for search
  110.                         "search" => "simple", // show single/multi field search condition (e.g. simple or advance)
  111.                         "showhidecolumns" => false // show single/multi field search condition (e.g. simple or advance)
  112.                     )
  113.                 );
  114.  
  115. // you can provide custom SQL query to display data
  116. $g->select_command = "Select
  117.  i.id,
  118.  i.invdate,
  119.  c.name,
  120.  i.note,
  121.  i.total,
  122.  i.closed,
  123.  c.company
  124. From
  125.  invheader i Inner Join
  126.  clients c On c.client_id = i.client_id";
  127.  
  128. // this db table will be used for add,edit,delete
  129. $g->table = "invheader";
  130.  
  131. // pass the cooked columns to grid
  132. $g->set_columns($cols);
  133.  
  134.  
  135. $e["on_insert"] = array("add_client", null, false);
  136. $e["on_update"] = array("update_client", null, false);
  137. $g->set_events($e);
  138.  
  139. function add_client($data)
  140. {
  141.     // first insert in clients table
  142.     mysql_query("insert into clients (name, gender,company)
  143.                     values ('{$data[params][name]}',
  144.                             '{$data[params][gender]}',
  145.                             '{$data[params][company]}')");
  146.    
  147.     // get insert id
  148.     $client_id = mysql_insert_id();
  149.    
  150.     // insert in invheader table
  151.     mysql_query("insert into invheader (client_id, note, invdate, total, closed)
  152.                     values ($client_id,
  153.                             '{$data[params][note]}',
  154.                             '{$data[params][invdate]}',
  155.                             '{$data[params][total]}',
  156.                             '{$data[params][closed]}')");
  157.    
  158.     // all done
  159.     die;
  160. }
  161.  
  162. function update_client($data)
  163. {
  164.     // first insert in clients table
  165.     mysql_query("update clients (name, gender,company)
  166.                     values ('{$data[params][name]}',
  167.                             '{$data[params][gender]}',
  168.                             '{$data[params][company]}')");
  169.    
  170.     // insert in invheader table
  171.     mysql_query("update invheader (client_id, note, invdate, total, closed)
  172.                     values ($client_id,
  173.                             '{$data[params][note]}',
  174.                             '{$data[params][invdate]}',
  175.                             '{$data[params][total]}',
  176.                             '{$data[params][closed]}')");
  177.    
  178.     // all done
  179.     die;
  180. }
  181.  
  182.  
  183. // generate grid output, with unique grid name as 'list1'
  184. $out = $g->render("list1");
  185. ?>
  186. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  187. <html>
  188. <head>
  189.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>  
  190.     <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link> 
  191.    
  192.     <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  193.     <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  194.     <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script> 
  195.     <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  196. </head>
  197. <body>
  198.     <div style="margin:10px">
  199.     <?php echo $out?>
  200.     </div>
  201. </body>
  202. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement