0

I tried to display the values from database but failed to display.,

First I inserted the values using checkbox.,

codes are below,

$fieldset->addField('city', 'checkboxes', array(
                'label' => $this->__('City'),
                'name' => 'city[]',
                'required' => true,
                "checked" => $city,
                'values' => array(
                    array('value' => '0', 'label' => 'aaaaa'),
                    array('value' => '1', 'label' => 'bbbbbbb'),
                    array('value' => '2', 'label' => 'ccccccc'),
                    array('value' => '3', 'label' => 'dddddddd'),
                    array('value' => '4', 'label' => 'eeeeeeee')
                ),
                'onclick' => "",
                'onchange' => "",
                'disabled' => false,
                'value'  => '1',
                'tabindex' => 1
            )); 

after that,

$city = $post_data['city'] = implode(',', $post_data['city']);

using above code inserted successfully like(0,1,2),

 if ($object->getData('city')) {
                $city = $object->getData('city');
                $city = explode(",", $city);
                 //var_dump($city);die;
            }

inserted value I get successfully, Now I want to display the checked one(0,1,2)

Any one please help to solve this!!!!!!

  • $city = explode(",", $city); what you get here, in array format like array(0=>0,1=>1,2=>2) right? – saravanavelu Jun 18 '15 at 5:03
  • @saravanavelu yes – Sathish Jul 8 '15 at 4:45
  • while edit the form with particular record need to show only selected value right? – saravanavelu Jul 8 '15 at 4:56
  • @saravanavelu yes – Sathish Jul 8 '15 at 4:58
  • check my answer it works – saravanavelu Jul 8 '15 at 6:49
1

Update the code as below,

   $fieldset->addField('city', 'checkboxes', array(
                    'label' => $this->__('City'),
                    'name' => 'city[]',
                    'required' => true,
                    "checked" => $city,
                    'values' => $this->getOptionValues($city),
                    'onclick' => "",
                    'onchange' => "",
                    'disabled' => false,
                    'value'  => '1',
                    'tabindex' => 1
                ));

Create a new function to get the option values,

 public function getOptionValues($city)

     $result = array();

     $selectedvalues =  $city; //exploded array

     $optionslists = array(
                            array('value' => '0', 'label' => 'aaaaa'),
                            array('value' => '1', 'label' => 'bbbbbbb'),
                            array('value' => '2', 'label' => 'ccccccc'),
                            array('value' => '3', 'label' => 'dddddddd'),
                            array('value' => '4', 'label' => 'eeeeeeee')
                        );

    if(count($selectedvalues)>1){
        foreach($selectedvalues as $selectedvalue ){
                foreach($optionslists as $optionslist)
                {
                    if(in_array($selectedvalue, $optionslist))
                    {
                        $result[] = $optionslist;
                    }

                }
            }
    }
    else{
       $result = $optionslists; 
    }
    return $result;
1

Try this:

$city = $post_data['city'];
foreach($city as $k => $c){
   if(isset($city[$k])){
      echo $c;
   }
}

I haven't tested, but should give you the checked one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.