Page 1 of 1

Dropdown Help Needed.

Posted: Tue Nov 18, 2003 9:56 am
by CSParker1
Well, here is the code for the dropdown I have been worlking on... So far it gives the appropriate category title to each dropdown. The trouble I am having is that it enters each of the products in the dropdowns 15 times, it adds an instance of the product for everytime it loops through the categories.

How can I get it to place each item in the dropdowns once instead of everytime it loops through the "categories" table?

Any help or suggestions is greatly appreciated!

BEGIN CODE:

Code: Select all

<?php
MYSQL_CONNECT($dbserver, $dbuser, $dbpass) OR DIE("Unable to connect to database"); 
@mysql_select_db( "$dbname") or die( "Unable to select database"); 
?> 
<? 
$result = mysql_query('select id, cat_name from category'); 

while ($row = mysql_fetch_array($result)) { 
echo $row["cat_name"].":<br>"; 
$id = $row["id"]; 

echo '<select name=product[]>'; 
$XX = "data empty!"; 
$aku = mysql_query("SELECT category_id, item_name, item_id FROM category, item WHERE $id=category_id ORDER BY cat_name"); 

while ($row = mysql_fetch_array($aku)) 
{ 
$colom_name=$row["item_id"]; 
$colom_name2=$row["item_name"]; 

echo "<option value=$colom_name>$colom_name2\n"; 
} 

if ($colom_name="") 
{ 
print ("$XX"); 
} 
echo "</select>\n"; 
print ("<br><br>"); 

} 
mysql_free_result($result);
?>

Posted: Wed Nov 19, 2003 12:30 am
by volka
the problem is certainly that your table is not in a normalized form.
select id, cat_name from category
...
SELECT category_id, item_name, item_id FROM category, item WHERE $id=category_id ORDER BY cat_name
all from the same table?

Could you please explain your table structure and what you want fetch from where?

Posted: Wed Nov 19, 2003 7:23 am
by CSParker1
It's fetching everything I need just fine from tables "category" and "item." The issue is that when the categories are listed above the drop down, each item is listed everytime it loops through the categories. So 15 categories, each item listed 15 times in the dropdowns. I need to figure out a way to only list each instance of each item one time.