Forum Moderators: coopster

Message Too Old, No Replies

dynamic drop down recursive

look up data on two tables and produce dropdown form

         

gecko001

10:22 am on Jul 22, 2007 (gmt 0)

10+ Year Member



Hi Guys i am trying to pull data for two tables like so.
scm_store_cat
cat_id cat_parent_id
1 0
2 0
3 2
4 2
5 1
6 1
7 3
8 3
9 4
10 4
21 14
12 9
13 9
14 9
16 4
17 4

and the second table

scm_store_cat_desc

cat_id cat_title
1 Australia
2 New Zealand
3 North Island
4 South Island
5 New South Wales
6 Victoria
7 Bay of Plenty
8 Waikato
9 Otago
10 Nelson
21 St Clair
12 Queenstown
13 Cromwell
14 Dunedin
16 Westland
17 Southland
18 Canterbury
I am trying to make a query that will produce a drop down box to select the catagory you want. Showing the parent catagories and the children under them indented.

example:
Australia
-New South Wales
-Sydney
-Victoria
-South Australia
New Zealand
-North Island
-Bay of PLenty
-Waikato
-South Island
-Nelson
-Otago
-Dunedin
-Cromwell
and so on, so far this is the only code i have been able to get my head around but cant find the way to get the whole thing together


<select >
<?php

$query = "SELECT cat_id FROM scm_store_item_to_cat WHERE item_id = $item_id;";
$result = mysql_query($query) or die ("Query failed: ".mysql_error());
$row = mysql_fetch_array($result);
$current_cat_id = $row['cat_id'];

$query = "SELECT * FROM scm_store_cat ORDER BY cat_parent_id;";
$result = mysql_query($query) or die ("Query failed: ".mysql_error());
$num_results_data = mysql_num_rows($result);
for ($i=0; $i < $num_results_data; $i++)
{
$row = mysql_fetch_array($result);
$cat_parent_id = $row['cat_parent_id'];
$cat_id = $row['cat_id'];
$query2 = "SELECT cat_title FROM scm_store_cat_desc WHERE cat_id=$cat_id;";
$result2 = mysql_query($query2) or die ("Query failed: ".mysql_error());
$row2 = mysql_fetch_array($result2);
$cat_title = $row2['cat_title'];

echo 'Parent ID = '.$cat_parent_id.' - Cat ID = '.$cat_id.' - '.$cat_title.' <br />';
}
?>
</select>


If you could help me get further down the track on this it would be awesome
thanks
Gecko

Habtom

10:49 am on Jul 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can easily create two loops if that is not too much, one pulls the country and the other the cities.

The country id of the first query can be passed to the query of the second.