Saving multiple values in a row to another database field

Saving multiple values in a row to another database field

d-podd-pod Posts: 8Questions: 3Answers: 0

Hello,

I have a dropdown in my editor that provides two columns per row from the database for selection. The selection is saved to only one column in another table. When I save a selection, only the first field is written to the database. Is it possible to save the whole row (both fields from the two columns)?

My Dropdown:

getFields.php

<?php

// DataTables PHP library
include( "../../../assets/plugins/custom/editor/lib/DataTables.php" );

// Get fields
$sql = "SELECT  Column1, Column2 FROM table";
$query = $db->sql($sql)->fetchAll();
$data = [];
foreach ($query as $value) {
        $data[] = [
            $value['Column1'],
            $value['Column2']
        ];
}

echo json_encode($data);

Datatables:

$('#example').DataTable( {
                dom: "Bfrtip",
                ajax: {
                    url: "ajax.php",
                    type: "POST"
                },
                serverSide: true,
                columns: [
                    { data: "ColumnInAnotherTable" },
                ],
                select: true,
                buttons: [
                    { extend: "create", editor: supplier },
                    { extend: "edit",   editor: supplier },
                    { extend: "remove", editor: supplier }
                ],
                pageLength: 10
            } );

Editor:


example = new $.fn.dataTable.Editor({ ajax: "ajax2.php", table: "#example", fields: [ { label: "ColumnInAnotherTable", name: "ColumnInAnotherTable", type: "select", options: getFields(), } ] });

Get fields function:

function getFields {
                var options = null;
                $.ajax({
                url: ".getFields.php",
                    async: false,
                    success: function(result) {
                        options = JSON.parse(result);
                    }
                });
                return options;
            }

Answers

  • allanallan Posts: 63,456Questions: 1Answers: 10,465 Site admin

    Sorry I'm a little confused. You only have a single field configured in the Javascript Editor code there. If you want it to submit two fields, you'd need to add another field.

    What are you using to write the values to the database? Are you using our Editor PHP libraries? If so, you could use that to also populate the table.

    Allan

  • d-podd-pod Posts: 8Questions: 3Answers: 0

    I have the solution. Instead of separating field 1 and field 2 in the foreach loop with a comma, I concatenated them with a period and put a space in between. That already took care of the problem. Thanks for your help Allan.

    <?php
    // DataTables PHP library
    include( "../../../assets/plugins/custom/editor/lib/DataTables.php" );
    
    // Get fields
    $sql = "SELECT  Column1, Column2 FROM table";
    $query = $db->sql($sql)->fetchAll();
    $data = [];
    foreach ($query as $value) {
            $data[] = [
                 $value['Column1']. ' ' .$value['Column2']
            ];
    }
     
    echo json_encode($data);
    
Sign In or Register to comment.