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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?