View A Single Record

View A Single Record

Restful Web ServicesRestful Web Services Posts: 192Questions: 46Answers: 2

I suspect the answer to this is 'no' having read what I can found on the forum but I thought I would ask anyway.

I am creating a JSON data source which is accessed using the server side method as per one of the standard ways to use DataTables. It works incredibly well and my php file usually contains a number of joins and filters which allow me to request all the necessary data I require when viewing multiple records.

Normally I implement a 'View Record' button which allows the user to view a single record from the table. It seems a shame that in reality I already have the data for the single record available from my JSON data source but I cannot access it in a single record format, i.e. I don't want to display it as a table.

So, can you access the JSON data source in any other way to allow a single record to be viewed or is it not possible?



This question has an accepted answers - jump to answer


  • kthorngrenkthorngren Posts: 4,325Questions: 19Answers: 939

    I might be misunderstanding your question but you can use the Select extension to easily get the data of a selected row.

    Here is an example:


  • Restful Web ServicesRestful Web Services Posts: 192Questions: 46Answers: 2

    Hi Kevin,

    What I want to do though is use that data and pass it to a new page which displays the 'detailed' record, i.e. the data from the single row.

    I have found this on Stackoverflow which looks like it could send the row data?

    What works (version 1):
    1) JSON is just text. Text in a certain format, but just a text string.

    2) In Javascript, var str_json = JSON.stringify(myObject) gives me the JSON string.

    3) I use the AJAX XMLHttpRequest object in Javascript to send data to the server:

    request= new XMLHttpRequest()"POST", "JSON_Handler.php", true)
    request.setRequestHeader("Content-type", "application/json")

    4) On the server, PHP code to read the JSON string:
    $str_json = file_get_contents('php://input');
    This reads the raw POST data. $str_json now contains the exact JSON string from the browser.

    What do you think?



  • kthorngrenkthorngren Posts: 4,325Questions: 19Answers: 939
    edited November 2017

    Well, I don't know anything about PHP but I may be able to get you to step 2. Built a simple example using the Select extension. Click a row and the data in that row is put into a JSON string.

    I would think that you can then use AJAX to POST the JSON string to your PHP script.

    Note that I removed the 'Salary' column from the table to show that all the data returned for that row, whether in the table or not, can be put into the JSON string.

    Also used toArray() to put the data into an array before converting to JSON string.

    Hope this gets you started. Others may have alternate / better ideas.


  • allanallan Posts: 48,464Questions: 1Answers: 7,020 Site admin

    I think there are basically two options here:

    1. Use Select and row().data() as Kevin suggests to generate the new page with a custom Javascript function. You don't need to hit the server-side at all for this. That might be an advantage to you, it might not!
    2. Get the row id for the selected row (again row().data() could be used for that) and use that to open a new window with the id in the URL. If you want to reuse the Editor PHP libraries to get the data, you could add a condition so that only the data for that row is fetched. This way you can generate the page on the server-side.

    Its really up to you if you want to generate it on the client or the server-side.


  • Restful Web ServicesRestful Web Services Posts: 192Questions: 46Answers: 2

    Many thanks for your extremely useful answers.

    If possible I would prefer to be able to access the data on the server side. For example if currently I do something like this using the URL:


    I then extract the ID and use that to get the data, however, as already indicated I have to rewrite the library to access the data again as I don't know how to utilise the Datatables library again.

    Allan, I don't really understand what you mean by 'reuse the Editor PHP' libraries to get the data. Well, I know what you mean but I don't know how you would reuse the libraries. I know it is probably outside the remit of the Editor documentation but is there anywhere I can see information/help about your concept in more detail. I.e. what files do I need to access and how do I parse the ID to the fle?

    Many thanks

  • allanallan Posts: 48,464Questions: 1Answers: 7,020 Site admin
    Answer ✓

    You'd basically create another Editor::inst() but just with a ->where() condition attached to it, which would limit the data selected to just the required customer id.

    Another option would be to use a simple SQL SELECT for that one record. That would actually make things easier for a single record and also offer more flexibility than the Editor libraries can offer.


  • Restful Web ServicesRestful Web Services Posts: 192Questions: 46Answers: 2
    edited December 2017

    Hi Allan,

    Thanks for your response. I have had a go and it seems to work. This is just some quick proof of concept code but it works so shows the approach is possible.

    $url = $config['root_url'].'/uploads/js/system/dataTables/php/table.t_booking.php';
    $data = array('where_1' => 'where', 'columnName_1' => 't_booking.iBookingId', 'value_1' => '15728', 'operator_1' => '=', 'bound_1' => 'true');
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    if ($result === FALSE) { 
        /* Handle error */ 
        $json = json_decode($result, true); 

    Thanks for your help. I can now present my 'Detail' view using the same code which is used to generate the summary datatable so this should help keep things efficient and tidy.

Sign In or Register to comment.