EasyUI Forum

General Category => General Discussion => Topic started by: Pierre on December 05, 2017, 02:29:03 PM



Title: [SOLVED] Get data from PHP code
Post by: Pierre on December 05, 2017, 02:29:03 PM
Hello
actually, it is just partially EasyUI question but maybe someone can help me.
I need to use PHP to get data and put result into EasyUI datagrid.
Here is PHP code which I try to use:

Code:
<?php $conn = new mysqli("127.0.0.1""root""""mydb"); 
if (
$conn->connect_error) {     
  die(
"Connection failed: " $conn->connect_error); 

$data = array(); 
$index = array(); 
$sql "SELECT ID, parentID, firstname, lastname, address FROM dm_contacts order by ID"
$result $conn->query($sql); 
while(
$row $result->fetch_array(MYSQLI_ASSOC)){   
  
$id $row["ID"];   
  
$parent_id $row["parentID"] === NULL "NULL" $row["parentID"];   
  
$data[$id] = $row;   
  
$index[$parent_id][] = $id;

function 
display_child_nodes($parent_id$level){   
  global 
$data$index;   
  
$parent_id $parent_id === NULL "NULL" $parent_id;   
  if (isset(
$index[$parent_id])) {       
    foreach (
$index[$parent_id] as $id) {           
      echo 
$data[$id]["firstname"] . "," $data[$id]["lastname"] .  "";           
      
display_child_nodes($id$level 1);       
    }   
  } 

display_child_nodes(1230);  // 123 is parentID and function display_child_nodes will return only valid records
$result->close(); 
$conn->close(); 
?>


question is - how to put result of PHP to datagrid like this one:

Code:
         <table id="dg" class="easyui-datagrid" fit="true" border="false" url="get_users.php" singleSelect="true">
            <thead>
              <tr>
                <th field="firstname" width="50">First Name</th>
                <th field="Lastnamee" width="50">Last Name</th>
              </tr>
            </thead>
          </table>

Thank you so much in advance.


Title: Re: Get data from PHP code
Post by: jarry on December 05, 2017, 06:58:14 PM
You can custom the 'loader' function to load any data from remote server.
Code:
<table id="dg" class="easyui-datagrid" data-options="
    url: 'test.php',
    fit: true,
    border: false,
    loader: function(param,succ,error){
        var opts = $(this).datagrid('options');
        $.post(opts.url, function(data){
            var result = [];
            var rows = data.split('\n');
            for(var i=0; i<rows.length; i++){
                if ($.trim(rows[i])){
                    var values = rows[i].split(',');
                    result.push({
                        firstname: values[0],
                        lastname: values[1]
                    });

                }
            }
            succ(result);
        })
    }
    ">
<thead>
  <tr>
    <th field="firstname" width="50">First Name</th>
    <th field="lastname" width="50">Last Name</th>
  </tr>
</thead>
</table>


Title: Re: Get data from PHP code
Post by: Pierre on December 06, 2017, 12:47:51 AM
Thank you so much, it works perfect but I need somehow to pass a value to "test.php", for example:
url: 'test.php?'+member_id,

How to do that, please?

I try this:
Code:
 $(document).ready(function() {
        var _member_id = 123;
        url = 'get_users.php?member_id='+_member_id;
        $('#dg').datagrid({
          url: url
        });
   });
and it does not works correctly (sometime, it displays datagrid declaration inside datagrid and next time it displays data correctly.
How to pass a value to url (to php), please?


Title: Re: Get data from PHP code
Post by: Pierre on December 06, 2017, 02:35:22 PM
Please?


Title: Re: Get data from PHP code
Post by: jarry on December 06, 2017, 05:08:38 PM
The 'loader' function can be rewritten to accept the request parameters.
Code:
loader: function(param,succ,error){
    var opts = $(this).datagrid('options');
    if (!opts.url) return false;
    $.ajax({
        type: opts.method,
        url: opts.url,
        data: param,
        success: function(data){
            var result = [];
            var rows = data.split('\n');
            for(var i=0; i<rows.length; i++){
                if ($.trim(rows[i])){
                    var values = rows[i].split(',');
                    result.push({
                        firstname: values[0],
                        lastname: values[1]
                    });
                }
            }
            success(result);
        },
        error: function(){
            error.apply(this, arguments);
        }
    });
}

You can initialize the query parameters by setting the 'queryParams' property.
Code:
$('#dg').datagrid({
  url: ...,
  queryParams: {
    memeber_id: 12
  }
});
Or reload the datagrid with parameters.
Code:
$('#dg').datagrid('load', {member_id: ...});


Title: Re: Get data from PHP code
Post by: Pierre on December 07, 2017, 01:44:11 AM
Just Awesome. Jarry you are the man! It works perfect, thank you so much!