<?php# FileName="connect.php"$hostname = "localhost";$database = "northwind";$username = "root";$password = "";?>2. The second step is to create the file that will handle the queries. We will call the file data.php. The data.php file connects to the ‘Customers’ table from the Northwind Database and returns the data as JSON.
<?php #Include the connect.php file include('connect.php'); #Connect to the database $mysqli = new mysqli("$hostname", "$username", "$password", $database); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } // Initialize pagenum and pagesize $pagenum = $_POST['pagenum']; $pagesize = $_POST['pagesize']; $start = $pagenum * $pagesize; if (isset($_POST['sortdatafield'])) { $sortfield = $_POST['sortdatafield']; $sortorder = $_POST['sortorder']; if ($sortorder != '') { if ($sortorder == "desc") { $query = "SELECT CompanyName, ContactName, ContactTitle, Address, City, Country FROM Customers ORDER BY" . " " . $sortfield . " DESC LIMIT ?, ?"; } else if ($sortorder == "asc") { $query = "SELECT CompanyName, ContactName, ContactTitle, Address, City, Country FROM Customers ORDER BY" . " " . $sortfield . " ASC LIMIT ?, ?"; } $result = $mysqli->prepare($query); $result->bind_param('ii', $start, $pagesize); } else { $result = $mysqli->prepare("SELECT SQL_CALC_FOUND_ROWS CompanyName, ContactName, ContactTitle, Address, City, Country FROM Customers LIMIT ?, ?"); $result->bind_param('ii', $start, $pagesize); } } else { $result = $mysqli->prepare("SELECT SQL_CALC_FOUND_ROWS CompanyName, ContactName, ContactTitle, Address, City, Country FROM Customers LIMIT ?, ?"); $result->bind_param('ii', $start, $pagesize); } /* execute query */ $result->execute(); /* bind result variables */ $result->bind_result($CompanyName, $ContactName, $ContactTitle, $Address, $City, $Country); /* fetch values */ while ($result -> fetch()) { $customers[] = array( 'CompanyName' => $CompanyName, 'ContactName' => $ContactName, 'ContactTitle' => $ContactTitle, 'Address' => $Address, 'City' => $City, 'Country' => $Country ); } // get the total rows. $result = $mysqli->prepare("SELECT FOUND_ROWS()"); $result->execute(); $result->bind_result($total_rows); $result->fetch(); $data[] = array( 'TotalRows' => $total_rows, 'Rows' => $customers ); echo json_encode($data); /* close statement */ $result->close(); /* close connection */ $mysqli->close();?>
<!DOCTYPE html><html lang="en"><head> <link rel="stylesheet" href="/jquery-widgets-documentation/jqwidgets/styles/jqx.base.css" type="text/css" /> <link rel="stylesheet" href="../../jqwidgets/styles/jqx.classic.css" type="text/css" /> <script type="text/javascript" src="none"></script> <script type="text/javascript" src="none"></script> <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script> <script type="text/javascript" src="none"></script> <script type="text/javascript" src="../../jqwidgets/jqxcheckbox.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxgrid.pager.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxgrid.sort.js"></script> <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script> <script type="text/javascript"> $(document).ready(function () { // prepare the data var theme = 'classic'; var source = { datatype: "json", datafields: [ { name: 'CompanyName', type: 'string'}, { name: 'ContactName', type: 'string'}, { name: 'ContactTitle', type: 'string'}, { name: 'Address', type: 'string'}, { name: 'City', type: 'string'}, { name: 'Country', type: 'string'} ], cache: false, url: 'data.php', type: "POST", root: 'Rows', beforeprocessing: function(data) { source.totalrecords = data[0].TotalRows; }, sort: function() { // update the grid and send a request to the server. $("#jqxgrid").jqxGrid('updatebounddata', 'sort'); } }; var dataadapter = new $.jqx.dataAdapter(source); // initialize jqxGrid $("#jqxgrid").jqxGrid( { width: 600, source: dataadapter, theme: theme, autoheight: true, pageable: true, virtualmode: true, sortable: true, rendergridrows: function(params) { return params.data; }, columns: [ { text: 'Company Name', datafield: 'CompanyName', width: 250 }, { text: 'Contact Name', datafield: 'ContactName', width: 200 }, { text: 'Contact Title', datafield: 'ContactTitle', width: 200 }, { text: 'Address', datafield: 'Address', width: 180 }, { text: 'City', datafield: 'City', width: 100 }, { text: 'Country', datafield: 'Country', width: 140 } ] }); }); </script></head><body class='default'> <div id='jqxWidget'"> <div id="jqxgrid"></div> </div></body></html>