【问题标题】:Convert function to codeigniter将函数转换为codeigniter
【发布时间】:2012-10-17 19:51:19
【问题描述】:

所以总的来说,我是 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;";
    }
}

【问题讨论】:

  • 基本 MVC 结构从视图中获取尽可能多的逻辑和数据。所以你应该取出DB抓取并放入模型中,然后将html从函数中取出并放入视图中,然后让控制器处理函数调用......

标签: php codeigniter codeigniter-2


【解决方案1】:

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

【讨论】:

  • 我没有正确解释的另一部分是我需要这个。我还有一个“项目”表,每个项目都有一个“cat_id”字段。当我查询项目表时,我想在视图中显示类别路径。那么最好是在模型中抓取项目,然后遍历项目结果并为类别数据的每个结果添加一个数组还是应该在控制器中这样做?
  • 你想提取多少个查询,你想使用写函数,就像我在上面为每个查询显示的那样。并从控制器中的函数中获取一个数组并将其传递给控制器​​中的模型数组,然后使用这些数组在视图文件中使用 php、html 和 css....... ...
【解决方案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);

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

【讨论】:

  • 谢谢!我一直在努力避免将 html 嵌入到视图之外的任何内容中。
  • 严格来说,您不必在控制器中避免使用 HTML(尽管这通常是个好主意)。当我有非特定于控制器的代码时,如果它需要数据库访问或使用 CI 库,我通常将其拆分为一个库,如果它是简单的字符串操作,我通常将其拆分为一个帮助程序。
  • 还错过了$this-&gt;db 的电话,并将答案更新为$_CI-&gt;db
  • 还没有,我正试图弄清楚如何从另一个查询中调用它。我有一个要查询的项目表,每个项目都有一个类别 ID。除了 id 本身之外,我还想从库中将该数组发送到视图。
  • 那可能完全是另一个问题了。
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 2015-09-07
  • 1970-01-01
  • 2013-11-21
  • 2020-10-31
  • 2016-01-24
  • 2016-03-02
  • 1970-01-01
相关资源
最近更新 更多