Hello! I'm trying to use the example of the grid (http://www.igniteui.com/grid/twitter-bootstrap), but I need the data displayed in it come from a database, someone has already done this or could help me explaining how to build the php to grab the database and play the grid?
I would be very grateful if someone could help me ˆˆ
ps .: am beginner and could not understand the examples I found like this (since it only contains the database connection and does not indicate how prepare the json data to insert into the grid):
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die (mysql_error());
$data = mysql_query($q) or die(mysql_error());
$recCount = mysql_query($countQuery) or die (mysql_error());
where $q holds your query string such as SELECT .... which selects the data rows, and $countQuery holds the query which returns how many rows in total you have (important for paging / summaries, etc.)
$rows = array();
while($r = mysql_fetch_assoc($data)) {
$rows[] = $r;
}
header("Content-type: application/json");
$arr = mysql_fetch_array($recCount);
$response = array("totalCount" => $arr[0], "records" => $rows);
echo json_encode($response);
mysql_close($conn);
Hello Tassiana Kautzmann,
Thank you for using our product!
Our igGrid will handle the returned data if it is in a well formed format (JSON, XML, HTML tables, .NET collections or .NET DataTables). It doesn't matter if you use php or other way to request data. For example the easiest way to request data with our grid is though ajax requests to a WebServices for example or with using our WebApi (http://igniteui.com/grid/bind-web-api). You can find a variety of samples in our website.
About your concerns related to PHP and MySQL my suggestion is to see the videos below, they will help you to get a better understanding of how to request data, setup a connection etc. Ones you get the data, you can simply format it into JSON and pass it as a dataSource to the igGrid.
https://www.youtube.com/playlist?list=PLfdtiltiRHWEbLm0ErHe7HgEOVIO26R_o
I have created a sample that is calling a php page that will retrieve a basic json with data which will be used by the igGrid.
Code snippet:
...
<?php
function getData() { $data = array( array( "ProductID" => 1, "Name" => "Adjustable Race", "ProductNumber" => "AR-5381" ),........ return $data;}
header('Content-type: application/json; charset=utf8;');
$data = getData();echo json_encode($data);
exit(200);
thanks for the examples! I will watch the videos and try to work with the database!
Hello Tassiana,
About the errors, jquery.min is missing, although this is relevant to jquery library itself and could not affect your igGrid. About the other error we do not have Feature named "Usuários" which I see means Users on English. Or maybe you extend this feature and the name is "Usuários" ?
Could you please create a script for your database in order to start the sample on my side?
Looking forward to hearing from you.
Hello! Yes, put "Usuários"! My mistake...I replace by Responsive and at least appeared the grid, but without the data, it appears the animation loading... On console now appears this error: "Uncaught TypeError: Can not read property 'length' of undefined infragistics.core.js: 234"
=(
Follow the link in the database that did file.
Thanks for the patience! =D
Thank you for the provided script, I have decided to use a different database though. Below you will find a code snippet related to the attached sample that will show you how to get data from MySql source and bind it to igGrid.
<?phpmb_internal_encoding('UTF-8');
define('HOST', 'localhost');define('USER', 'root');define('PASSWORD', '');
$database = 'users';
function connectToDatabase($database) { $connection = mysqli_connect(HOST, USER, PASSWORD, $database);
if (!$connection) { throw new Exception('No connection with database!'); }
mysqli_set_charset($connection, 'utf8');
return $connection;}
function getAllUsers($connection) { $query = 'SELECT * FROM users';
$result = mysqli_query($connection, $query);
if (!$result) { throw new Exception('Invalid query: ' . $query); }
$users = [];
while ($row = mysqli_fetch_assoc($result)) { $user['id'] = $row['id']; $user['username'] = $row['username']; $user['isActive'] = (bool)$row['is_active']; $user['role'] = $row['role'];
$users[] = $user; }
return $users;}
header('Content-type: application/json');
try { $connection = connectToDatabase($database); $data = getAllUsers($connection);} catch (Exception $e) { header('HTTP/1.1 400 ' . $e->getMessage());
echo $e->getMessage(); die();}
header('HTTP/1.1 201');echo json_encode($data);
exit();
Thank you so much!! Thanks to your code, after a month racking my brain, I finally got to do what I wanted!! And best of all, it fits any mobile device! It's awesome! Thank you for your patience and for the solution of my problem!! *-* =D
You are welcome!
Thank you for using our controls.
hello!! I again!! =D
I'm using iggrid in another project, and he is working in part not only displays the data in the table (he "prints" two lines, because I have 2 inserts in the database), I reviewed to see the errors, nothing appears on the console but in data.php (the same example you sent me here), accuses a syntax error at line 31: $ users = [];I do not know how to solve, because the previous design worked correctly....any ideas?Thank you!