Ok I did this in a similar fashion ... however the problem is a bit different.
Let me show you the code to take a category's ID and then call all the subcategories that go with it. They are setup in two different tables with the same database. See if this code can help you. Thanks!
<?php
// This is an include of the connection strings.
include("connection.inc");
// First I get everything from categories.
$sql = "select * from categories";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
// I then get the category_id.
$category_id = $row['category_id'];
$category_name = $row['category_name'];
// This just outputs the category name
echo "\r\r<p><strong>$category_name<br>";
// Now we call the subcategories table and search ref_cat for the category_id.
// My table structure:
//
// -----------------------
// sub_id INT NOT NULL AUTO_INCREMENT (PRIMARY KEY)
// sub_title TEXT (The title of the sub-category)
// ref_cat TEXT (referring category number)
// ------------------------
//
$sqla = "select * from subcategories where ref_cat='$category_id'";
$resulta = mysql_query($sqla);
while ($row = mysql_fetch_array($resulta)) {
$sub_id = $row['sub_catid'];
$sub_title = $row['sub_title'];
echo "\r<input name='checkbox[]' type='checkbox' class='text_boxes_contractor' value='$sub_id'></strong>$sub_title<br>";
}
}
?>
the \r's will just make the source code cleaner to read. Just FYI (incase you didn't know).