Forums

Checkbox group from multiline text list

blissj 01 Mar, 2015
OK so here's the issue. I have a form that has a checkbox group with over 50 items in it. This list of items changes on a weekly basis. The person that makes the changes to this list is not very tech literate and I would rather not have them inside chronoforms editing the form. My idea was to just make another form for them to paste the list into which would save that data to a table and then have the checkbox group use dynamic data. My problem comes from the fact that the original list just a multi-line text document. I can make a form with a textarea for them to paste it into and that will be saved to the db, but when I try to use the dynamic data feature I only get one checkbox with a whole lot of text. I could paste the list into the options box on the general tab of the checkbox options but it looks like the dynamic data option requires the list of options to be separated in the array. This is where I'm stuck. I don't know how to get the multiline formatted text into the checkbox options or I guess somehow convert the multiline pasted text into an array of values in the first form. Any help would be appreciated. Thanks.
GreyHead 01 Mar, 2015
Hi blissj,

I think that you need to add some code to the update form that will take the pasted list and clean it up into a set of separate entries for saving to the database table.

I guess that this would mean separating out the lines, removing any empty ones; validating what is left and putting all of that into an array that is suitable for saving.

Bob
blissj 01 Mar, 2015
Do you have any suggestions on what that code may look like? Even a link to a similar thing and I can take it from there. I'm just not sure where to start or what to look for.
GreyHead 01 Mar, 2015
Hi blissj,

Can you post an example of what you are starting with - what does the multi-line text document look like?

Bob
blissj 01 Mar, 2015
An example is below. It's just a simple list.



ABE SAPIEN 
ACTION COMICS 
ADVENTURE TIME 
AFTERLIFE WITH ARCHIE 
ALEX + ADA 
ALICE COOPER 
ALIENS FIRE AND STONE 
ALL NEW INVADERS 
ALL NEW X-FACTOR 
ALL-NEW GHOST RIDER 
ALL-NEW ULTIMATES 
ALL-NEW X-MEN 
AMAZING SPIDER-MAN 
AMAZING X-MEN 



[attachment=0]example1.txt[/attachment]
blissj 02 Mar, 2015
I've been messing with custom code and have tried using both preg_split and explode with no luck. The debugger isn't showing me any useful information so I'm somewhat at a stand still. It's hard to believe that this has never been done before but I'm having trouble finding any examples.
blissj 02 Mar, 2015
Code I'm trying:

<?php
$form->data["comlist"] = preg_split("/\r\n|\n|\r/", $Textarea);
// $Textarea = nl2br($Textarea);
// $form->data["comlist"] = explode("<br />", $Textarea);
?>


Debugger:

Array
(
    [option] => com_chronoforms5
    [chronoform] => ChronoComic
    [clist] => Array
        (
            [id] => 1
            [uniq_id] => 9c1a725deefc7e24848807062c3b539e5aba6c05
            [user_id] => 666
            [created] => 2015-03-02 00:19:40
            [modified] => 
            [Textarea] => ALIENS FIRE AND STONE 
ALL NEW INVADERS 
ALL NEW X-FACTOR 
ALL-NEW GHOST RIDER 
ALL-NEW ULTIMATES 
ALL-NEW X-MEN 
AMAZING SPIDER-MAN 
AMAZING X-MEN 
            [button2] => Submit
            [list] => 
        )

    [comlist] => Array
        (
            [0] => 
        )

)
GreyHead 02 Mar, 2015
Answer
Hi blissj,

This code works to create a usable drop-down.
<?php
$text = $form->data['textarea1'];
$text = explode("\n", $text);
$options = array();
$i = 0;
foreach ( $text as $t ) {
  $t = trim($t);
  if ( !$t ) {
    continue;
  }
  $options[$i]['value'] = $t;
  $options[$i]['text'] = $t;
  $i++;
}
$form->data['options'] = $options;
?>
You can see it working here

This is doing an immediate refresh - in your case you probably need to save the new options array into a database record that you can reload in the live form. I think this will let you save:
<?php
$form->data['save_options'] = json_encode($form->data['options']);
?>


And this will reload and unpack in the On Load event of the live form - after a DB Read to get it.
<?php
$form->data['options'] = json_decode($form->data['save_options']);
?>

Alternatively you could save to a file and include that.

Bob
blissj 02 Mar, 2015
It's still not working for me. It seems so simple but it's just not happening. When I run some test data and look at the debug info it looks like it is just making an empty array. I'm pulling the textarea data from a db then running it through the custom code.

Code:
<?php
$text = $clist->Textarea;
$text = explode("\n", $text);
$options = array();
$i = 0;
foreach ( $text as $t ) {
  $t = trim($t);
  if ( !$t ) {
    continue;
  }
  $options[$i]['value'] = $t;
  $options[$i]['text'] = $t;
  $i++;
}
$form->data['comlist'] = $options;
?>


Debug:
Array
(
    [option] => com_chronoforms5
    [chronoform] => ChronoComic
    [clist] => Array
        (
            [id] => 16
            [uniq_id] => 395a88237c679c6b3a2efaf66fe3a49607a53607
            [user_id] => 666
            [created] => 2015-03-02 15:37:07
            [modified] => 
            [Textarea] => bob
bill
bliss
brad
bud

            [button2] => Submit
            [list] => []
        )

    [comlist] => Array
        (
        )

)
blissj 02 Mar, 2015
I figured it out. Thank you very much for your help. The issue was with how I was trying to grab the data from the DB. I was trying to pull it as an array when it was not one.
This topic is locked and no more replies can be posted.