One of my project I need to display records in tabular format for that purpose I am using Guriddo jqGrid PHP to display records, but I am getting error like:
"Fatal error: Uncaught Error: Class 'jqGridRender' not found....."
I am using PDO for connecting database.
Can any one help me out to overcome with this error?
Please have a look on my code:
require_once 'jqgrid/jq-config.php';
// include the jqGrid Class
require_once ABSPATH."jqgrid/php/PHPSuito/jqGrid.php";
// include the driver class
require_once ABSPATH."jqgrid/php/PHPSuito/DBdrivers/jqGridPdo.php";
// Connection to the server
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
// Write the SQL Query
$sql = 'select member_id,members.full_name, email, mobile, member_type, state_name, city_name, location_name, IF(status=1,"YES","NO") as status, IF(validation_success=1,"YES","NO") as validation_success, profile_picture, date_added, date_updated, last_seen, isloggedin';
$sql .= " from members ";
$grid->SelectCommand = $sql;
// Set the table to where you add the data
$grid->table = 'members';
// set the ouput format to json
$grid->dataType = 'json';
// Let the grid create the model
$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl('manage_member_grid.php');
// Set some grid options
$grid->setGridOptions(array("rowNum"=>100,"sortname"=>"full_name","sortorder"=>"asc","height"=>150,"multiselect"=>true,"hoverrows"=>true,"rowList"=>array(10,20,30,40,50,100),"shrinkToFit"=>false,"width"=>1140,"height"=>500));//
//Set Primary Key for row
$grid->setPrimaryKeyId("member_id");
// Hide fields
$grid->setColProperty("member_id", array("hidden"=>true));
//Change some property of the full_name field
$grid->setColProperty("full_name", array("label"=>array("Name")));
$grid->setColProperty("full_name", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
//Change some property of the email field
$grid->setColProperty("email", array("label"=>array("Email")));
$grid->setColProperty("email", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
// Change some property of the mobile field
$grid->setColProperty("mobile", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));//,"width"=>300,"lable"=>array("Added Date")
$grid->setColProperty("mobile", array("label"=>array("Mobile")));
// Change some property of the member_type field
$grid->setColProperty("member_type", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));//,"width"=>300,"lable"=>array("Added Date")
$grid->setColProperty("member_type", array("label"=>array("Member Type")));
// Change some property of the State field
$grid->setColProperty("state_name", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
$grid->setColProperty("state_name", array("label"=>array("Satate")));
// Change some property of the City field
$grid->setColProperty("city_name", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
$grid->setColProperty("city_name", array("label"=>array("City")));
// Change some property of the Address field
$grid->setColProperty("location_name", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
$grid->setColProperty("location_name", array("label"=>array("Location No")));
// Change some property of the date_added field
$grid->setColProperty("date_added", array("formatter"=>"date","formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"d/m/Y h:i a")));//,"lable"=>array("Added Date")
$grid->setColProperty("date_added", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
$grid->setColProperty("date_added", array("label"=>array("Added Date")));
$grid->setDatepicker('date_added');
$grid->setColProperty('date_added', array("editable"=>false));
// Change some property of the date_updated field
$grid->setColProperty("date_updated", array("formatter"=>"date","formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"d/m/Y h:i a")));
$grid->setColProperty("date_updated", array("label"=>array("Last Update")));
$grid->setColProperty('date_updated', array("editable"=>false));
// Change some property of the status field
$grid->setColProperty("status", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
$grid->setColProperty("status", array("label"=>array("isActive")));
// Change some property of the validation_success field
$grid->setColProperty("validation_success", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
$grid->setColProperty("validation_success", array("label"=>array("isEmail Validate")));
// Change some property of the profile_picture field
$grid->setColProperty("profile_picture", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
$grid->setColProperty("profile_picture", array("label"=>array("Profile Picture"),"formatter"=>"js:formatLinkImages"));
$grid->setColProperty('profile_picture', array("editable"=>false));
// Change some property of the last_seen field
$grid->setColProperty("last_seen", array("formatter"=>"date","formatoptions"=>array("srcformat"=>"Y-m-d H:i:s","newformat"=>"d/m/Y h:i a")));//,"lable"=>array("Added Date")
$grid->setColProperty("last_seen", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
$grid->setColProperty("last_seen", array("label"=>array("Last Seen")));
$grid->setDatepicker('last_seen');
$grid->setColProperty('last_seen', array("editable"=>false));
// Change some property of the isloggedin field
$grid->setColProperty("isloggedin", array("searchoptions"=>array("sopt"=>array("cn","eq","ne","le","ge","gt","nc","bw","bn","ew","en"))));
$grid->setColProperty("isloggedin", array("label"=>array("isLoggedin Now")));
$grid->setColProperty('isloggedin', array("editable"=>false));
// Enable toolbar searching
$grid->toolbarfilter = true;
$grid->setFilterOptions(array(
"stringResult"=> true,
"searchOperators"=>true
));
// We can put JS from php
$custom = <<<CUSTOM
/*function formatLinkMember(cellValue, options, rowData) {
return "<a class='button' href='#login_form' id='login_pop' onclick='getMemberDetails("+rowData.member_id+")' >"+rowData.full_name+"</a>";
//return "<input type='button' value='somevalue' onclick='some_function'\>";
}*/
function formatLinkImages(cellValue, options, rowData) {
var path = "'"+rowData.member_id+"', '"+rowData.profile_picture+"'";
return '<a class="button" href="#login_form" id="login_pop" onclick="javascript:getImagesDetails('+path+');" >'+rowData.profile_picture+'</a>';
//return "<input type='button' value='somevalue' onclick='some_function'\>";
}
CUSTOM;
// Let set the code which is executed at end
$grid->setJSCode($custom);
//add on double clicl event (This will take to the View Records)
$ondblclick = <<<DBLCLICK
function(rowId, iRow, iCol, e)
{
if(rowId)
{
// this refers to grid
$(this).jqGrid('viewGridRow',rowId,{});
}
}
DBLCLICK;
$grid->setGridEvent('ondblClickRow',$ondblclick);
// add a custom button via the build in callGridMethod
// note the js: before the function
$buttonoptions = array("#pager",
array("caption"=>"Pdf", "title"=>"Export to Pdf", "onClickButton"=>"js: function(){
jQuery('#grid').jqGrid('excelExport',{tag:'pdf', url:'manage_member_grid.php'});}"
)
);
$grid->callGridMethod("#grid", "navButtonAdd", $buttonoptions);
// Enable navigator searching
$grid->navigator = true;
// Set which buttons should be visible
$grid->setNavOptions('navigator',array("add"=>false,"edit"=>false,"del"=>false,"view"=>true,"excel"=>true,"recreateForm"=>true));
// Enjoy
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
It is important to know which version of Guriddo Suito PHP is used. In the latest versions (can't remember which one) autoloader class is used.
Maybe you can try to load the render class insted of jqgrid one i.e replace
// include the jqGrid Class
require_once ABSPATH."jqgrid/php/PHPSuito/jqGrid.php";
with this one
// include the jqGrid Class
require_once ABSPATH."jqgrid/php/PHPSuito/jqGridRender.php";
I want to make an attendance sheet with jqGrid. I'm using PHP and Mysql
I have two tables, one called MemberInfo and one called Attendance.
From the MemberInfo I want to show in the grid the first name and the last name of the member. Then I want to have a box for every day of the week. I want that when I add some data to those fields, for the data to be saved in the Attendance table and also that if I generate the Attendance grid again, the fields that were already filled up, to show the data.
My question is:
How can I add more columns and How can I connect those columns with the Attendance table? Thanks!
EDIT:
I was able to generate new columns and to add the data to the database with cellEdit. Still having problems with generating the grid with the data from 2 tables. Thanks!
I hope this is clear! if its not please let me know! thanks!
(if there is another library for PHP that would make this easier please let me know)
EDIT:
<?php
require_once 'jqgrid/jq-config.php';
// include the jqGrid Class
require_once "jqgrid/php/jqGrid.php";
// include the driver class
require_once "jqgrid/php/jqGridPdo.php";
// Connection to the server
$conn = new PDO("mysql:host=localhost;dbname=db;","root",NULL);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
// Write the SQL Query
$grid->SelectCommand = 'SELECT member_id, first_name, last_name FROM members_info WHERE member_type !=5';
// set the ouput format to json
$grid->dataType = 'json';
$grid->table ="members_info";
$grid->setPrimaryKeyId("member_id");
// Let the grid create the model
$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl('grid.php');
$grid->cacheCount = true;
// Set grid caption using the option caption
$today = date('Y-m-d');
if(isset($_POST['past_month'])){
$today = date('Y-m-d', strtotime($_POST['past_month']));
}
if(isset($_POST['next_month'])){
$today = date('Y-m-d', strtotime($_POST['next_month']));
}
$days = attendance_cal(date('F', strtotime($today)), date('Y', strtotime($today))); // Gets the days for that month and that year
sort($days); //sort the days
foreach($days as $day){
$grid->addCol(array(
"name"=>date('m-d', $day)
));
}
$grid->setGridOptions(array(
"caption"=>"This is custom Caption",
"rowNum"=>30000,
"sortname"=>"member_id",
"hoverrows"=>true,
"width"=>1000,
"height"=>1000,
"cellEdit"=> true,
"cellsubmit"=>"remote",
"cellurl"=> "cell_dump.php",
"rowList"=>array(10,20,50),
"postData"=>array("grid_recs"=>776)
));
// Change some property of the field(s)
$grid->setColProperty("member_id", array("label"=>"ID", "width"=>60, "editable"=>false));
$grid->setColProperty("first_name", array("label"=>"First Name", "width"=>120, "editable"=>false));
$grid->setColProperty("last_name", array("label"=>"Last Name", "width"=>120, "editable"=>false));
// Enjoy
$grid->navigator = false;
// and finaly bind key navigation
// This is way if no events or parameter
//$grid->callGridMethod('#grid', 'bindKeys');
//
//in case of passing events is better this way
$bindkeys =<<<KEYS
$("#grid").jqGrid('bindKeys', {"onEnter":function( rowid ) { alert("You enter a row with id:"+rowid)} } );
KEYS;
$grid->setJSCode($bindkeys);
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
$conn = null;
?>
Let me be more specific:
My table "Members" has the fields "member_id", "first_name", "last_name"
The "Attendance" table has the fields "attendance_id", "member_id", "attendance_date" ,"attendance_value"
My Grid, I want it to look like:
| Member Id | Name | 03-15-2012 | 03-20-2012 | 03-22-2012 |
The "Member Id" column and "Name" column is being generated from the "Members" table with the SelectCommand, the other columns I'm creating them with addCol. I kinda can figure out how to add data to the database via cellEdit, but when I load the sheet, I dont know how to put the data from the database in the grid besides for the ones coming from the Members table. I hope this is clearer! thanks!
I am assuming you have never used jqGrid and you need to get started...
Please have a look at this link, it gives you demos with code for everything you need to know on how to create your grid using PHP.
http://www.trirand.net/demophp.aspx
I am new to jquery and jqgrid, but i am comfortable with javascript. However I have managed to install jqgrid after some effort.
I have been trying a to find a solution to enable ore disable the delete feature from the navigation bar based on the value of the 'lock' column. I read the following link
jqgrid: how to set toolbar options based on column value in row selected
But I was not able to get the contents of 'lock' cell for the javascript. I also tried to format the lock string without effect.
the jqgrid is loaded via php. The script is here http://www.trirand.net/demophp.aspx
The php script is the following
require_once("JQGrid/jq-config.php");
require_once("JQGrid/php/jqGridASCII.php");
require_once("JQGrid/php/jqGridPdo.php");
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
$grid = new jqGridRender($conn);
$grid->SelectCommand = 'SELECT * FROM `device_assignement` ';
$grid->dataType = 'json';
$grid->setColModel();
$grid->setUrl('Grid_ecu_display.php');
$grid->setColProperty("company",
array("label"=>"Dealer Name",
"width"=>350
),
array( "searchrules"=>
array("searchhidden"=>false, "required"=>false, "search"=>false)));
$grid->setGridOptions(array(
"sortable"=>true,
"rownumbers"=>true,
"rowNum"=>40,
"rowList"=>array(10,50,100),
"sortname"=>"ecu",
"width"=>940,
"height"=>400,
"shrinkToFit"=>true,
"hidden" => true,
"hoverrows"=>true ));
$grid->toolbarfilter = true;
$grid->setFilterOptions(array("stringResult"=>true));
$grid->setColProperty("ecu", array(
"label"=>"ECU Number" ,
"sortable"=>true
));
$grid->setColProperty("lock", array(
"label"=>"<i>Lock</i>" ,
"width"=>60,
"sortable"=>false,
"editable"=>true
));
etc etc...
$ecu = jqGridUtils::GetParam('ecu');
// This command is executed immediatley after edit occur.
$grid->setAfterCrudAction('edit', "UPDATE `ecu_master` SET `lock` = '1' WHERE `ecu` =?",array($ecu));
$grid->navigator = true;
$grid->setNavOptions('navigator', array("pdf"=>true, "add"=>false,"edit"=>true,"del"=>false,"view"=>false, "excel"=>true));
$grid->setColProperty('company',array("searchoptions"=>array("sopt"=>array("cn"))));
$oper = jqGridUtils::GetParam("oper");
if($oper == "pdf") {
$grid->setPdfOptions(array(
// set the page orientation to landscape
"page_orientation"=>"L",
// enable header information
"header"=>true,
// set bigger top margin
"margin_top"=>27,
// set logo image
//"header_logo"=>"logo.gif",
// set logo image width
//"header_logo_width"=>30,
//header title
"header_title"=>"Autograde CMS ECU Allocation List",
// and a header string to print
"header_string"=>"$SoftwareVersion"
));
}
// Run the script
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
This is included in another php script where.
All I want is to enable or disable the delete row button based on the "lock" value
If this seems too basic and ridiculous please let me know I will understand.
If the user click on a cell of the grid the whole row will be selected and the callback function onSelectRow will be called. So you should implement the callback function onSelectRow which has the rowid (the id of the <tr>) as the first parameter. Inside of the onSelectRow handler you can call getCell method. Depend on the value of the 'lock' column (which can be hidden if needed) you can enable of disable "Edit" and "Delete" buttons of the navigator bar.
So the code can be about the following:
$('#list').jqGrid({
... all other jqGrid options which you need
pager: '#pager',
onSelectRow: function (rowid) {
var gridId = $.jgrid.jqID(this.id);
// test 'lock' column for some value like 'yes'
if ($(this).jqGrid('getCell', rowid, 'lock') === 'yes') {
// disable the "Edit" and "Delete" buttons of the navigator
$("#edit_" + gridId).addClass('ui-state-disabled');
$("#del_" + gridId).addClass('ui-state-disabled');
} else {
// enable the "Edit" and "Delete" buttons of the navigator
$("#edit_" + gridId).removeClass('ui-state-disabled');
$("#del_" + gridId).removeClass('ui-state-disabled');
}
}
}).jqGrid('navGrid', '#pager');
Because you are new in jqGrid I want comment the usage of $.jgrid.jqID() function. In the most cases if returns the value of the input parameter: 'list' in case of the example. It's needed for more common case if the id of the grid (the id of the <table> element) contains meta-characters. $.jgrid.jqID() function include additional escape characters (two backslashes: \\) before any meta-character.
I am new in jqgrid and I would like to increase the width of jqgrid.
I have increased the column width but the grid width is not increasing.
I am using php jqgrid.
Is there any parameters to pass this function :=
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
or How can i do this ?
Thanks a lot.
You question is mostly about the commercial version of jqGrid, which I don't know. The main problem exist also in the jqGrid too. jqGrid has width parameter which can be used to define the grid width. I suppose that you should use (or already use) $grid->setGridOptions to define the option. Another option which can be additionally used are autowidth which will be overwrite the width value calculated based on the size of the grid's parent. Other important option can be important you: shrinkToFit which default value is true. It meant that the width properties for the column will be not used as the exact column width in pixel. Instead of that the width properties will be used to define only proportion between the column widths. If the column width of some column should be not changed you should include fixed: true property in the colModel for the corresponding definition of the column. If you want to have exact column width for all columns (as it's defined in width properties of the items of the colModel) you should use the jqGrid setting shrinkToFit: false. Try to include the setting in the $grid->setGridOptions call.
You can use below php code:
// Set grid with 1000px by php
$grid->setGridOptions(array("width"=>1000));
I had same issue, my grid was taking 650px by default with. So, I check some blog and also wiki and now is ended up with :)
Here is my complete php code with auto grid width:
<?php
require_once '../../../jq-config.php';
// include the jqGrid Class
require_once ABSPATH."php/jqGrid.php";
// include the driver class
require_once ABSPATH."php/jqGridPdo.php";
// Connection to the server
$conn = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
// Tell the db that we use utf-8
$conn->query("SET NAMES utf8");
// Create the jqGrid instance
$grid = new jqGridRender($conn);
// SQL query
$sql = <<<SQL
SELECT *,
CASE total_correct_answer
WHEN total_correct_answer=1 THEN 1
WHEN total_correct_answer=2 THEN 3
ELSE 6
END AS points
FROM
(
SELECT COUNT(*) total_correct_answer, v.coupon_code_id, v.coupon_no, v.login_id, v.cc_match_id, v.name, v.contact_no, v.email, v.user_from
FROM (
SELECT p.login_id, ui.name, ui.contact_no,l.email,l.user_from,
p.quiz_id p_quiz_id,p.question_bank_id p_question_bank_id, p.answer_id p_answer_id,
cc.quiz_id cc_quiz_id,cc.question_bank_id cc_question_bank_id, cc.answer_id cc_answer_id,
cc.match_id cc_match_id, p.coupon_code_id, cd.coupon_no
FROM prediction p
INNER JOIN correct_answer cc
INNER JOIN `user_information` ui ON p.`login_id` = ui.`login_id`
INNER JOIN coupon_code cd ON cd.coupon_code_id = p.coupon_code_id
INNER JOIN login l ON l.login_id = p.login_id
WHERE cc.quiz_id=p.quiz_id AND cc.question_bank_id=p.question_bank_id
AND cc.answer_id=p.answer_id AND cc.match_id IN (SELECT match_id FROM `match` WHERE
start_time BETWEEN DATE_ADD(NOW(), INTERVAL -7 DAY) AND NOW())
) v GROUP BY v.coupon_code_id ORDER BY v.login_id DESC
) a
SQL;
// Write the SQL Query
$grid->SelectCommand = $sql;
// Set output format to json
$grid->dataType = 'json';
// Let the grid create the model
$grid->setColModel();
// Set the url from where we obtain the data
$grid->setUrl('grid.php');
// Set alternate background using altRows property
$grid->setGridOptions(array(
"rowNum"=>10,
"sortable"=>true,
"rownumbers"=>true,
"width"=>'auto',
"altRows"=>true,
"multiselect"=>true,
"rowList"=>array(10,20,50),
));
// Change some property of the field(s)
$grid->setColProperty("total_correct_answer", array("label"=>"Answer", "width"=>80));
$grid->setColProperty("coupon_code_id", array("label"=>"Coupon Code", "width"=>80));
$grid->setColProperty("coupon_no", array("label"=>"Coupon Number", "width"=>120));
$grid->setColProperty("login_id", array("label"=>"User ID", "width"=>80));
$grid->setColProperty("cc_match_id", array("label"=>"Match ID", "width"=>80));
$grid->setColProperty("name", array("label"=>"User Name", "width"=>120));
$grid->setColProperty("contact_no", array("label"=>"Contact No", "width"=>120));
$grid->setColProperty("email", array("label"=>"User Email", "width"=>120));
$grid->setColProperty("user_from", array("label"=>"User Mode", "width"=>120));
$grid->setColProperty("points", array("label"=>"User Points", "width"=>120));
// Enable navigator
$grid->navigator = true;
// Enable excel export
$grid->setNavOptions('navigator', array("excel"=>true,"add"=>false,"edit"=>false,"del"=>false,"view"=>false));
// Set different filename
$grid->exportfile = 'Prediction_Report.xls';
// Enjoy
$grid->renderGrid('#grid','#pager',true, null, null, true,true);
$conn = null;
?>
This is my complete Wordpress plugin file:
<?php
function wp_create_table_install()
{
global $wpdb;
$table_name = $wpdb->prefix.'createtable';
$sql = 'CREATE TABLE '.$table_name.'(
id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(75)
);';
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
function wp_create_table_insert_data()
{
global $wpdb;
$table_name = $wpdb->prefix.'createtable';
$id = 1;
$name = 'WP Create Table!';
$wpdb->insert($table_name, array('id' => $id, 'name' => $name));
}
register_activation_hook(__FILE__, 'wp_create_table_install');
register_activation_hook(__FILE__, 'wp_create_table_insert_data');
?>
When I activate the plugin, it always tries to create a table and insert data. How could I do it once, just in the first plugin activation?
Thank you.
A quicker way would be to add [IF NOT EXISTS] in your CREATE statement so that you don't get an error if your table already exists.
Before running CREATE TABLE, you could query information_schema.tables to check to see whether or not the table already exists.
Unfortunately, there's no way to run something on "install" - surprisingly, WordPress doesn't provide any hooks for install as opposed to activation!
The way people cope with this is to set and test an option - if the option is not set, then create the tables, and if it is set do nothing or do a DB upgrade. Options are read in and cached so there is no performance penalty to doing this.
$opt = get_option(MYPLUGIN_OPTIONS);
$opt['dbversion'] = 100;
...
update_option(MYPLUGIN_OPTIONS, $opt);