doufuhuang6981 2014-07-18 13:52
浏览 50
已采纳

检查类别是否具有父级

i'm in mid of creating my own cms . And now i want to show which one of the category has parent but i don't know how, so please help me.

my category table

 idkategori  |  namakategori  | parentid 
      1         Programming         0
      2            PHP              1

Or i need relationship table for my categories?

My Controller so far.

function tampilmenu()
{
            $sql              = "select * from fc_kategori";
            $data['kategori'] = $this->bymodel->tampildata($sql);
            $sql1             = "select parentid from fc_kategori";
            $data['parent']   = $this->bymodel->tampildata($sql1);
                            $id=array();
                foreach ($data['parent'] as $paren)
            {
                $id[]=$paren->parentid;
            }
            foreach ($data['kategori'] as $cat) 
            if(in_array($cat->parentid,$id))
            {
                        $have ='Yes';
            }
            else
            {
                        $have ='No';
            }
            echo $cat->idkategori.$have;
                                    }   

}

my model

function tampildata ($sql)
{
    $query = $this->db->query($sql);
    return $query->result();
}

Please don't laugh on me.

  • 写回答

1条回答 默认 最新

  • douxi9245 2014-07-18 14:16
    关注

    Kindly follow:

    1) Since you are using a MVC framework, never write queries inside the controller (queries should always be written in models).
    2) Never use raw queries, since CI provides you what is called as Active Record.
    3) Also never pass direct queries anywhere you'll possibly code in whichever language. Always pass data and make do that function to compute and query process.
    4) Remember, in CI Models are only used for database functionalities, Views are only used for your HTML markups and Controllers acts as the mediator between models and views.


    Your code:

    Controller -

    public function tampilmenu()
    {
        $categories = $this->bymodel->get_category_having_parent();
    
        echo "<pre>"; print_r($categories);
    
        // this will return object having all categories that are parents
    }
    

    Model -

    public function get_category_having_parent()
    {
        $parent_ids = array();
        $ps = $this->get("parentid");
        foreach($ps as $p)
        {
           $parent_ids[] = $p->parentid;
        } 
    
        $this->db->where_in("id", $parent_ids);
        $query = $this->db->get("fc_kategori");
        return $query->result();
    }
    
    public function get($column="*")
    {
        $this->db->select($column);
        $query = $this->db->get("fc_kategori");
        return $query->result();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么