Thank you so much for your help! I'm having one problem with the code, however:
[EDIT]I've figured out what's wrong, but now I'm not sure if I need to change something else.
The query wasn't picking up the parent categories because of the condition that only categories with allowed >0 should be retrieved.
The array originally printed like this:
Array
(
[3] => ~>Bikes
[5] => ~>Other
[4] => ~>Parts
[6] => ~>Bikes
[8] => ~>Other
[7] => ~>Parts
)
So I changed :
$query = "SELECT * FROM `classifieds_categories` WHERE `ads_allowed` > 0 ORDER BY `parent`,`title` ASC;";
$result = mysql_query($query);
to:
$query = "SELECT * FROM `classifieds_categories` ORDER BY `parent`,`title` ASC;";
$result = mysql_query($query);
Which gave me what looks like a proper array:
Array
(
[3] => For Sale~>Bikes
[5] => For Sale~>Other
[4] => For Sale~>Parts
[6] => Wanted~>Bikes
[8] => Wanted~>Other
[7] => Wanted~>Parts
)
So it looks like it fixed it. I'm confused now about which part of the code is keeping the categories that you're not allowed to post in out of the array. I thought removing that was going to give me an array that looked like this:
Array
(
[1] => For Sale~>
[2] => Wanted~>
[3] => For Sale~>Bikes
[5] => For Sale~>Other
[4] => For Sale~>Parts
[6] => Wanted~>Bikes
[8] => Wanted~>Other
[7] => Wanted~>Parts
)
But your code seems to have taken care of this elsewhere. Could you let me know where this is covered in the code? I would very much like to understand how it handled that.
For reference, here's my implementation of your code:
$query = "SELECT * FROM `classifieds_categories` ORDER BY `parent`,`title` ASC;";
$result = mysql_query($query);
$parents = array();
$categories = array();
while ($row = mysql_fetch_assoc($result)) {
if ($row['parent'] == 0) {
$parents[$row['id']] = $row['title'];
} else {
$categories[$row['id']] = $parents[$row['parent']]."~>".$row['title'];
}// end if
}
printf('<pre>%s</pre>',print_r($categories,true));
echo("<br><br>
<select name='category' id='category'>");
foreach ($categories as $id => $title) {
echo '<option value="'.$id.'">'.$title.'</option>'."\n";
}
echo("</select>");
thanks,
json