0

私のテーブル[cat_id,title,pid]。すべての子サブカテゴリ ID を次の形式で取得する必要があります。

[1] =>Array ( [cat_id] => 2 [title] => TEST [pid] => 1 ),

[2] =>Array ( [cat_id] => 3 [title] => TEST [pid] => 1 ),

[3] =>Array ( [cat_id] => 4 [title] => TEST [pid] => 2 ), 

[4] =>Array ( [cat_id] => 5 [title] => TEST [pid] => 3 ) 

目的 - 子の ID を使用して、これらのアイテムをすべて取得します。

次のコードのように何かをしようとしましたが、うまくいきません:

public function get_ids($tree,$cid = 0)
{

    $data = $this->db->select('cat_id, title, pid')
                     ->where('pid',$cid)
                     ->get('catalog');

    $result = array_push($tree,$data->result_array());

    if($data->num_rows() > 0){

        foreach ($data->result_array() as $r) {

            return $this->get_ids($result,$r['cat_id']);

        }   
    }
    else{
        return $result;
    }
}

多分もっと良い方法がありますか?

4

1 に答える 1

0

以下のコードを試して、指定された親IDですべての子IDを取得してください。

function fnGetChildCatelogs($id) {

    $qry = "SELECT cat_id FROM catalog WHERE pid = $id";

    $rs = mysql_query($qry);    

    $arrResult = array();    

    if(mysql_num_rows($rs) > 0) {

        # It has children, let's get them.

        while($row = mysql_fetch_array($rs)) {

            # Add the child to the list of children, and get its subchildren

            $arrResult[$row['cat_id']] = fnGetChildCatelogs($row['cat_id']);

        }

    }

    return $arrResult;

}
于 2012-07-30T11:42:31.380 に答える