Create a table with catgory_id,name,parent,sort_order field
<?php
$g_link = mysql_connect( 'localhost', 'root', '');
mysql_select_db('your_database_name');
$result = mysql_query("SELECT category_id,name,parent,sort_order FROM category ORDER BY parent,sort_order,name");
$category = array('categories' => array(),'parent_cats' => array());
while ($row = mysql_fetch_assoc($result)) {
$category['categories'][$row['category_id']] = $row;
$category['parent_cats'][$row['parent']][] = $row['category_id'];
}
?>
/*This function used for show add category (in select box for select parent category) in menu*/
<select name="cat">
<option value="0"></option>
<?php
function buildCategory($parent, $category,$name) {
$html = "";
if (isset($category['parent_cats'][$parent])) {
foreach ($category['parent_cats'][$parent] as $cat_id) {
if (!isset($category['parent_cats'][$cat_id])) {
$html .= "<option value='".$category['categories'][$cat_id]['category_id']."'>" . trim($name."->". $category['categories'][$cat_id]['name'],"->") . "</option>";
}
if (isset($category['parent_cats'][$cat_id])) {
$html .= "<option value='".$category['categories'][$cat_id]['category_id']."'>" . trim($name."->".$category['categories'][$cat_id]['name'],"->") . "</option>";
$html .= buildCategory($cat_id, $category,trim($name."->".$category['categories'][$cat_id]['name'],"->"));
$html .= "</option>";
}
}
}
return $html;
}
echo buildCategory(0, $category ,'');
?>
</select>
<?php
echo '<br><br>';
/*--------------------------------end-------------------------------------------------------------------------------------------------------------------------*/
/*This function used for show all categories in menu*/
function buildCategory1($parent, $category) {
$html = "";
if (isset($category['parent_cats'][$parent])) {
$html .= "<ul>\n";
foreach ($category['parent_cats'][$parent] as $cat_id) {
if (!isset($category['parent_cats'][$cat_id])) {
$html .= "<li>\n <a href='#'>" .$category['categories'][$cat_id]['name'] . "</a>\n</li> \n";
}
if (isset($category['parent_cats'][$cat_id])) {
$html .= "<li>\n <a href='#'>" .$category['categories'][$cat_id]['name'] . "</a> \n";
$html .= buildCategory1($cat_id, $category);
$html .= "</li> \n";
}
}
$html .= "</ul> \n";
}
return $html;
}
echo buildCategory1(0, $category);
/*--------------------------------end-------------------------------------------------------------------------------------------------------------------------*/
?>
<?php
$g_link = mysql_connect( 'localhost', 'root', '');
mysql_select_db('your_database_name');
$result = mysql_query("SELECT category_id,name,parent,sort_order FROM category ORDER BY parent,sort_order,name");
$category = array('categories' => array(),'parent_cats' => array());
while ($row = mysql_fetch_assoc($result)) {
$category['categories'][$row['category_id']] = $row;
$category['parent_cats'][$row['parent']][] = $row['category_id'];
}
?>
/*This function used for show add category (in select box for select parent category) in menu*/
<select name="cat">
<option value="0"></option>
<?php
function buildCategory($parent, $category,$name) {
$html = "";
if (isset($category['parent_cats'][$parent])) {
foreach ($category['parent_cats'][$parent] as $cat_id) {
if (!isset($category['parent_cats'][$cat_id])) {
$html .= "<option value='".$category['categories'][$cat_id]['category_id']."'>" . trim($name."->". $category['categories'][$cat_id]['name'],"->") . "</option>";
}
if (isset($category['parent_cats'][$cat_id])) {
$html .= "<option value='".$category['categories'][$cat_id]['category_id']."'>" . trim($name."->".$category['categories'][$cat_id]['name'],"->") . "</option>";
$html .= buildCategory($cat_id, $category,trim($name."->".$category['categories'][$cat_id]['name'],"->"));
$html .= "</option>";
}
}
}
return $html;
}
echo buildCategory(0, $category ,'');
?>
</select>
<?php
echo '<br><br>';
/*--------------------------------end-------------------------------------------------------------------------------------------------------------------------*/
/*This function used for show all categories in menu*/
function buildCategory1($parent, $category) {
$html = "";
if (isset($category['parent_cats'][$parent])) {
$html .= "<ul>\n";
foreach ($category['parent_cats'][$parent] as $cat_id) {
if (!isset($category['parent_cats'][$cat_id])) {
$html .= "<li>\n <a href='#'>" .$category['categories'][$cat_id]['name'] . "</a>\n</li> \n";
}
if (isset($category['parent_cats'][$cat_id])) {
$html .= "<li>\n <a href='#'>" .$category['categories'][$cat_id]['name'] . "</a> \n";
$html .= buildCategory1($cat_id, $category);
$html .= "</li> \n";
}
}
$html .= "</ul> \n";
}
return $html;
}
echo buildCategory1(0, $category);
/*--------------------------------end-------------------------------------------------------------------------------------------------------------------------*/
?>
No comments:
Post a Comment