Skip to content

Instantly share code, notes, and snippets.

@gridphp
Last active June 14, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gridphp/5f81437486a3288ffd905c8cb824ba57 to your computer and use it in GitHub Desktop.
Save gridphp/5f81437486a3288ffd905c8cb824ba57 to your computer and use it in GitHub Desktop.
Many to Many relation table entry, https://www.gridphp.com
<?php
/**
* PHP Grid Component
*
* @author Abu Ghufran <gridphp@gmail.com> - http://www.phpgrid.org
* @version 2.0.0
* @license: see license.txt included in package
*/
include_once("../../config.php");
include(PHPGRID_LIBPATH."inc/jqgrid_dist.php");
// Database config file to be passed in phpgrid constructor
$db_conf = array(
"type" => PHPGRID_DBTYPE,
"server" => PHPGRID_DBHOST,
"user" => PHPGRID_DBUSER,
"password" => PHPGRID_DBPASS,
"database" => PHPGRID_DBNAME2
);
$g = new jqgrid($db_conf);
$opt["caption"] = "Sample Grid";
$g->set_options($opt);
$g->table = "hubo_clients";
$g->select_command = "select c.id, c.name,
(select group_concat(manager_id) from hubo_client_manager where client_id = c.id) as manager
from hubo_clients c";
$cols = array();
$col = array();
$col["title"] = "Id";
$col["name"] = "id";
$col["editable"] = false;
$cols[] = $col;
$col = array();
$col["title"] = "Client";
$col["name"] = "name";
$col["editable"] = true;
$cols[] = $col;
$col = array();
$col["title"] = "Manager";
$col["name"] = "manager";
$col["editable"] = true;
$col["edittype"] = "select"; // render as select
$col["formatter"] = "select"; // render as select
$str = $g->get_dropdown_values("select distinct id as k, name as v from hubo_managers");
$col["editoptions"] = array("value"=>$str);
$col["editoptions"]["dataInit"] = "function(){ setTimeout(function(){ link_select2('{$col["name"]}'); },200); }";
$col["editoptions"]["multiple"] = true;
$cols[] = $col;
$g->set_columns($cols);
$e["on_insert"] = array("add_client", null, false);
$g->set_events($e);
function add_client($data)
{
global $g;
$client_id = $g->execute_query("INSERT INTO hubo_clients VALUES (null,'{$data["params"]["name"]}')",array(),"insert_id");
$manager_ids = explode(",",$data["params"]["manager"]);
foreach($manager_ids as $mid)
{
$g->execute_query("INSERT INTO hubo_client_manager VALUES ('$client_id','$mid')");
}
$res = array("id" => $client_id, "success" => true);
echo json_encode($res);
}
$out = $g->render("list1");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/themes/redmond/jquery-ui.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../../lib/js/jqgrid/css/ui.jqgrid.css" />
<script src="../../lib/js/jquery.min.js" type="text/javascript"></script>
<script src="../../lib/js/jqgrid/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="../../lib/js/jqgrid/js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script src="../../lib/js/themes/jquery-ui.custom.min.js" type="text/javascript"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
</head>
<body>
<div>
<?php echo $out?>
</div>
<script>
function link_select2(id)
{
var el = $('select[id='+id+'].FormElement')[0];
if (el)
{
// remove nbsp; from start of textarea
if(el.previousSibling) el.parentNode.removeChild(el.previousSibling);
jQuery(el).parent().css('padding-left','5px');
jQuery(el).parent().css('padding-bottom','5px');
}
$('select[name='+id+'].editable, select[id='+id+']').select2({width:'100%'});
$(document).unbind('keypress').unbind('keydown');
}
</script>
</body>
</html>
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
CREATE TABLE IF NOT EXISTS `hubo_clients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40000 ALTER TABLE `hubo_clients` DISABLE KEYS */;
INSERT INTO `hubo_clients` (`id`, `name`) VALUES
(1, 'test'),
(8, 'test2'),
(9, 'test3');
/*!40000 ALTER TABLE `hubo_clients` ENABLE KEYS */;
CREATE TABLE IF NOT EXISTS `hubo_client_manager` (
`client_id` int(11) DEFAULT NULL,
`manager_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40000 ALTER TABLE `hubo_client_manager` DISABLE KEYS */;
INSERT INTO `hubo_client_manager` (`client_id`, `manager_id`) VALUES
(8, 1),
(8, 2),
(9, 1),
(9, 2);
/*!40000 ALTER TABLE `hubo_client_manager` ENABLE KEYS */;
CREATE TABLE IF NOT EXISTS `hubo_managers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40000 ALTER TABLE `hubo_managers` DISABLE KEYS */;
INSERT INTO `hubo_managers` (`id`, `name`) VALUES
(1, 'Manager1'),
(2, 'Manager2');
/*!40000 ALTER TABLE `hubo_managers` ENABLE KEYS */;
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment