[development] arrays

Tolga tolga at ozses.net
Tue Jun 14 19:19:46 UTC 2011


Hi,

I don't know if this is the right place to ask Drupal related PHP 
questions, so please bear with me. I've got two arrays and a form like 
below:

/*
  * This function stuffs currency codes into an array,
  * check the TCMB XML file for matching records, and
  * displays the relevant code's exchange rates in a table
  */
function tcmb_get_currencies() {
   $tcmb_cur_codes = array(
       1 => 'USD',
       2 => 'CAD',
       3 => 'XDR',
       4 => 'DKK',
       5 => 'SEK',
       6 => 'CHF',
       7 => 'NOK',
       8 => 'JPY',
       9 => 'SAR',
       10 => 'KWD',
       11 => 'AUD',
       12 => 'EUR',
       13 => 'GBP',
       14 => 'RUB',
       15 => 'RON',
       16 => 'IRR',
       17 => 'BGN',
       18 => 'DEM',
       19 => 'BEF',
       20 => 'LUF',
       21 => 'ESP',
       22 => 'FRF',
       23 => 'IEP',
       24 => 'ITL',
       25 => 'NLG',
       26 => 'ATS',
       27 => 'PTE',
       28 => 'FIM',
       29 => 'GRD'
);
$tcmbXML = new DomDocument();
$tcmbXML->load('http://www.tcmb.gov.tr/kurlar/today.xml');
$currency = array();
$rows = array();
$result = variable_get('tcmb_currency', $currency);

// Let's find out if $result is a variable
if (!is_array($result)) {
   watchdog('Error: The tcmb_currency variable was not an array.');
   drupal_set_message(t('Error: The tcmb_currency variable was not an 
array.'));
   return;
}
foreach ($result as $tcmb) {
   $tcmb_cur = $tcmb['currency'];
   $currency[] = $tcmb_cur;
   $curcode = $tcmb_cur_codes[$tcmb_currency];
   $buy = 
$tcmbXML->getElementsByTagName('ForexBuying')->item($tcmb_cur)->nodeValue;
   $sell = 
$tcmbXML->getElementsByTagName('ForexSelling')->item($tcmb_cur)->nodeValue;
   $rows[] = array($curcode, $buy, $sell);
}
$header = array('', t('BUYING'), t('SELLING'));
$output = theme('table', $header, $rows);
return $output;
}

...
...
...

/*
  * This function generates the form and allows the admin to set the 
currency code(s).
  */

function tcmb_settings_form() {
   $form = array();
   $form['tcmb_currency'] = array(
     '#title' => t('Currency code'),
     '#default_value' => variable_get('tcmb_currency', array()),
     '#type' => 'checkboxes',
     '#options' => array(1 => t('American Dollar'), 2 => t('Canadian 
Dollar'), 3 => t('Special D. Rights'), 4 => t('Danish Krone'), 5 => 
t('Swedish Krona'), 6 => t('Swiss Franc'), 7 => t('Norwegian Krone'), 8 
=> t('Japanese Yen'), 9 => t('Saudi Arabian Riyal'), 10 => t('Kuwaiti 
Dinar'), 11 => t('Australian Dollar'), 12 => t('Euro'), 13 => t('Great 
Britain Pounds'), 14 => t('Russian Ruble'), 15 => t('Romanian Leu'), 16 
=> t('Iranian Riyals'), 17 => t('Bulgarian Lev'), 18 => t('German 
Marc'), 19 => t('Belgian Franc'), 20 => t('Luxembourgian Franc'), 21 => 
t('Spanish Peseta'), 22 => t('French Franc'), 23 => t('Irish Pound'), 24 
=> t('Italian Lira'), 25 => t('Dutch Guilder'), 26 => t('Austrian 
Schilling'), 27 => t('Portuguese Escudo'), 28 => t('Finnish Mark'), 29 
=> t('Greek Drachma')),
   );
     return system_settings_form($form);
};

As you can see, I'm trying to return a table with the relevant rates in 
it. However, whatever I check, I get USD rates, and also it lists all 29 
values (all USD by the way) even if there are only 2. What do I need to do?

Regards,


More information about the development mailing list