getting output from checkboxes

Hi Guys,

I'm using the checkboxes input to allow users to select a whole bunch of features found on/at an event or thing...But am having difficulty extracting their responses.

in my form i call

elgg_view('input/checkboxes',array('internalname' => 'features' , 'options' => array(elgg_echo("Original") => "Original" , elgg_echo("Unoriginal") => "Unorginal" , etc

so I believe the responses saved from the form (to object 'compost') will be as an array.

I've tried this to get the responses back (I just wanted them as printed text - just the checkboxes they ticked)...but it returns nothing.

                    $featurelisting = $compost->features;
                    $info = elgg_view('output/checkbox', array($featurelisting));

Any hints as to where I've gone wrong? Thanks in advance...

 

  • Where do you process the form submission? Usually you would have some action process the results and grab the data from that input, ie:

    $features = get_input('features');

  • yes in the action related to the comments it saves the 'features' (internalname) to the comment.

    i.e

    $comment_features = get_input('features');

                    $thecomment->features = $comment_features;
                    // as well as other values       
                    $thecomment->save();

    is it here the checkboxes array for 'features' needs to be translated or worked with before saving to thecomment? I figured the array was being saved ok to $thecomment and just needed to be pulled out a specific way later on like when doing the info = elgg_view(output/checkboxes etc

    OH MY GOD! the output view being called is checkboxes not checkbox.............*sigh!* haha......

  • Haha, using the right view helps :) 

    One thing though, you won't be able to save the array right into the metadata. Metadata is stored as strings. If you want to store the array intact you'll have to serialize it first, and then unserialize later. 

  • cheers, mate!

    any pointers on unserialising an array?

    I'm reading into this:

    http://www.php.net/manual/en/function.serialize.php

  • Nothing to it really, just serialize the array first, ie: 

    $comment_features = serialize(get_input('features'));

    Store that as metadata in your entity

    $thecomment->features = $comment_features;

    $thecomment->save();

    Then, when you need to get it later, and want to turn it back into an array you would do:

    $something = unserialize($thecomment->features);

  • Great - thanks Jeff - super appreciated :D

  • Hi Geniuses - back to this one again;

    for each individual post I can pull down the array of 'features' and present them as desired by doing this:

    $featurelisting = unserialize($compost->features);

    foreach ($featurelisting as $featureindiv) {

    $info .= "{$featureindiv}   ";

    }

    ...any text entry that has been checked is presented one after the other. Great! :-)

     

    But 

    I'd also like to be able to create an array of features found by all users (so all comments) and then unique_array it so its not double ups...

     

    $ratings = elgg_get_entities_from_metadata (Get the comments)

    foreach ($ratings as $rating) {

    $featuresvalue[] = $rating->features;

    //This reports a fatal error!!!

    }

     

    $featurelisting = unserialize($featuresvalue);

    $featurelist = array_unique($featurelisting);

    foreach ($featurelist as $featureindiv) {

    $area2 .= "{$featureindiv}  ";

    }

     

    Any ideas what i can do about the:

    Fatal error: [] operator not supported for strings in ....

    ...I've spent a good while refering the Php arrays manual, but I'm stumped - any help appreciated.

     

    Thanks,

    Jimbo

     

  • Have you remembers to initialise $featuresvalue?

    Something like

    $featuresvalue = array();

    Otherwise you will get the error because until a variable is set to an array ... it isn't!

  • Ha! Kev - you're a genius (AGAIN!) ...another array in a similar scenario worked fine because I was reseting it to blank by doing exactly that $string = array() ...Had I done the same thing with my $featurevalue it would have worked fine....sometimes the simplest things are staring you in the face! :)

    Thanks again :-)