Jump to content

SELECT BOX WITH A VARIABLE


jwk811

Recommended Posts

here the code for the select box on the form:
<td class="content"> <select name="cboCategory" id="cboCategory" class="box">
     <option value="" selected>-- Choose Category --</option>
<?php
echo $categoryList;
?>
    </select></td>
this must be set up wrong because what comes out when you go to drop down the box for options it shows the options that were in the category list but they are not clickable so it has to stay with choose category-- thing.. could you please tell me how i could make this work? thanks very much for any help!

and the variable $categoryList is buildCategoryOptions($catId)
and heres the script for this function:
[code]function buildCategoryOptions($catId = 0)
{
$sql = "SELECT cat_id, cat_parent_id, cat_name
FROM tbl_category
ORDER BY cat_id";
$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());

$categories = array();
while($row = dbFetchArray($result)) {
list($id, $parentId, $name) = $row;

if ($parentId == 0) {
// we create a new array for each top level categories
$categories[$id] = array('name' => $name, 'children' => array());
} else {
// the child categories are put int the parent category's array
$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
}
}

// build combo box options
$list = '';
foreach ($categories as $key => $value) {
$name    = $value['name'];
$children = $value['children'];

$list .= "<optgroup label=\"$name\">";

foreach ($children as $child) {
$list .= "<option value=\"{$child['id']}\"";
if ($child['id'] == $catId) {
$list.= " selected";
}

$list .= ">{$child['name']}</option>\r\n";
}

$list .= "</optgroup>";
}

return $list;
}[/code]
if that has anything to do with it
Link to comment
https://forums.phpfreaks.com/topic/25759-select-box-with-a-variable/
Share on other sites

I suppose my question to you is this...what value did you assign to $categoryList? And can you please show the rest of the code. If you are pulling it from a db, and the variable is an array, you will have to use a loop to display the diferent values, not to mention, adding the html surrounding those values. But I would have to see the rest of your code to determine.
yep.. i added the rest of the code above.. thought you'd need that.. it is coming from a db as you can see.. could you give me an example of like how to have it come out onto the select form thing? i wouldnt be able to do each little piece individually since it will be changing constantly.. its for a cart system.. would you know what to do? thanks
oh nevermind man.. what was happening was it was being used as a main category therefore it wasnt supposed to clickable because there had to be a child category in it in order to be able to add the product..(in the cart system) i got it now.. thanks anyways for your help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.