1 reply [Last post]
glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
Was this information Helpful?

Hi

I am writing a module which dynamically build a table with a number of rows, and then adds a number of forms defined by $order->qty - count(table_rows).

The forms need to have the order_id as a part of the field and submit names, otherwise they are all the same.

How can I pass the order_id dynamically to the form generation?

My code currently is:

<?php
function license_codes() {
    global
$user;
    global
$oid;
   
drupal_set_title(t('License Codes'));
 
   
// For each order id, get the codes that exist
   
$query = db_query("SELECT uc_orders.order_id AS oid, uc_order_products.qty AS qty, created as date FROM {uc_order_products} LEFT JOIN {uc_orders} ON uc_orders.order_id = uc_order_products.order_id WHERE uid = %d AND uc_order_products.nid = '30' AND uc_orders.order_status = 'completed' || uc_orders.order_status = 'payment_received'", $user->uid);
   
$orders = array();
   
$codes = array();
   
$header = array();
   
$rows = array();
    while (
$value = db_fetch_array($query)) {
     
$codes = NULL;
       
$codes_query = db_query("SELECT serial, license_code FROM {license_codes} WHERE order_id = %d", $value['oid']);
        while (
$codes_array = db_fetch_array($codes_query)) {
         
$codes[] = array(
           
'serial' => $codes_array['serial'],
           
'license_code' => $codes_array['license_code'],
          );
        }
       
$orders[] = array(
         
'oid' => $value['oid'],
         
'qty'=> $value['qty'],
         
'codes' => $codes,
        );
    }
   
   
// Build a table of the existing codes
 
$header = array(
   
t('Serial Number'),
   
t('License Code'),
  );
    foreach (
$orders as $table) {
      if (
$table['codes']) {
       
$codes = $table['codes'];
        foreach (
$codes as $code) {
          
$rows[] = array(
         
$code['serial'],
         
$code['license_code'],
        );
        }
      }
     
$output .= '
        <fieldset>
        <legend><b>Order No. '
. $table['oid']. '</b></legend>
        <p>Order Date: '
. $table['date'] .'</p>
      '
;
      if (
$rows) {
       
$output .= theme(table, $header, $rows);
      }
   
// If there are spaces available to generate more codes, put a form in
   
$missing = $table['qty'] - count($rows);
    for (
$i = 1; $i <= $missing; $i++) {
        
$oid = NULL;
        
$oid = $table['oid']; //<-- THIS ISN'T PASSING PROPERLY TO THE SUBMIT FUNCTION
        
$output.= drupal_get_form('license_codes_new');
      }
     
$output .= '</fieldset>';
   
$rows = NULL;
    }
    return
$output;
}

function

license_codes_new() {
  global
$oid;
 
$serial = 'serial_'. $oid;
 
$submit = 'submit_'. $oid;
 
$form[$serial] = array(
   
'#type' => 'textfield',
   
'#size' => 25,
   
'#title' => t('Serial Number'),
  );
 
$form[$submit] = array(
   
'#type' => 'submit',
   
'#value' => t('Obtain license code'),
   
'#prefix' => '<div class="submit">',
   
'#suffix' => '</div>',
  );
 
$form['oid'] = array(
   
'#type' => 'hidden',
   
'#value' => $oid,
  );
 
$form['markupoid'] = array(
   
'#type' => 'markup',
   
'#value' => 'OID = '. $oid,
  );
  return
$form;
}
?>

I have the markup field in the form for testing at the moment. The markup field in the form displays the order_id perfectly, but it is not passing to the submit function, at the submit function, $form_state['values']['oid'] always has the order_id from the first form on the page.

Thanks for any help.

Glenn

glennnz's picture
Offline
Joined: 01/20/2009
Juice: 451
SOLVED