Forum


14:22

27/10/2009

Hello to everyone,
I've recently looked into JDGrid but had a seriously hard time understanding the help files and how to implement certain things, which I could really use some help with. You'll have to bear with me a bit because my Javascript/JQuery isn't very good, and unfortunately I don't have the free time to start learning it all from scratch. I am proficient in PHP/MySQL so don't think I'm totally useless 😉
I have spent over 10 hours trying to make this work and I'm frustrated and very tired. I've gone over all the documents regarding this project and looked throught the forums in the hope that I will find answers, but I'm either blind, or I just don't understand how and why its not working.
Let me get to the point:
I have successfully installed everything... and was able to make my datagrid show correctly with all the elements displaying, however I can't understand HOW THE HECK I can set up the EDIT, ADD, DELETE buttons to work, because I just cannot see WHERE I set the necessary values to be sent throught GET and how exactly it works.
The code so far:
display page: country.php (the main page that displays the GRID)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Country Management</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script type="text/javascript" src="script/jquery.js"></script>
<link type="text/css" href="script/jquery_ui/redmond/jquery-ui-css.css" rel="stylesheet" />
<script type="text/javascript" src="script/jquery_ui/jquery-ui.min.js"></script>
<link type="text/css" href="script/jgrid/ui.jqgrid.css" rel="stylesheet" />
<script type="text/javascript" src="script/jgrid/i18n/grid.locale-en.js"></script>
<script type="text/javascript" src="script/jgrid/jquery.jqGrid.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery("#list").jqGrid({
url:'script/jgrid/country_loader.php?q=2', /* WHAT IS THIS q=2 FOR???? IT DOESN'T MAKE SENSE */
datatype: 'xml',
mtype: 'GET',
colNames:['ID','Country Code', 'English','Greek'],
colModel :[
{name:'location_country_id', index:'location_country_id', width:90, align:'center', sortable:true, editable:false, hidden: false},
{name:'location_country_tel_countrycode', index:'location_country_tel_countrycode', width:90, align:'center', sortable:true, editable:true, edittype:'text'},
{name:'location_country_en', index:'location_country_en', width:90, align:'center', sortable:true, editable:true, edittype:'text'},
{name:'location_country_gr', index:'location_country_gr', width:90, align:'center', sortable:true, editable:true, edittype:'text'}
],
pager: '#pager',
rowNum:10,
rowList:[10,20,30],
sortorder: 'desc',
viewrecords: true,
caption: 'Country Management',
editurl:"script/jgrid/country_save.php" /* IS THIS IT? BUT WHAT NEEDS TO GO HERE?? */
});
/* do not confuse the "gr" in the functions below with any variables of mine. These were examples I found and used as is */
$("#bedata").click(function(){
var gr = jQuery("#list").getGridParam('selrow');
if( gr != null ) jQuery("#list").editGridRow(gr,{height:280,reloadAfterSubmit:true,closeAfterEdit:true});
else alert("Please Select Row"); });
$("#dedata").click(function(){
var gr = jQuery("#list").getGridParam('selrow');
//getSelectedRow();
if( gr != null ) jQuery("#list").delGridRow(gr,{reloadAfterSubmit:true});
else alert("Please Select Row to delete!"); });
});
</script>
</head>
<body>
<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>
<input type="BUTTON" id="bedata" value="Edit Selected" />
<input type="BUTTON" id="dedata" value="Delete Selected" />
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------------------------------------
The above information is to create countries in a database where the "location_country_id" is the primary key, "location_country_tel_countrycode" is an INT value, and the rest are the values for each language (varchar(255)).
-----------------------------------------------------------------------------------------------------------------------------------------------------------
The file: country_loader.php (I don't understand why this has to be a separate file but anyways)
<?php
//include the information needed for the connection to MySQL data base server.
// we store here username, database and password
include('include/config.php'); /* database connection its all setup in here, no need for anything more here. */
$page = $_GET['page'];
// get how many rows we want to have into the grid - rowNum parameter in the grid
$limit = $_GET['rows'];
// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel
$sidx = $_GET['sidx'];
// sorting order - at first time sortorder
$sord = $_GET['sord'];
// if we not pass at first time index use the first column for the index or what you want
if(!$sidx) $sidx =1;
// calculate the number of rows for the query. We need this for paging the result
$result = mysql_query("SELECT COUNT(*) AS count FROM location_country");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$count = $row['count'];
// calculate the total pages for the query
if( $count > 0 && $limit > 0) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
// if for some reasons the requested page is greater than the total
// set the requested page to total page
if ($page > $total_pages) $page=$total_pages;
// calculate the starting position of the rows
$start = $limit*$page - $limit;
// if for some reasons start position is negative set it to 0
// typical case is that the user type 0 for the requested page
if($start <0) $start = 0;
// the actual query for the grid data
$SQL = "SELECT * FROM location_country ORDER BY $sidx $sord LIMIT $start , $limit";
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
// we should set the appropriate header information. Do not forget this.
header("Content-type: text/xml;charset=utf-8");
$s = "<?xml version='1.0' encoding='utf-8'?>";
$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[location_country_id]."'>";
$s .= "<cell>". $row[location_country_id]."</cell>";
$s .= "<cell>". $row[location_country_tel_countrycode]."</cell>";
$s .= "<cell><![CDATA[". $row[location_country_en]."]]></cell>";
$s .= "<cell><![CDATA[". $row[location_country_gr]."]]></cell>";
$s .= "</row>";
}
$s .= "</rows>";
echo $s;
?>
-----------------------------------------------------------------------------------------------------------------------------------------------
I don't understand where I set the variables to be passed using $_GET. For EDIT, I need to send all the variables to here: editurl:"script/jgrid/country_save.php". So, what do I have inside country_save.php??? I thought something like this was needed:
<?php
include('include/config.php'); /* database connection */
$SQL = "UPDATE location_country SET location_country_tel_countrycode = '".$_GET['location_country_tel_countrycode']."', location_country_en = '".$_GET['location_country_en']."', location_country_gr = '".$_GET['location_country_gr']."' WHERE location_country_id = '".$_GET['location_country_id']."' ";
$result = mysql_query( $SQL ) or die("Couldn't execute query.".mysql_error());
?>
And even so I don't get how this would work since I haven't sent the variables to this page. As for the DELETE function, I assume I need to do the same as in the EDIT, ofcourse, I only need to send the ID to the correct page and execute the command.
If someone could send me a basic example of how to write the script correctly, I'm sure I'm only missing a few small lines of code (the part that sends the GET variables I assume) so that I can understand just how variables get send and processed. The examples I found: "http://trirand.com/jqgrid/jqgrid.html#" don't make sense to me.
08:59

php file name: edit_user.php
<?
include('../connections/theConnection.php'); //connections
$newid = $_POST['id']; //row id. this is the id of the selected row
$uname = $_POST['uname']; //some variable
$pass = md5($_POST['pass']); //some variable
$email = $_POST['email']; //some variable
$fname = $_POST['fname']; //some variable
$mname = $_POST['mname']; //some variable
$lname = $_POST['lname']; //some variable
$cnumber = $_POST['cnumber']; //some variable
$level = $_POST['level']; //some variable
$operation = $_POST['oper']; //can be “add” “edit” “del”
if ($operation == “edit”)
{
$result = mysql_query(“UPDATE user_tbl SET uname = '$uname', fname = '$fname', mname = '$mname', lname = '$lname', email = '$email', pass = '$pass', cnumber = '$cnumber', level = '$level' WHERE uid = '$newid'”);
}
else if ($operation == “add”) {
$result = mysql_query(“INSERT INTO user_tbl(uname, fname, mname, lname, email, pass, cnumber, level, status) VALUES ('$uname', '$fname', '$mname', '$lname', '$email','$pass', '$cnumber','$level', 2)”);
}
else if($operation == “del”)
{
$result = mysql_query(“UPDATE user_tbl SET del = 2 WHERE uid = '$newid'”);
}
mysql_close($db);
?>
the javascript required:
jQuery(”#btn_add_edit”).click(function(){
var gr = jQuery(”#my_jqgrid”).getGridParam('selrow');
if( gr != null ) {
jQuery(”#my_jqgrid”).editGridRow(gr,{height:280,reloadAfterSubmit:true});
}
else {
jQuery(”#my_jqgrid”).editGridRow(”new”,{height:280,reloadAfterSubmit:true});
}
});
the html required:
SPAN { font-family: “Courier New”; font-size: 10pt; color: #000000; background: #FFFFFF; }A { text-decoration: none; font-weight: bold; color:#000000;}.S1 { color: #009300; background: #FFF7F7; }.S2 { color: #00008B; background: #FFF7F7; }<button type='submit' id='btn_add_edit'>ADD/EDIT</button>
____________________________________________________________________________
my simple example.. i use that in our school project. and works well.
09:48

Dear pupian,
I have try your php file. it works on add dan edit function but niether does delete function. could anyone help me please?
<?
include('config/connect.php'); //connections
$id = $_POST['id']; //row id. this is the id of the selected row
if ($operation == "del")
{
$result = mysqli_query($link, "UPDATE table SET del = 2 WHERE id = '$id'"); //which syntax should I use?
$result = mysqli_query($link, "DELETE FROM table WHERE id = '$id'"); //which syntax should I use?
//both of them do not work
}
mysqli_close($link);
?>
Regards,
Candra
Most Users Ever Online: 715
Currently Online:
66 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66