php 在视图 codeigniter 中调用模型函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21662859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:20:25  来源:igfitidea点击:

Calling model function in view codeigniter

phpmysqldatabasecodeignitermodel-view-controller

提问by adnan kamili

I am new to MVC, I am porting a project written in non-MVC style to MVC, but I am stuck on a problem where it is necessary to call Model function in View.

我是 MVC 的新手,我正在将一个以非 MVC 风格编写的项目移植到 MVC,但我遇到了一个需要在 View 中调用 Model 函数的问题。

Scenario:

设想:

Table1 - Products:
contains product_id, product_nameetc. and for each product there can be multiple versions.

表1 -产品:
包含product_idproduct_name等,并为每个产品可以有多个版本。

Table2 - Versions:
contains version_id, version_name, ... , product_idetc.

表 2 -版本:
包含version_idversion_name、 ...product_id等。

Now in the View I am displaying products and under each product heading I have to display version list of that product, in non-MVC style it was pretty simple, I can use the following code snippet in View:

现在在视图中我正在显示产品,在每个产品标题下我必须显示该产品的版本列表,在非 MVC 样式中非常简单,我可以在视图中使用以下代码片段:

foreach ($product as $row) 
{
    echo $row['product_name'];
    if ($main->getVersionList($vresult,$row["product_id"]))
    {
        foreach ($vresult as $vrow)
        {
          echo $vrow['version_name'];
        }
    }
}

Now, I can pass Product array from controller to view but what about each Versionarray which needs to be generated corresponding to each product?

现在,我可以将 Product 数组从控制器传递给视图,但是需要生成对应于每个产品的每个Version数组呢?

Update:

更新:

This is my final working solution (used a map), in controller:

这是我在控制器中的最终工作解决方案(使用地图):

        $this->load->model ( 'product_mod' );
        $data ['products'] = $this->product_mod->getProductList ();
        $data ['versions'] = array ();
        foreach ( $data ['products'] as $product )
        {
            $data ['versions'] [$product['product_id']] =   $this->product_mod->getVersionList ( $product['product_id'] );
        }

回答by Hashem Qolami

MVC or not to MVC

MVC 或不 MVC

The first thing I should note is that It is impossible to write classical MVC in PHP. In fact the MVC-like PHP frameworks such as CodeIgniter or Yii implements sort of MVPin which:

我应该注意的第一件事是在 PHP 中编写经典 MVC 是不可能的。事实上,类似 MVC 的 PHP 框架,例如 CodeIgniter 或 Yii 实现了某种MVP,其中:

  • view is passive and unaware of model
  • presenter (controller) changes state of model, reads information and passes it to view
  • 视图是被动的,不知道模型
  • 演示者(控制器)改变模型的状态,读取信息并将其传递给视图

Credits to tere?ko

学分泰雷?KO

CodeIgniter Approach

CodeIgniter 方法

However, particularly in CodeIgniter, you have 3 steps:

但是,特别是在 CodeIgniter 中,您有 3 个步骤:

  • Create a Modelto query through the database and return the data (as an array or object)
  • Create a Controllerto loadand fetch the resultfrom the Model(a method of the Model), and pass the returned data to the view
  • Create a Viewand use PHP loops to echo the result out, build the HTML.
  • 创建模型以通过数据库查询并返回数据(作为数组或对象)
  • 创建一个Controller,用于从Model中加载获取结果Model的一个方法),并将返回的数据传递给视图
  • 创建一个视图并使用 PHP 循环来回显结果,构建 HTML。

Getting all together

聚在一起

Considering the above approach, you need to fetch the result from the database in your Model:

考虑到上述方法,您需要从模型中的数据库中获取结果:

application/models/product.php

应用程序/模型/product.php

class Product extends CI_Model
{
    public function get_product($product_id)
    {
        $this->db->select('*')->from('products');
        $this->db->where('product_id', $product_id);
        $this->db->join('versions', 'versions.product_id = products.product_id');
        $query=$this->db->get();
        return $query->first_row('array');
    }
}

Then fetch and pass the result within the Controller:

然后在控制器中获取并传递结果:

application/controllers/products.php

应用程序/控制器/products.php

class Products extends CI_Controller
{
    public function view($product_id)
    {
        $this->load->model('product');
        // Fetch the result from the database
        $data['product'] = $this->product->get_product($product_id);
        // Pass the result to the view
        $this->load->view('product_view', $data);
    }
}

Finally, use the returned data in the view, to generate the list:

最后,在视图中使用返回的数据,生成列表:

application/views/product_view.php

应用程序/视图/product_view.php

// Use $product to display the product.
print_r($product);

回答by Oguz Kurtcuoglu

You should do mysql query in your model, for example products_model (don't forget to load it)

您应该在模型中执行 mysql 查询,例如 products_model (不要忘记加载它)

Example Query : This is just select * from products

示例查询:这只是从产品中选择 *

public function All_Products()
{
$this->db->select('');
$result = $this->db->get('products')->result_array();
return $result;
}

So as I see , you are foreach guy like me, rather than using mass queries.

所以正如我所看到的,你是像我一样的 foreach 人,而不是使用批量查询。

In your controller you could load $products my your model.

在您的控制器中,您可以加载 $products 我的模型。

$products = $this->products_model->All_Products();

My solution is creating a new array then putting new values in it.Also you need to getVersionList function in your model.

我的解决方案是创建一个新数组,然后在其中放入新值。此外,您还需要在模型中使用 getVersionList 函数。

$newArray = array ();
foreach ( $products as $values ) {
$version = $this->products_model->getVersionList($values['product_id']);
$tmp = array (
'product_id' => $values ['product_id'], 
'product_name' => $values ['product_name'], 
'version' => $version
);
array_push ( $newArray, $tmp );
}

So add your new array to $data you could have them in your view file.

因此,将您的新数组添加到 $data 中,您可以将它们放在您的视图文件中。

$data ['products'] = $newArray;

Sorry I didnt figure out all your queries but I think its better to teach how to catch fish than giving a fish.

对不起,我没有弄清楚你所有的疑问,但我认为教如何钓鱼比给鱼更好。

回答by Devendra Rajput

Using the below code you can call the model's method in the view file. It's worked for me and is so simple also.

使用以下代码,您可以在视图文件中调用模型的方法。它对我有用,而且也很简单。

$CI =& get_instance();
$CI->model_name->method_name();