Jump to content

creating categories inside a combo box


karthikanov24

Recommended Posts

Hi,

In the following codes,i could not understand these lines..

 

$categories[$id] = array('name' => $name, 'children' => array());

$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);

$name    = $value['name'];

$children = $value['children'];

 

Could u explain me clearly with an example.....?

 

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=\"$key\"";
	if ($key == $catId) {
		$list.= " selected";
	}

Link to comment
https://forums.phpfreaks.com/topic/171739-creating-categories-inside-a-combo-box/
Share on other sites

if ($parentId == 0) {
    // we create a new array for each top level categories
    $categories[$id] = array('name' => $name, 'children' => array());
}

 

This code creates an array for every top-level category (every record in your database that a 0 as a parent_id). Which will give you something like:

 

Cat 1
Cat 2
Cat 3

 

else {
    // the child categories are put int the parent category's array
    $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);   
}

 

This code retrieves the earlier set $id (== $parentId) and fills the 'children' => array() part with the $id and name of the child categories. Which will give you something like:

 

Cat 1
Cat 1.1
Cat 1.2
Cat 1.3
Cat 2
..

 

But it is also possible that a child becomes a parent like:

 

Cat 1 (parent of 1.1 and ancestor of 1.1.1)
Cat 1.1 (child of 1)
Cat 1.1.1 (child of 1.1 and descendant of 1)

hi

 

What are the keys and values of $categories and what is stored in $name variable and $children variable ....in the following lines of the above posted codes...

 

foreach ($categories as $key => $value) {

$name    = $value['name'];

$children = $value['children'];

foreach ($categories as $key => $value) {

$name    = $value['name'];

$children = $value['children'];

 

$name is the name of the category and $children are the children of the parent category presumably they afterwards create another foreach loop going over the children categories.

foreach ($categories as $key => $value) {

$name    = $value['name'];

$children = $value['children'];

 

$name is the name of the category and $children are the children of the parent category presumably they afterwards create another foreach loop going over the children categories.

- write a query to fetch all categories and their id

- in html use <optgroup>code to display categories here</optgroup>...after you display categories, inside optgroup write a query to fetch products related to that category..and use <option>//code to display products</option>

hi

if i am typing the following codes to see the array output..

print_r $categories;

print_r $categories[$id];

 

at the above posted codings as like this...i am not getting the out put..so how can i see those array output..?

 

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);	
	}
}	
print_r $categories;
print_r $categories[$id];

 

 

hi

if i am trying to print this

print_r ($categories[$parentId]['children']);

in the  above posted code..i get the output as:

 

Array ( [0] => Array ( [id] => 16 [name] => Naruto ) [1] => Array ( [id] => 17 [name] => Hunter X Hunter ) ) 

 

 

Here 'Naruto' and 'Hunter x Hunter'  are the two sub categories(children) of the 'Manga' category

 

But I have other 2 sub categories(children) 'volvo','mercedes benz' for cars(first level category) which is not displayed here..when running the file.

 

can u say me the reason and how to display it....

 

It contains 'children' if you would print $categories you would get:

 

Array (
    $parentId => Array (
        'children' => Array (
            0 => Array ( 'id' => $id, 'name' => $name),
            1 => Array ( 'id' => $id, 'name' => $name),
            ..
        )
    )
)

 

I used $parentId, $id and $name these would translate to what they hold at that time ofcourse.

hi

In the following codes,

 

1.I tried to echo $children;below the following line of code

$children=$value['children'];

echo $children;

  it gives output as"array array".whats the reason?

  How to see the output of $children ?

 

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=\"$key\"";
	if ($key == $catId) {
		$list.= " selected";
	}

	$list .= ">$name</option>\r\n";

	foreach ($children as $child) {
		$list .= "<option value=\"{$child['id']}\"";
		if ($child['id'] == $catId) {
			$list.= " selected";
		}

		$list .= ">  {$child['name']}</option>\r\n";
	}
}

return $list;
}

 

2. <optgroup>and </optgroup> displays -cat1 and cat 2

How the children categories cat.1 ,cat1.2,cat  .1,cat2.2)  in <option></option> is matched to categories cat 1  and cat2 in the foreach loop.?

hi

in the above posting,when i printed $categories using,

print_r ($categories);

I get output as,

 

Array ( [12] => Array ( [name] => Cars [children] => Array ( [0] => Array ( [id] => 14 [name] => Volvo ) [1] => Array ( [id] => 15 [name] => Mercedes-Benz ) ) ) [13] => Array ( [name] => Manga [children] => Array ( [0] => Array ( [id] => 16 [name] => Naruto ) [1] => Array ( [id] => 17 [name] => Hunter X Hunter ) ) ) )

 

But when i printed the key of [name] which is 12 using

print_r ($key[name]);

and value of 12 which is [name],[children], etc..using

print_r ($value[12]);

 

i am not getting the output..

So how can i get above said outputs..what is the codings..?

Thanks in advance..

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.