0

In the process of creating data base, I need to read the Name, Father Name, Gender and Language. I could able to successfully store the data for all the fields stated above. When it comes to language field, I need to provide a checkbox option where user can select two or three options. But the problem is, it is not showing up in the proper format.

enter image description here

In database I used language field as varchar(50):

my_register.module

 <?php
 // $Id$

 /**
 * Implement hook_help().
 */
 function my_register_help($path, $arg) {
   if ($path == 'admin/help#my_register') {
      return t('Fill the registration form by user');
   }
 }

 /**
 * Implements hook_menu().
 */
 function my_register_menu() {
   $items = array();

   $items['admin/registration'] = array(
     'title' => 'Registration',
     'description' => 'Configuration for the My register Module.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('my_register_form'),
     'access arguments' => array('administer users'),
     'type' => MENU_NORMAL_ITEM,
    );

   $items['admin/registration/show'] = array(
    'title' => 'View Registered User',
    'description' => 'Tabuler disply of registered user',
    'page callback' => 'my_register_table_page_display',
    'access callback' => TRUE,
    'access arguments' => array('administer users'),
   'type' => MENU_NORMAL_ITEM,
  );


 return $items;
}

/**
* Form builder.
*/
function my_register_form($form, &$form_state) {
  // Text for Name.
  $form['my_register_uname'] = array(
   '#type' => 'textfield',
 '#title' => t('Enter Name'),
 '#description' => t('Enter the user name'),
 '#size' => 40,
 '#maxlength' => 120,
 '#required' => TRUE,
  );
// Text for F_name.
 $form['my_register_fathername'] = array(
'#type' => 'textfield',
'#title' => t('Father Name'),
'#description' => t('Enter the Father name'),
'#size' => 40,
'#maxlength' => 120,
'#required' => TRUE,
 );

// Text for contact number.
 $form['my_register_phone'] = array(
'#type' => 'textfield',
'#title' => t('Contact Number'),
'#description' => t('Mention 10 digit Valid Contact Number'),
'#size' => 20,
'#maxlength' => 10,
'#required' => TRUE,
 );

 // Text for Email Id.
 $form['my_register_email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail Address'),
'#description' => t('Mention valid E-mail address'),
'#size' => 30,
'#maxlength' => 50,
'#required' => TRUE,
  );
  // Radio for gender
  $selectgender = array(0 => t('Male'), 1 => t('Female'), 2 => t('other'));
  $form['gender']['selectgender'] = array(
  '#type' => 'radios',
 '#title' => t('Select Gender'),
'#default_value' => isset($node->selectgender) ? $node->selectgender : 0,
'#options' => $selectgender,
 );

  //Textarea for Address.
  $form['my_register_address'] = array(
 '#type' => 'textarea',
 '#title' => t('Postal Address'),
 '#description' => t('Postal address'),
 '#rows' => 5,
 '#columns' => 40,
 '#required' => TRUE,
  );  
  // Drop down for Select education
  $form['education'] = array(
 '#title' => t('Select which class you have Passed'),
 '#type' => 'select',
 '#description' => 'Select the Class whic you have passed',
 '#options' => array(t('--- SELECT ---'), t('10th'), t('12th'), t('BCA'), t('BBA'), t('B-Tech'), t('MCA'), t('M-Tech')),
  );
  // Check box for select language
  $form['my_register_language'] = array(
 '#type' => 'checkboxes',
 '#options' => drupal_map_assoc(array(t('Hindi'), t('English'), t('other'))),
   '#title' => t('Select language you Known'),
   );

  //submit button
  $form['submit'] = array(
   '#type' => 'submit',
   '#value' => t('Submit'),
   '#submit' => array('my_register_form_submit'),
   '#validate' => array('my_register_form_validate'),
  );

return $form;
 }

  $friend_ids = serialize(array_filter($form_state['values']['my_register_language'])); 
  global $user;
   db_insert('my_register')
     ->fields(array(
     'rid' => $user->rid,
   'uname' => $form_state['values']['my_register_uname'],
   'father_name' => $form_state['values']['my_register_fathername'],
   'contact' => $form_state['values']['my_register_phone'],
   'email_id' => $form_state['values']['my_register_email'],
   'address' => $form_state['values']['my_register_address'],
  'education' => $value_edu,
  'language' => $friend_ids,      
  'gender' => $value_gender,

   ))->execute();
   drupal_set_message("Successfully user registered");
   }



    function my_register_table_page_display() {

     $header = array(
    array('data' => t('Rid')),
    array('data' => t('Name')),
     array('data' => t('Father Name')),
    array('data' => t('Address')),
    array('data' => t('Contact')),
   array('data' => t('E-mail Id')),
   array('data' => t('Education')),
   array('data' => t('Gender')),
   array('data' => t('Language')),
   );

$query = db_query("SELECT * FROM {my_register}");


     $rows = array();
           foreach($query as $res) {
         $rows[] = array($res->rid, $res->uname, $res->father_name, $res->address, $res->contact, $res->email_id, $res->education, $res->gender, $res->language,);
      }
        // If rows are not empty theme and display the rows.

        $output = '';

        if (!empty($rows)) {

       $output = theme('table', array('header' => $header, 'rows' => $rows));



      }

     else {

        $output .= t("No results found.");

     }

     return $output;

    }
  • It seems that this question is about code you wrote. If so, it is impossible to answer without seeing this code first. – Mołot Aug 21 '15 at 15:53
  • From the looks of it, you're displaying your checkbox option array, not the selected value from your DB table. But as Molot says, it's impossible to tell without seeing the custom form, DB tables, & your table display code. – Shawn Conn Aug 21 '15 at 16:28
  • Hello, Molto i Have update my qustion with code – Ankit Gupta Aug 22 '15 at 4:02
1

Looks like you'll need to unserialize the data:

$arr = unserialize($result->my_field);

Then you can turn the array into a string (or whatever):

$str = implode(', ', array_filter($arr));

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