将函数转换为codeigniter

时间:2012-10-17 19:51:19

标签: php codeigniter codeigniter-2

所以我对codeigniter和MVC很新。我有一个应用程序,我正在转换,我想知道最好的方法是处理这种类型的功能,特别是在视图中。

我有一个类别表,如下表所示:

cat_id | parent_id | catname
------------------------------
1       0           this category
2       1           that category

给定cat_id,该函数会吐出一个带链接的格式化字符串。我知道我不应该在视图之前处理URL项,所以我不确定我是否在CI中重写了如何在视图中处理结果数组。

思考?原始功能如下:

    function createPath($id, $category_tbl, $except = null) {
    $s = "SELECT * FROM ".$category_tbl." WHERE cat_id = $id";
    $r = mysql_query($s);
    $row = mysql_fetch_array($r);
    if($row['parent_id'] == 0) {
        $name = $row['catname'];
        if(!empty($except) && $except == $row['cat_id']) {
            return "<a href='index.php'>Admin</a> &raquo; ".$name."";
        }
        //return "<a href='index.php'>Admin</a> &raquo; <a href='index.php?folder_id=$id'>".$name."</a> &raquo; ";
        return "<a href='category.php?catid=$id&category=".$row['slugname']."'>".$name."</a> &raquo; ";
    } else {
        if(!empty($except) && $except == $row['cat_id']) {
            $name = $row['catname'];
            return createPath($row['parent_id'],$category_tbl, false). " $name";
        } 
        $name = $row['catname'];
        return createPath($row['parent_id'],$category_tbl, false). " <a href='category.php?catid=$id&category=".$row['slugname']."'>".$name."</a> &raquo;";
    }
}

2 个答案:

答案 0 :(得分:2)

以编码方式框架方式执行..........

  1. 在模型中编写查询函数
  2. 然后在控制器函数中调用模型并将结果集保存在数组
  3. 然后将保存的数组传递给视图
  4. 在视图中,您解析该数组并显示您想要的内容

    公共函数abc(参数列表)

    {

     $xyz=$this->model->function_name_in_model(arguemnts);
     $model=array();
     $model['content']=$xyz;
     $this->load->view('view_file_name',$model);
    

    }

  5. 在模型页面中编写模型函数,并在视图文件中使用传递的数组.......

    控制器................

    public function createpath($id)
    
    {
       $result = $this->model->getResult($id);
    
       $model = array();
       $model['content'] = $result;
       $this->load->view('view_file_name_path',$model);
    
    }
    

    模型...................

    public function getResult($id) 
    
    {
       $query_str="SELECT * FROM ".$category_tbl." WHERE cat_id = $id";
    
        //echo $query_str;exit;
        $res=$this->db->query($query_str);
        if($res->num_rows()>0){
            return $res->result("array");
        }
    
        return array();
    
    }
    

    查看文件...........................在视图文件的代码中用$ content替换$ row我假设你的代码没有'有错误

    if($row['parent_id'] == 0) {
        $name = $row['catname'];
        if(!empty($except) && $except == $row['cat_id']) {
            return "<a href='index.php'>Admin</a> &raquo; ".$name."";
        }
        //return "<a href='index.php'>Admin</a> &raquo; <a href='index.php?folder_id=$id'>".$name."</a> &raquo; ";
        return "<a href='category.php?catid=$id&category=".$row['slugname']."'>".$name."</a> &raquo; ";
    } else {
        if(!empty($except) && $except == $row['cat_id']) {
            $name = $row['catname'];
            return createPath($row['parent_id'],$category_tbl, false). " $name";
        } 
        $name = $row['catname'];
        return createPath($row['parent_id'],$category_tbl, false). " <a href='category.php?catid=$id&category=".$row['slugname']."'>".$name."</a> &raquo;";
    }
    

答案 1 :(得分:2)

我迅速采取了自由(我的意思是快速)将您的功能转换为库。

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Path {

    function __construct()
    {
        $_CI =& get_instance();
    }

    function create($id, $category_tbl, $except = NULL)
    {
        $_CI->db->from($category_tbl);
        $_CI->db->where('cat_id', $id);
        $query = $_CI->db->get();

        if ($query->num_rows() > 0)
        {
            $row = $query->row();

            if($row->parent_id == 0)
            {
                $name = $row->catname;

                if(!empty($except) && $except == $row->cat_id) {
                    return "<a href='index.php'>Admin</a> &raquo; ".$name."";
                }

                return "<a href='category.php?catid=$id&category=".$row->slugname."'>".$name."</a> &raquo; ";
            }
            else
            {
                if(!empty($except) && $except == $row->cat_id) {
                    $name = $row->catname;
                return $this->create($row->parent_id, $category_tbl, FALSE). " $name";
                }

                $name = $row->catname;
                return $this->create($row->parent_id, $category_tbl, FALSE). " <a href='category.php?catid=$id&category=".$row->slugname."'>".$name."</a> &raquo;";
            }

        }

        return NULL;
    }
}

/* End of file Path.php */
/* Location: ./application/libraries/Path.php */

请使用以下方式调用:

$this->load->library('path');
$this->path->create($id, $category_tbl, $except);

我没有对它进行测试,所以可能会有一些错误,但是我觉得这应该会让人失望?