Retrieve data from array

Retrieve data from array

KarkuroKarkuro Posts: 7Questions: 5Answers: 0
edited April 2016 in Free community support

Hi,

I'm using DataTables, but at the moment, I'm taking the data from my Controller, and put it on a file, and the DataTable reads this file.

 $arrayMess = array();

// Put the data into an array 
foreach ($content as $value) 
{

$arrayMess['data'][] = array('id' => $value->id_message, 'message' => $value->message);

}

  // We load the Helper File from CodeIgniter to allow use to read, open and delete files
  $this->load->helper('file');

  // Convert the array to JSON to use it with DataTables
  // IMPORTANT : The JSON file must be well formated
  $arrayMess = json_encode($arrayMess);

  // Write the JSON into a text file to use with DataTables
  write_file(APPPATH.'../assets/files/data/data.txt', $arrayMess);

I'd like to take directly the data from the array, and not go through a file.
I tried to pass the array into the "ajax": parameter but it doesn't work.

Thanks for the help.

Answers

  • JujuPommeJujuPomme Posts: 50Questions: 6Answers: 0
    edited April 2016

    Hey,

    You've to call your array in a php file and call this php file with the "ajax" attribute.

    See the ajax request to understand how it works.

    For example :

    var table = $('#example').DataTable( {
            "ajax": "yourfile.php"
    }); 
    

    And in yourfile.php :

    ```
    <?php

    $data = $class->callthefunctionyouwant($ur_parameters);
    
    echo $data;
    
    <?php > ``` ?>

    The "ajax" attribute will recover the $data from the yourfile.php..

    I don't know if you will understand what I say, i'm french so... ^^

    Let me know if it's working for u :)

  • allanallan Posts: 63,791Questions: 1Answers: 10,513 Site admin

    If you do echo $arrayMess; and then load that PHP file - what is the structure of your JSON data?

    Allan

This discussion has been closed.