Use PHP Editor data on the server

Use PHP Editor data on the server

rf1234rf1234 Posts: 2,818Questions: 85Answers: 406
edited February 7 in Editor

I am making an Excel spreadsheet server side using PHP Spreadsheet. The workbook has two sheets the first gets its data from the client and the second one uses a PHP Editor instance to retrieve the data.

For the first sheet I use this which works fine:

For the second one I call a function that holds the Editor instance and make sure it doesn't try to return anything to the client which doesn't exist in this case. I don't know how to return that data from the Editor instance to my PHP so I just set a SESSION variable with the data and take it from there.

Is there a better way to use PHP Editor instances to retrieve data that are not returned to the client?

$editor = Editor::inst( $db, 'sub_order' )
....
->on( 'postGet', function ( $editor, &$data, $id ) use ($ctrId) {     
        .....
        //prepend the sub projects to the data read
        $data = array_merge($subProjects, $data);
        if ( $ctrId > 0 ) {
            $_SESSION['sub_order_data'] = $data;
        }
    })
->process($_POST);
if ( $ctrId <= 0 ) {
    $editor->json();
}

This question has accepted answers - jump to:

Answers

  • rf1234rf1234 Posts: 2,818Questions: 85Answers: 406

    @allan: Would you have an idea? Can I do this better?

    Roland

  • allanallan Posts: 62,246Questions: 1Answers: 10,211 Site admin
    Answer ✓

    Hi Roland,

    Apologies, I missed this thread!

    Yes, there is a ->data() method on the Editor instance that will get you the data an an associative array.

    You could think of ->json() as:

    $data = $editor->data();
    echo json_encode($data);
    

    That is more or less what it does internally.

    Then do $data['data'] to get the array of data that would have been used to populate the DataTable.

    Allan

  • rf1234rf1234 Posts: 2,818Questions: 85Answers: 406
    edited February 8

    Thanks, Allan. That's good to know.

    I am quite happy with this solution to make nicer spreadsheets server side. I don't need any kind of additional rendering and can choose whether I use rendered client side data from the Data Table or "getFormatted" server side data from Editor or a mix of both.

    One of my use cases - just to share:
    - Whenever a user makes changes in fairly complex data that are aggregated in one very large data table and edited in almost 10 different Editors, then "onSubmitSuccess" I generate a new spreadsheet and put a link to it in the "documentation" column of my data table. The previous version gets moved into the "deleted documentation" column. Nothing ever gets deleted, only soft deleted. I only have one spreadsheet version per day and user, to avoid having hundreds of recoverable spread sheets. So that is the exception: Those previous versions of the same day and the same user get physically deleted.

    Since server side solutions for spreadsheets allow you to use templates they look much nicer than the usual Excel export:

  • allanallan Posts: 62,246Questions: 1Answers: 10,211 Site admin
    Answer ✓

    Looks nice! Great work on the Excel output formatting.

    Allan

Sign In or Register to comment.