SHARE
TWEET

Dropdown Country City

gridphp Aug 6th, 2014 (edited) 151 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. $g = new jqgrid();
  21.  
  22. $col = array();
  23. $col["title"] = "Id"; // caption of column
  24. $col["name"] = "id";
  25. $col["width"] = "10";
  26. $cols[] = $col;        
  27.                
  28. $col = array();
  29. $col["title"] = "Country";
  30. $col["name"] = "country_id";
  31. $col["dbname"] = "store.country_id";
  32. $col["width"] = "100";
  33. $col["align"] = "left";
  34. $col["search"] = true;
  35. $col["editable"] = true;
  36. $col["edittype"] = "select"; // render as select
  37. # fetch data from database, with alias k for key, v for value
  38.  
  39. # on change, update other dropdown
  40. $str = $g->get_dropdown_values("select distinct id as k, name as v from country");
  41. $col["editoptions"] = array(
  42.                         "value"=>":;".$str,
  43.                         "onchange" => array(    "sql"=>"select distinct id as k, name as v from state WHERE country_id = '{country_id}'",
  44.                                                                         "update_field" => "state_id" )
  45.                                                         );
  46.  
  47. $col["formatter"] = "select"; // display label, not value
  48. $col["stype"] = "select"; // enable dropdown search
  49. $col["searchoptions"] = array("value" => ":;".$str);
  50.  
  51. $cols[] = $col;        
  52.  
  53. $col = array();
  54. $col["title"] = "State";
  55. $col["name"] = "state_id";
  56. $col["dbname"] = "store.state_id";
  57. $col["width"] = "100";
  58. $col["search"] = true;
  59. $col["editable"] = true;
  60. $col["edittype"] = "select"; // render as select
  61. $str = $g->get_dropdown_values("select id as k, name as v from state");
  62. $col["editoptions"] = array(
  63.                         "value"=>$str,
  64.                         "onchange" => array(    "sql"=>"select distinct id as k, name as v from city WHERE state_id = '{state_id}'",
  65.                                                                         "update_field" => "city_id" )
  66.                                                         );
  67.                                                        
  68. $col["editoptions"]["onload"]["sql"]="select distinct id as k, name as v from state WHERE country_id = '{country_id}'";
  69.  
  70. $col["formatter"] = "select"; // display label, not value
  71. $col["stype"] = "select"; // enable dropdown search
  72. $col["searchoptions"] = array("value" => ":;".$str);
  73.  
  74. $cols[] = $col;
  75.  
  76. $col = array();
  77. $col["title"] = "City";
  78. $col["name"] = "city_id";
  79. $col["dbname"] = "store.city_id";
  80. $col["width"] = "100";
  81. $col["search"] = true;
  82. $col["editable"] = true;
  83. $col["edittype"] = "select"; // render as select
  84. $str = $g->get_dropdown_values("select id as k, name as v from city");
  85. $col["editoptions"] = array(
  86.                         "value"=>$str,
  87.                                                         );
  88.  
  89. $col["editoptions"]["onload"]["sql"]="select distinct id as k, name as v from city WHERE state_id = '{state_id}'";
  90.                                                        
  91. $col["formatter"] = "select"; // display label, not value
  92. $col["stype"] = "select"; // enable dropdown search
  93. $col["searchoptions"] = array("value" => ":;".$str);
  94.  
  95. $cols[] = $col;
  96.  
  97.  
  98. $col = array();
  99. $col["title"] = "Store";
  100. $col["name"] = "name";
  101. $col["dbname"] = "store.name";
  102. $col["width"] = "50";
  103. $col["editable"] = true; // this column is editable
  104. $col["editoptions"] = array("size"=>20); // with default display of textbox with size 20
  105. $col["editrules"] = array("required"=>true); // and is required
  106. $cols[] = $col;
  107.  
  108. $grid["sortname"] = 'id'; // by default sort grid by this field
  109. $grid["sortorder"] = "asc"; // ASC or DESC
  110. $grid["caption"] = "Stores Location - Country/State/City wise"; // caption of grid
  111. $grid["autowidth"] = true; // expand grid to screen width
  112. $grid["add_options"]["afterShowForm"] = "function(){ setup_fields(); }";
  113. $grid["edit_options"]["afterShowForm"] = "function(){ setup_fields(); }";
  114.  
  115. $g->set_options($grid);
  116.  
  117. $g->set_actions(array( 
  118.                                                 "add"=>true, // allow/disallow add
  119.                                                 "edit"=>true, // allow/disallow edit
  120.                                                 "delete"=>true, // allow/disallow delete
  121.                                                 "rowactions"=>true, // show/hide row wise edit/del/save option
  122.                                                 "autofilter" => true, // show/hide autofilter for search
  123.                                         )
  124.                                 );
  125.  
  126. $g->select_command = "SELECT store.id, store.country_id, store.state_id, store.city_id, store.name from store
  127.                                                 left JOIN country on country.id = store.country_id
  128.                                                 INNER JOIN city on city.id = store.city_id
  129.                                                 INNER JOIN state on state.id = store.state_id
  130.                                                 ";
  131.  
  132. // this db table will be used for add,edit,delete
  133. $g->table = "store";
  134.  
  135. // pass the cooked columns to grid
  136. $g->set_columns($cols);
  137.  
  138. // generate grid output, with unique grid name as 'list1'
  139. $out = $g->render("list1");
  140. ?>
  141. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  142. <html>
  143. <head>
  144.         <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css"></link>  
  145.         <link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css"></link>     
  146.        
  147.         <script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
  148.         <script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
  149.         <script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>     
  150.         <script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
  151. </head>
  152. <body>
  153.         <div style="margin:10px">
  154.         <?php echo $out?>
  155.         </div>
  156.        
  157.         <script>
  158.         function setup_fields()
  159.         {
  160.                 if ($('#country_id').val() == '')
  161.                 {
  162.                         $('#tr_city_id').hide();
  163.                         $('#tr_state_id').hide();
  164.                 }
  165.                
  166.                 $('#country_id').change(function(){
  167.                         if ($('#country_id').val() == '')
  168.                         {
  169.                                 $('#tr_city_id').hide();
  170.                                 $('#tr_state_id').hide();
  171.                         }
  172.                         else
  173.                         {
  174.                                 $('#tr_city_id').show();
  175.                                 $('#tr_state_id').show();
  176.                         }
  177.                 });
  178.         }
  179.         </script>
  180. </body>
  181. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top