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

MVC或不是MVC

>视图是被动的,不知道模型

> presenter(控制器)更改模型的状态,读取信息并将其传递给视图

CodeIgniter方法

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

>创建一个Model来查询数据库并返回数据(作为数组或对象)

>创建一个Controller来加载和获取Model(Model的一个方法)的结果,并将返回的数据传递给视图

>创建一个View并使用PHP循环来回显结果,构建HTML.

全力以赴

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

应用程序/模型/ 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');

}

}

然后在Controller中获取并传递结果:

应用/控制器/ 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);

}

}

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

应用/视图/ product_view.php

// Use $product to display the product.

print_r($product);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值