[英]How to join these tables in codeigniter
我在加入这些表时遇到问题,并且知道如何使控制器代码使用文本框查看视图文件中的数据。
public function get_order_return_info()
{
$this->db->select('tbl_order_details.*', false);
$this->db->select('tbl_order_details.order_details_id', false);
$this->db->select('tbl_order.order_id', false);
$this->db->select('tbl_product.product_id', false);
$this->db->select('tbl_inventory.product_quantity', false);
$this->db->from('tbl_order_details');
$this->db->join('tbl_order', 'tbl_order_details.order_id = tbl_order.order_id ', 'left');
$this->db->join('tbl_product', 'tbl_order_details.product_code = tbl_product.product_id ', 'left');
$this->db->join('tbl_inventory', 'tbl_product.product_id = tbl_inventory.product_id ', 'left');
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
您可以使用MVC来解决此问题
//Controller
function get_data() {
$data['list'] = $this->your_model->get_data();
$this->load->view('your_view/location',$data);
}
//Model
function get_data() {
$sql = "your_query";
$list = $this->db->query($sql)->result_array();
return $list;
}
//View
foreach($list as $data) {
echo $data['your_selected_field']; // it could be table,text field,or just text
}
API参考: https : //www.codeigniter.com/user_guide/database/results.html#result-arrays
希望这可以帮助
首先,您可以创建OrderModel以使用订单表。 然后创建函数get_order_data。
//Order Model
public function get_orders() {
// your db query to get orders
return $result;
}
然后你可以使用$this->load->model('OrderModel');
在控制器中加载模型$this->load->model('OrderModel');
最好的地方是__construct来加载模型
并调用你的get_orders()函数
// Your controller
// Create data array to store front data
$data = [];
$data['orders'] = $this->OrderModel->get_orders();
获得$ orders后,使用以下代码将其传递给查看
$this->load->view('path/to/view', $data);
最后,您可以使用$ orders在视图中调用您的订单数据
我希望这会对你有所帮助
您需要为表设置别名才能正常运行。
$this->db->select('a.*,a.order_details_id, b.order_id ', false);
$this->db->from('tbl_order_details as a');
$this->db->join('tbl_order as b', 'a.order_id = b.order_id ', 'left');
如果只想选择1行,则需要使用此功能。
$this->db->get('tbl_order_details')->row_array();
或者如果你想要所有结果行,你需要使用它。
$this->db->get('tbl_order_details')->result_array();
在控制器上,您应该添加以下行以在结果页面上传递结果。
public function index()
{
$data['orderDetails'] = $this->your_model->get_order_return_info();
$this->load->view('pages/yourview/index',$data);
}
最后将订单详细信息设置为文本框。 你需要像这样在文本框中回显它。
<input type="Text" name="orderID" value="<?php echo $orderDetails['order_id'];?>" >
如果使用result_array(),则需要使用foreach循环来回显它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.