Jump to content


Photo

Foreach and list/menu


  • Please log in to reply
6 replies to this topic

#1 maziagha

maziagha
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 07 August 2006 - 11:58 AM

Hello all!

I'm using php mysql databse to creat a list/menu drop down.
In my category database i also have child categories.
with my script i can pull the categories and child categories.
the problem i have is that the child categories are placed in the list as they wish and not directly under the main category.
Can anyone help me? here is the part of that script.
/*
	Generate combo box options containing the categories we have.
	if $catId is set then that category is selected
*/
function buildCategoryOptions($catId = 0)
{
	$sql = "SELECT cat_id, cat_parent_id, cat_name
			FROM tbl_category
			ORDER BY cat_id";
	$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());
	
	$categories = array();

	while($row = dbFetchArray($result)) {
		list($id, $parentId, $name) = $row;
		
		
		if ($parentId = 0) {
			// we create a new array for each top level categories
			$categories[$id] = array('name' => $name, 'children' => array());
		} else {
			// the child categories are put int the parent category's array
			$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);
			
		}
	}	
	
	// build combo box options
	
	$list = '';
	foreach ($categories as $key => $value) {
		
		//$name     = $value['name'];
		$children = $value['children'];
		
		$list .="<option value=\"$parentId\"> $name"; 
		
		foreach ($children as $child) {
			$list .= "<option value=\"{$child['id']}\"";
			if ($child['id'] == $catId) {
				$list.= " selected";
			}
			
			$list .= ">{$child['name']}</option>\r\n";
		}
		
		$list .= "</optgroup>";
	}
	
	return $list;
}


#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 07 August 2006 - 12:51 PM

try change line
if ($parentId = 0) {
to
if ($parentId == 0) {


#3 maziagha

maziagha
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 07 August 2006 - 01:20 PM

i tryed that but it messes the thing up. Note that have removed this part
//$name     = $value['name'];

if i out that back it really mess thing up. I can get it work then but i use the same  list menu and if i want to add products to my database they all get recorded in wrong category.

#4 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 07 August 2006 - 01:26 PM

What about:

if ($parentId == "") {

#5 maziagha

maziagha
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 07 August 2006 - 01:29 PM

OK it work almost fine now except the main category gets wrong ID when choosen. no mather what main category i choose it is the same cat-id all the time. Work fine with child categories.
someone can tell why?
looks like this now:

/*
	Generate combo box options containing the categories we have.
	if $catId is set then that category is selected
*/
function buildCategoryOptions($catId = 0)
{
	$sql = "SELECT cat_id, cat_parent_id, cat_name
			FROM tbl_category
			ORDER BY cat_id";
	$result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error());
	
	$categories = array();
	while($row = dbFetchArray($result)) {
		list($id, $parentId, $name) = $row;
		
		if ($parentId == 0) {
			// we create a new array for each top level categories
			$categories[$id] = array('name' => $name, 'children' => array());
		} else {
			// the child categories are put int the parent category's array
			$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);	
		}
	}	
	
	// build combo box options
	$list = '';
	foreach ($categories as $key => $value) {
		$name     = $value['name'];
		$children = $value['children'];
		
		$list .= "<option value=\"$parentId\"> $name"; 
		
		foreach ($children as $child) {
			$list .= "<option value=\"{$child['id']}\"";
			if ($child['id'] == $catId) {
				$list.= " selected";
			}
			
			$list .= ">{$child['name']}</option>\r\n";
		}
		
		$list .= "</optgroup>";
	}
	
	return $list;
}


#6 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 07 August 2006 - 01:41 PM

try
$list .= "<option value=\"$parentId\"> $name";
$list .= "<option value=\"$key\"> $name";


#7 maziagha

maziagha
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 07 August 2006 - 01:44 PM

I bow before you  ;D
thx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users