Howdy all.

I am building a drop down from a database call and NID values for product pages aren't sequential like the select is attempting to populate. Here is the HTML code that is being generated:

select name="sku" class="form-select" id="edit-sku" > optgroup label="0"> option value="0">FWCL-O-LR1 /option> option value="1">FWCL-O-LR2 /option> /optgroup> /select>

I would prefer that ' option value="x"> ' would be populated with the NID value for the product related to FWCL-O-LR1. Here's a sample of my code. Can anyone make any suggestions? Am I missing something in creating / populating $arrey?

#####
function farrowmedical_product_form($form_state) {

$title = explode("/", $_GET['q']); // Title holds the name of the product we are looking at. (page name)
$result = db_query("SELECT * FROM `{fmi_products}` WHERE `{category}` = '{$title[2]}' order by weight asc");
$count = 0;

while ($data[] = db_fetch_object($result)) {
$arrey[$count] = $data[$count]->model;
$count++;
}

$period = array($arrey);

if($arrey) {
$form['sku'] = array(
'#type' => 'select',
'#title' => t("Farrow Medical $title[2]"),
'#options' => $period,
'#description' => t("$title[2]"),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'));
}
else {
$form['sku'] = array(
'#type' => 'select',
'#title' => t("Farrow Medical $title[2]"),
'#options' => $period,
'#description' => t("There is no product to display"), );
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Return'));
}
return $form;
}

Comments

nevets’s picture

If fmi_products has nid as a field you can do

while ($data = db_fetch_object($result)) {
$arrey[$data->nid] = $data->model;
}

Note the $period = array($arrey); should probably be $period = $arrey though thats not really needed as you could either use $period in the while loop or $arrey as the option value.

DLBaker’s picture

I appreciate the input, and it did set me on the right path. The following code is what *mostly* works. It populates the list and adds a few extras in too. what the heck is an optgroup?

while ($data[] = db_fetch_object($result)) {
$arrey[$count]= array($data[$count]->nid => $data[$count]->model );
$count++;
}

Produces HTML Code as such, leading tags substituted with a dash:

-select name="sku" class="form-select" id="edit-sku" > -optgroup label="0"> -optgroup label="0"> -option value="2">FWCL-O-LR1 -/option> -/optgroup>-optgroup label="1"> -option value="3">FWCL-O-LR2-/option> -/optgroup> -optgroup label="2"> -option value="4">FWCL-O-LR3-/option> -/optgroup> -/optgroup> -/select>

It produces a drop down that looks like so:
0
0
FWCL-O-LR1
1
FWCL-O-LR2
2
FWCL-O-LR3

I would expect a nesting issue if I had 3 '0's and 2 '1's But this doesn't seem to be 'symmetric' I am at a complete loss as to where these are coming from. There's just the three lines in the database for testing.

Has anyone else seen this before or have any ideas?

- Dan.

DLBaker’s picture

Ok, so sometimes I can be a moron.

I went back to what had been offered and realized I had neglected to remove the brackets from my opening while statement. After that it worked perfectly.

The following is the full function:

function farrowmedical_product_form($form_state) {
$title = explode("/", $_GET['q']); // Title holds the name of the product we are looking at. (page name)
$result = db_query("SELECT * FROM `{fmi_products}` WHERE `{category}` = '{$title[2]}' order by weight asc");

while ($data = db_fetch_object($result)) {
$arrey[$data->nid] = $data->model;
}

if($arrey) {
$form['sku'] = array(
'#type' => 'select',
'#title' => t("Farrow Medical $title[2]"),
'#options' => $arrey,
'#description' => t("$title[2]"),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'));
}
else {
$form['sku'] = array(
'#type' => 'select',
'#title' => t("Farrow Medical $title[2]"),
'#options' => $arrey,
'#description' => t("There is no product to display"), );
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Return'));
}

return $form;
}