You can do something like this (I usually solved similar problems that way) - SQL first
select t1.MainID, t1.Company, t2.SubID, t2.SubCompany from table1 t1 join table2 t2 on t1.MainID = t2.MainID order by t1.MainID
then in php something like
$last_company_id = -1;
while (some_data_read()) {
if ($last_company_id != $current_company_id) {
if ($last_company_id != -1)
echo "</item">; // to close that tag of previous main category
$last_company_id = $current_company_id;
echo "<item id='parent' value='$current_company'>"; // openning tag for new category
}
echo "<item id='child' value='$current_subcompany'/>";
}
// now just finish last open main company if any
if ($last_company_id != -1)
echo "</item">;
I think you got the idea - remember main company you are working with currently, when it changes then create the new tag and do whatever necessary