kod umím sem vložit, ale bez řádků ...
<?php
class Categories{
function getCategoriesArray($parent_id = 0, $padding=0, $prefix="»"){
$return = array();
$return = $this->getSubCategoriesArray($parent_id, $padding, $prefix);
return $return;
}
function getSubCategoriesArray($parent_id, $padding, $prefix){
static $i=0;
$resource = $this->getAllSubCategories($parent_id);
static $return = array();
if (@is_resource($resource)){
while ($records_array = mysql_fetch_assoc($resource)) {
$return['id'][$i] = $records_array['id'];
if(is_int($padding)){
$return['name'][$i] = "<div style='padding-left:".$padding."px'>".$prefix ." ".$records_array['name']."</div>";
}else{
$return['name'][$i] = $padding. $prefix .$records_array['name'];
}
$i++;
if(is_int($padding)){
$this->getSubCategoriesArray($records_array['id'], ($padding+15), $prefix);
}else{
static $pad ="";
if($pad == ""){
$pad = $padding;
}
$this->getSubCategoriesArray($records_array['id'], $padding.$pad, $prefix);
}
}
}
return $return;
}
function getAllSubCategories($parent_id=0) {
$result = mysql_query("SELECT * FROM categories WHERE parentid='" . $parent_id . "'");
return $result;
}
//when disabling the parent category its all subcategory should be disabled.
function changeAllSubCategoriesStatus($id, $status)
{
static $subcat = array();
$subcat[] = $id;
$result = mysql_query("UPDATE categories set status=$status WHERE id = $id");
$select_result = mysql_query("SELECT id FROM categories WHERE parentid=$id");
$hasSubcat = mysql_result($select_result, 0, 0);
if($hasSubcat){
$this->changeAllSubCategoriesStatus($hasSubcat, $status);
}
return $subcat;
}
//when enabling the subcategory its all parent category should be enabled.
function changeAllParentCategoriesStatus($id, $status)
{
static $parentcat = array();
$parentcat[] = $id;
$result = mysql_query("UPDATE categories set status=$status WHERE id = $id");
$select_result = mysql_query("SELECT parentid FROM categories WHERE id=$id");
$hasParentcat = mysql_result($select_result, 0, 0);
if($hasParentcat){
$this->changeAllParentCategoriesStatus($hasParentcat, $status);
}
return $parentcat;
}
//if you know the level of category then you use this other wise dont use this
function getCategoriesInTreeStructure($level = 10){
$query = " SELECT node0.name AS node0_name, node0.status as node0_status,node0.parentid as node0_parent, node0.id as node0_id";
//select nodes
for($l=1;$l<=$level;$l++) {
$query .= ",node".$l.".name AS node".$l."_name, node".$l.".status as node".$l."_status,node".$l.".parentid as node".$l."_parent, node".$l.".id as node".$l."_id ";
}
//join
$query .= " FROM categories AS node0 ";
for($l=1;$l<=$level;$l++) {
$query .= " LEFT OUTER JOIN categories AS node".$l." ON node".$l.".parentid = node".($l-1).".id";
}
//conditions
$query .= " WHERE ((node0.parentid IS NULL) OR (node0.parentid =0)) ORDER BY node0_name ";
for($l=1;$l<=$level;$l++) {
$query .= ",node".$l."_name ";
}
$resource = mysql_query($query);
return $resource;
}
}
$connect = mysql_connect("localhost", "user", "password");
$dh = mysql_select_db("database", $connect);
$cat_obj = new Categories();
$cat_array =$cat_obj->getCategoriesArray();
//For dropdowns
//$cat_array = $cat_obj->getCategoriesArray(0, "--", "");
foreach($cat_array['name'] as $key =>$catname) {
//id---->echo $cat_array['id'][$key];
echo $catname;
}
?>
|