简体   繁体   中英

Printing the Categories and Sub Categories Alone

function build_list($id=0,$collapsed="") //return an array with the categories ordered by position
{

$RootPos = "";
$this->c_list = array();

if($id != 0){
$this_category  = $this->fetch($id);
$positions      = explode(">",$this_category['position']);
$RootPos        = $positions[0];
}

// lets fetch the root categories
$sql = "SELECT *
        FROM ".$this->table_name."
        WHERE position  RLIKE '^([0-9]+>){1,1}$' AND c_group    =      '".$this->Group."'
        ORDER BY c_name";
$res = mysql_query($sql) or die(trigger_error("<br><storng><u>MySQL Error:</u></strong><br>".mysql_error()."<br><br><storng><u>Query Used:</u></strong><br>".$sql."<br><br><storng><u>Info:</u></strong><br>",E_USER_ERROR));

while($root = mysql_fetch_array($res)){
$root["prefix"] = $this->get_prefix($root['position']);
$this->c_list[$root['id']] = $root;

    if($RootPos == $root['id'] AND $id != 0 AND $collapsed != ""){
    $this->list_by_id($id);
    continue;

}else{

// lets check if there is sub-categories
    if($collapsed == "" AND $id==0){
    $has_children = $this->has_children($root['position']);
    if($has_children == TRUE) $this->get_children($root['position'],0);
}}}
return $this->c_list;
}

// He is the Author of the code... Categories Class Author: Shadi Ali

Now I want to just return the Categories and Sub Categories from the above code.

function browse() {

$categories = new categories;
$categories_list = $categories->build_list();

    foreach($categories_list as $c)
        {


            return $c->$id;

    }
}

The above code is not working.... can anyone help me out.

Here are two problems with the browse() function:

  • The return statement is inside the foreach loop. The statement will return one value for one of the items in the $categories-list (at most), and not continue to loop over the rest of the $categories-list.

  • The $id variable is never declared or initialised in return $c->$id , perhaps you meant to use $c['id'] or $c->id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM