Jump to content

flexigrid php help


cinek

Recommended Posts

I'm trying to use flexigrid for a table. It seems to be connecting ok as no errors are shown, but it doesn't get any data back. I'm using an example page (changed the info; connection & fields).....here's the whole php code

<?function runSQL($rsql) {		$base_url = "http://".$_SERVER['HTTP_HOST'];	$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);	if (strpos($base_url,'webplicity.net')) $active_group = "live";	$connect = mysql_connect('localhost', 'root', 'pass') or die ("Error: could not connect to database");	$db = mysql_select_db('monitor') or die("error");		$result = mysql_query($rsql) or die ('test');	return $result;	mysql_close($connect) or die("ers");}function countRec($fname,$tname) {	$sql = "SELECT count(id) FROM serverinfo ";	$result = runSQL($sql) or die("errrr");	while ($row = mysql_fetch_array($result)) {		return $row[0];	}	}$page = $_POST['page'];$rp = $_POST['rp'];$sortname = $_POST['sortname'];$sortorder = $_POST['sortorder'];if (!$sortname) $sortname = 'name';if (!$sortorder) $sortorder = 'desc';$sort = "ORDER BY $sortname $sortorder";if (!$page) $page = 1;if (!$rp) $rp = 10;$start = (($page-1) * $rp);$limit = "LIMIT $start, $rp";$sql = "SELECT id, Company, Name, Status FROM serverinfo $sort $limit";$result = runSQL($sql);$total = countRec('id','Company');header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );header("Cache-Control: no-cache, must-revalidate" );header("Pragma: no-cache" );header("Content-type: application/json");$json = "";$json .= "{\n";$json .= "page: $page,\n";$json .= "total: $total,\n";$json .= "rows: [";$rc = false;while ($row = mysql_fetch_array($result)) {if ($rc) $json .= ",";$json .= "\n{";$json .= "id:'".$row['id']."',";$json .= "cell:['".$row['id']."','".$row['id']."'";$json .= ",'".addslashes($row['Company'])."'";$json .= ",'".addslashes($row['Name'])."'";$json .= ",'".addslashes($row['Status'])."'";$json .= "}";$rc = true;}$json .= "]\n";$json .= "}";echo $json;?>

and here's the js on the index page which shoiuld get the info from this script

<script type="text/javascript">			$("#flex1").flexigrid			(			{			url: 'post.php',			dataType: 'json',			colModel : [				{display: 'ID', name : 'id', width : 40, sortable : true, align: 'center'},				{display: 'Company', name : 'Company', width : 180, sortable : true, align: 'left'},				{display: 'Name', name : 'Name', width : 130, sortable : true, align: 'left'},				{display: 'Status', name : 'Status', width : 80, sortable : true, align: 'right'}				],			searchitems : [				{display: 'id', name : 'id'},				{display: 'Name', name : 'name', isdefault: true}				],			sortname: "id",			sortorder: "asc",			usepager: true,			singleSelect: true,			title: 'Countries',			useRp: true,			rp: 15,			showTableToggleBtn: true,			width: 700,			height: 200			}			);   	</script>

Link to comment
Share on other sites

Is that JSON structure correct? It looks like the grid is expecting fields called "id", "Company", "Name", and "Status", but each item has "id" and "cell" properties, and the cell property is an array with 5 items. It seems like you should be giving it an object with the same property names you're specifying in the grid. Also, you're not closing the cell array in the JSON structure. That means there's a syntax error in the JSON structure, but it sounds like you're not seeing the error that should cause. And according to the definition here:http://json.org/A string in a JSON structure needs to be surrounded with double quotes, not single quotes. Object property names are also strings, so they need to be quoted also.You may find it easier to just build your array in PHP and then use json_encode to encode it for output.

Link to comment
Share on other sites

you mean the php json should look like this?

while($row = mysql_fetch_array($result)){$json = array("id" => $row['id'],"Company" => $row['Company'],"Name" => $row['Name'],"Status" => $row['Status']);}echo json_encode($json);

is the javascript ok ?

Link to comment
Share on other sites

That loop will keep resetting $json to another array, and then it will print the last one. I'm not sure what kind of data structure the grid component expects, so it's hard to say what's right. There should be documentation about that online. I don't do a lot with jQuery so I'm not sure if your Javascript is correct for what you're trying to do, but look for documentation for the grid component you're using.

Link to comment
Share on other sites

Check the example PHP file for JSON here:http://www.flexigrid.info/It looks like it outputs a structure that has "page", "total", and "rows" properties, and the "rows" property is an array of rows. It looks like each row has "id" and "cell" properties, and the cell property is just an array of the data for that row. Contrary to what I thought, there's no correlation between the names in the column model and the JSON data. I'm not sure why he made that decision, it's probably better to have named properties than rely on the array order. I guess you can always read through the source code for the class, but if he's not going to bother to provide documentation for the things he releases then it makes it a little difficult for people to learn how to use it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...