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;";
    }
}
4

2 回答 2

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);

我没有测试它,所以那里可能会有一些错误,但我认为这应该让球滚动起来?

于 2012-10-17T20:22:03.437 回答
2

以codeingiter框架的方式来做............

  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);
    

    }

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

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

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();

}

查看文件.......................在查看文件中的代码中将 $row 替换为 $content 我假设您的代码没有错误

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;";
}
于 2012-10-17T20:03:40.980 回答