2

I am displaying a custom table records using my custom form and included a select field in it so that when i select a product then only the products related to that term should be displayed and all other should hide. I am new to drupal and don't know how to start for this task.


Here is what i had done so far:

<?php

function formModule_display($form, &$form_state)
{

  // The form we are building
  $form = array();

 $result = db_query("SELECT o.nid, o.sku FROM {formmodule_order} o ");

    $options = array();
   foreach($result as $row) {
      $options[$row->nid] = $row->sku;

      if($options == 'pen-11') {
       print_r($options[$row->nid]);

      }
    }
    // Get the list of interview requests ordered by newest date.
  $query = db_select('formmodule_order', 'h')
    ->fields('h',array('nid','sku','orderIn','orderOut','created'))
    ->orderBy('h.nid', 'ASC');
    $result = $query->execute();

     $form['productid'] = array(
    '#type' => 'select', 
    '#title' => t('Select a product'), 
    '#options' => $options,
    '#description' => t('Choose an option.'),
   );


   $form['requests'] = array('#prefix' => '<div id="interview-request">',
                            '#suffix' => '</div>',
                            '#tree' => TRUE,
                            '#theme' => 'table',
                            '#header' => array(t('Product ID'), t('Product Code'), t('Product Stock'), t('Product Purchased'), t('Created')),
                            '#rows' => array(),
                            );
//foreach ($result as $row) { 
 while($row = $result->fetchAssoc()) {
     //   print_r($row);
    //$debug .= '<br/>Header Record #'.' <pre>' . print_r($row, true) . '</pre>';
  $nid = array('#id' => 'id',
                 '#type' => 'markup',
                 '#markup' => '<b>' . $row['nid']. '</b>',
                 );
    //drupal_set_message('<pre>' . print_r($nid, true) . '</pre>');
    $sku = array('#id' => 'sku',
                     '#type' => 'markup',
                    '#markup' => '<b>' . $row['sku'] . '</b>',
                 );
    //drupal_set_message('<pre>' . print_r($sku, true) . '</pre>');
   $orderin = array('#id' => 'orderin',
                 '#type' => 'markup',
                 '#markup' => '<b>' . $row['orderin']. '</b>',
                 );
            //   print_r($orderin);
    //drupal_set_message('<pre>' . print_r($nid, true) . '</pre>');
    $orderout = array('#id' => 'orderout',
                     '#type' => 'markup',
                    '#markup' => '<b>' . $row['orderout'] . '</b>',
                 );
    //drupal_set_message('<pre>' . print_r($orderOut, true) . '</pre>');
    $created = array('#id' => 'created',
                     '#type' => 'markup',
                    '#markup' => '<b>' . $row['created'] . '</b>',
                 );
    $form['requests'][] = array('nid' => &$nid,
                                'sku' => &$sku,
                                'orderIn' => &$orderin,
                                'orderOut' => &$orderout,
                                'created' => &$created,
                                );
    $form['requests']['#rows'][] = array(array('data' => &$nid),
                                         array('data' => &$sku),
                                         array('data' => &$orderin),
                                        array('data' => &$orderout),
                                         array('data' => &$created),
                                         );
    unset($nid);
    unset($sku);
    unset($orderin);
    unset($orderout);
    unset($created);



  }

  return $form;

} 

The code above just dispalys the whole record in the table. But I want to display record as per the seleted value through select field. Be;ow is the output of above code:


In the below image when i select any product (say,notebook-11) then only the record related to notebook-11 should only display. how do i relate the select option and database record? enter image description here

7
  • Please refer to this articles Using views with a custom table/schema. This will answer most of the query. If not please revert back to me we will discuss more to solve your problem. Here is a video about learning/using Views 2. It's more of a getting started and how-it-works type of video, but is pretty comprehensive. 33 minutes long Incase you are using Drupal 6 more detail to learn drupal views are here Feb 8 '13 at 6:14
  • afterrefering to the article-Using views with a custom table/schema i made changes in my custom form but now confused that how to debugg or see the working of the code
    – aashi
    Feb 8 '13 at 6:43
  • how you are listing your table via custom code or via views? Feb 8 '13 at 7:29
  • via custom code
    – aashi
    Feb 8 '13 at 7:35
  • I would suggest you to create a views type page with table view. Then use expose filter to expose field product code. Feb 8 '13 at 7:43

Your Answer

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

Browse other questions tagged or ask your own question.