Forums

How do I save an array into separate DB records?

tshirley 25 Dec, 2016
Hi,

I have a form using a multiplier which collects several rows of data into an array:

data => Array (
     [class] => Array
        (
            [0] => Open
            [1] => Open
        )

    [compid] => Array
        (
            [0] => XC
            [1] => SS
        )

    [starttime] => Array
        (
            [0] => 1435
            [1] => 1432
        )
) 


I want to write a record for each element in the array so that class[0], compid[0], and starttime[0] are in one record and class[1], compid[1], and starttime[1]. I can't do this with a standard DBSave so what is the correct solution?

Cheers

Tim
GreyHead 25 Dec, 2016
Hi Tim,

IIRC the trick is to name the inputs like data[_XNX_][class] instead of data[class][_XNX_] then you should get results in the form of an array like this
data => Array (
     [0] => Array
        (
            [class] => Open
            [compid] => XC
            [starttime] => 1435
        )
    [1] => Array
        (
            [class] => Open
            [compid] => SS
            [starttime] => 1432
        )
) 
and that can be saved using a DB Save with the Multi-save option.

Bob
tshirley 25 Dec, 2016
Thanks Bob,

Well it worked OK to set up the array, as you can see:


Array
(
    [option] => com_chronoforms5
    [chronoform] => start_time_input
    [event] => submit
    [data] => Array
        (
            [date] => 09.01.2017
            [country] => AUS
            [email] => tshirley@internode.on.net
            [0] => Array
                (
                    [class] => Open
                    [compid] => XC
                    [starttime] => 1444
                )

            [1] => Array
                (
                    [class] => Open
                    [compid] => SS
                    [starttime] => 1445
                )

            [comments] => Test 8
            [id] => 0
        )

    [button5] => Submit
)


And then I set the DBSave to multiple and the model ID to "data" but no records were saved. I suspect there is another trick in here somewhere...

Tim
GreyHead 25 Dec, 2016
Hi Tim,

You have a bunch of other stuff in the data array there. Do you need to have date, country, email, etc. in there as well? I suggest that you remove the data from those.

Bob
tshirley 25 Dec, 2016
Hi Bob,

No luck I am afraid, I removed all the "fixed" fields from the Model array but still have no records saved.


Array
(
    [option] => com_chronoforms5
    [chronoform] => start_time_input
    [event] => submit
    [date] => 09.01.2017
    [country] => AUS
    [email] => someone@gmail.com
    [data] => Array
        (
            [0] => Array
                (
                    [class] => Open
                    [compid] => XC
                    [starttime] => 1400
                )

            [1] => Array
                (
                    [class] => Open
                    [compid] => SS
                    [starttime] => 1401
                )

            [comments] => bbbb
            [id] => 0
        )

    [button5] => Submit
)


You are right that I don't need to store the fixed fields, except for date - if I have date in there, I don't need a master-detail setup and I can get what I need with one table.

So I can add the date with a hidden field and a for-each loop - no problem.

My table has columns date,class,starttime etc - I guess that is the correct naming in this case?

But I don't see why the DBSave doesn't work. I have save under model id = Yes, Enabled = Yes, Table selected, Model Id= data, Force = No.

Very puzzling.

Tim
GreyHead 25 Dec, 2016
Hi Tim,

The Debugger output should show the MySQL query being generated - do you see that if you scroll down the page?

Bob
tshirley 25 Dec, 2016
Ah, well there's the problem🙂 The query is empty:

Array
(
    [3] => Array
        (
            [DB Save] => Array
                (
                    [Queries] => Array
                        (
                        )

                )

        )

)


Maybe I need to recreate this form from scratch. I started from the demo multiplier example...

Tim
GreyHead 25 Dec, 2016
Hi Tim,

I think that it should work OK with the Model id as 'data' - though there is still a stray 'comments' in the data sub-array and an Id - not sure where that is coming from if the data isn't saving.

Bob
tshirley 25 Dec, 2016
Hi Bob,

Yes that was the problem, I had not removed all the items outside the actual array.

Off and running now.

Thanks heaps as always. Coffee on the way.

Tim
tshirley 26 Dec, 2016
But then... another problem...

OK i have a date in $form->data['inputdate'] which I want to add to each element of the array $form->data['data']. So I made a hidden field to create the element ['date'] inside the array but I can't get the foreach syntax right😟


<?php

foreach ($form->data['data'] as $d) {

     $d['date'] = $form->data['inputdate'];

}

?>


does not populate... this has to be a stupid mistake but I can't see it!

Cheers

Tim
tshirley 26 Dec, 2016
Cancel that,

I got it! needed ...as &$d... though I don't understand why.

Many thanks
GreyHead 26 Dec, 2016
Hi Tim,

Well done for getting it working :-)

I think I would probably have use code like this
<?php
foreach ($form->data['data'] as $k => $d) {
  $form->data['data'][$k]['date'] = $form->data['inputdate'];
}
?>

Bob
krotec 12 Jan, 2017
Hi there. I am doing a similar thing but I don't seem to be understanding what is meant by the [_XNX_]
<?php
foreach ( $form->data['order_items'] as $p ) {
$form->data[]['order_item_sku']= "{$p['order_item_sku']}";
$form->data[]['product_quantity'] = "-{$p['product_quantity']}";
}

?>

I want ['product_quantity'] and ['order_item_sku'] to show up in one array. Any help is greatly appreciated. Cheers Kevin
This topic is locked and no more replies can be posted.