Jump to content


Photo

Populating select box with groups


  • Please log in to reply
3 replies to this topic

#1 curthard89

curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 14 April 2007 - 09:14 AM

Hi guys,

i currently have this

<select name="category[]">
<?php
$tbl_name3 = "categories";

$go="SELECT * FROM $tbl_name3 ORDER BY category ASC";
$result2=mysql_query($go);

while($rows3=mysql_fetch_array($result2)){

echo "
	<option value=\"".$rows3['category']."\">".$rows3['category']."</option>
";
}
?>
</select>

this is fine, but i want groups in th select box to like p2l have,

so how can i print out the group, and for ever group, get the catergory inside the db and print it out under the group, then move on to the next group and do the same?

cats and groups all stored in db

Edited by curthard89, 14 April 2007 - 10:45 AM.


#2 Chaos King

Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 14 April 2007 - 12:43 PM

One possible way of doing it is running two queries, one to call all the groups, and another to call all the categories. Then you can store all those values into an array, and then call them later on.

Hope this helps:

<?php

// Call all groups in a query
$fetch = mysql_query ( "SELECT * FROM groups ORDER BY groupName ASC" );

$groups = array ();

while ( $row = mysql_fetch_assoc ( $fetch ) )
{
	// store all the information about a group into an array, having a unique group ID to identify each group
	$groups [ $row['groupID'] ]['info'] = $row;
}

// Call all categories in a query
$fetch = mysql_query ( "SELECT * FROM categories ORDER BY categoryName ASC" );

while ( $row = mysql_fetch_assoc ( $fetch ) )
{
	// Sort all categories into a group according to their parent group id
	$groups [ $row['categoryGroupID'] ]['cats'][] = $row;
}

// now loop through all the groups

if ( count ( $groups > 0 )
{
	foreach ( $groups as $group )
	{
		$groupInformation = $group['info'];
		
		if ( count ( $group['cats'] ) > 0 )
		{
			// <optgroup>
			
			foreach ( $group['cats'] as $cat )
			{
				$categoryInformation = $cat;
				
				// <option>Category Name</option>
			}
			
			// </optgroup>		
		}

	}
}

?>


#3 curthard89

curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 14 April 2007 - 02:48 PM

i tried and filled in the blanks etc, no luck tho, all i get is 1 group and everything called array.....arrays are over my head, is there a simpler way? as i also have couple another script in the tutorial edit page that selects the right option from dropdown box depending on what cat its submitted into

#4 curthard89

curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 14 April 2007 - 03:05 PM

lol, i tried my theory of this, and it works fine

<select name="category[]">
<?php
$tbl_name3 = "groups";

$go="SELECT * FROM $tbl_name3 ORDER BY `group` ASC";
$result2=mysql_query($go);

while($rows3=mysql_fetch_array($result2)){

echo "
	<optgroup label=\"".$rows3['group']."\">
";

$tbl_name4 = "categories";

$go1="SELECT * FROM $tbl_name4 WHERE `group` = '$rows3[group]' ORDER BY category ASC";
$result3=mysql_query($go1);

while($rows4=mysql_fetch_array($result3)){

echo "<option value=\"".$rows4['category']."\">".$rows4['category']."</option>";

}


echo"
	</optgroup>
";
}
?>
</select>

cheers anyway




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users