Dropdown Help Needed.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CSParker1
Forum Newbie
Posts: 2
Joined: Tue Nov 18, 2003 9:56 am

Dropdown Help Needed.

Post 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);
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
CSParker1
Forum Newbie
Posts: 2
Joined: Tue Nov 18, 2003 9:56 am

Post 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.
Post Reply