Insert a line into a database for each row that has text in the input with PHP

Insert a line into a database for each row that has text in the input with PHP

JordanPHPJordanPHP Posts: 6Questions: 3Answers: 0

Hi all this is my first post so please be gentle.

I have a table that on each row has an input text, not all will be filled in all the time, so I need a way of checking if any row on any page has text in the input field and insert each row into a database if it has and to ignore it if it hasnt.

Has anyone done anything like this before and has a good way to go about this?

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,956Questions: 87Answers: 416
    Answer ✓

    Well you could set up an Editor instance for just this one field. Whenever it gets edited Editor would update the field in the database table as well.
    https://editor.datatables.net/examples/inline-editing/simple.html
    If you want to have an insert into a separate table each time somebody enters a text you could use the same logic as would be used for logging changes. If you do a log you make an insert into a log table for each change you are making. You could apply this to your field:
    https://editor.datatables.net/manual/php/events#Logging-changes

  • rf1234rf1234 Posts: 2,956Questions: 87Answers: 416
    edited February 2017

    In case you always want to display an empty field you can use a getFormatter that returns spaces. Then if the field gets edited Editor will update its value in the database. So let's assume the user cannot add new rows to the parent table but she can only edit the text field in existing rows. The field instance below returns spaces but saves edited text in the parentTable in myTextField.

    Field::inst( 'parentTable.myTextField' )
            ->getFormatter( function ( $val, $data, $opts ) {
                      return ' ';                     
               } ),
    

    After the update of the parent table you can do the insert into the child table in the same Editor instance.

    ->on( 'postEdit', function ( $editor, $id, $values, $row ) {
           if ($row['parentTable']['myTextField'] > ' ') {     
              $editor->db()->insert ('childTable', array (
               'parentTable_id' => $id,    
               'myTextField' => $row['parentTable']['myTextField']
            }
           ) );
    

    This assumes that the child table only has three colums: id, parentTable_id, myTextField.

    The result is that you are saving all text that users enter into the text field of the parent table.

This discussion has been closed.