CodeIgniter join() 不显示逗号分隔的值



我正在使用CodeIgniter,我遇到了连接方法的问题。

我想要db_services表中的服务service_id该表也存储在db_orders中同一列名service_id中,我service_iddb_services表中的主键。

现在,用户在implode()功能的帮助下通过单击所需服务的复选框来添加服务,该功能将数据正确放入service_id列下的数据库db_orders表中,即1,2,3,5

现在,当我从db_orders中检索数据时,我从数据库中获得了逗号分隔的值,当我在我的Order_model-> view_orders((函数中删除此行$this->db->join( $this->db->dbprefix('db_services'), $this->db->dbprefix('db_services').'.service_id = '.$this->db->dbprefix('db_orders').'.service_id' );时,该值工作正常,一切都像魅力一样工作,但是当我添加该行时,它仅显示一条记录(第一条记录(,例如,如果插入了两(2(个service_id,所以我只得到一个我猜在期间插入的第一个数字数据库。

<?php $services = explode(",", $value->service_id);
foreach ( $services as $service ): ?>
<a href="" class="btn btn-success btn-flat btn-xs"><?php echo $service; ?></a>
<?php endforeach; ?>

这是我解决的问题,但实际问题是当我使用表格方法join它不起作用,让我复制粘贴我的代码,这有助于您更好地解决问题,我已经提到了每一个小细节,如果您需要更多我可以进一步详细说明。

Order_model -> view_orders((

<?php
public function view_orders()
{
$this->db->select( '*' );
$this->db->from( $this->db->dbprefix('db_orders') );
$this->db->join( $this->db->dbprefix('db_order_status'), $this->db->dbprefix('db_order_status').'.order_status_id = '.$this->db->dbprefix('db_orders').'.order_status_id' );
$this->db->join( $this->db->dbprefix('db_users'), $this->db->dbprefix('db_users').'.user_id = '.$this->db->dbprefix('db_orders').'.user_id' );
$this->db->join( $this->db->dbprefix('db_services'), $this->db->dbprefix('db_services').'.service_id = '.$this->db->dbprefix('db_orders').'.service_id' );
$query = $this->db->get();
return $query->result();
}
?>

订单控制器 -> 索引((

public function index()
{
$data['orders'] = $this->order->view_orders();
$data['title'] = "View All Orders";
$this->load->template('orders/view_all_orders', $data);
}

订单/view_all_orders.php

<?php
$services = explode(",", $value->service_id);
foreach ( $services as $service ): ?>
<a href="" class="btn btn-success btn-flat btn-xs"><?php echo $service; ?></a>
<?php endforeach; ?>

好的,这是我提供的解决方案。

只需删除此行即可使用我的函数进行调整 ->//$this->db->join( $this->db->dbprefix('db_services'), $this->db->dbprefix('db_services').'.service_id = '.$this->db->dbprefix('db_orders').'.service_id' );

public function view_orders()
{
$this->db->select( '*' );
$this->db->from( $this->db->dbprefix('db_orders') );
$this->db->join( $this->db->dbprefix('db_order_status'), $this->db->dbprefix('db_order_status').'.order_status_id = '.$this->db->dbprefix('db_orders').'.order_status_id' );
$this->db->join( $this->db->dbprefix('db_users'), $this->db->dbprefix('db_users').'.user_id = '.$this->db->dbprefix('db_orders').'.user_id' );
//$this->db->join( $this->db->dbprefix('db_services'), $this->db->dbprefix('db_services').'.service_id = '.$this->db->dbprefix('db_orders').'.service_id' );
$query = $this->db->get();
return $query->result();
}

我在 Order_model ->get_services()下创建了另一个函数,如下所示。

public function get_services()
{
$query = $this->db->get( $this->db->dbprefix('db_services') );
return $query->result();
}

现在我的控制器看起来像这样

public function index()
{
$data['get_services'] = $this->order->get_services(); // added this line to get all services and show it view by respective id
$data['orders'] = $this->order->view_orders();
$data['title'] = "View All Orders";
$this->load->template('orders/view_all_orders', $data);
}

我现在的观点是这样的。

<?php foreach( $orders as $order ): ?>
<tr>
<td><a href="/orders/view/<?php echo $order->order_id; ?>"><?php echo $order->order_id; ?></a></td>
<td><?php echo $order->status; ?></td>
<td>
<a href="/user/view/<?php echo $order->user_id; ?>"><?php echo $order->name;?></a>
</td>
<td>
<?php
$service_ids = explode(',', $order->service_id);
foreach ($service_ids as $service_id) {
foreach ($get_services as $row) {
if ($service_id == $row->service_id) {
echo $row->service_name;
}
}
}
?>
</td>
<td><?php echo gmdate('l jS F, Y',$order->created); ?></td>
<td>
<a class="btn btn-xs btn-flat btn-info" href="/orders/view/<?php echo $order->order_id; ?>"><i class="fas fa-eye"></i></a>
<a class="btn btn-xs btn-flat btn-success" href="/orders/edit/<?php echo $order->order_id; ?>"><i class="fas fa-edit"></i></a>
<a class="btn btn-xs btn-flat btn-danger" href="/orders/delete/<?php echo $order->order_id; ?>"><i class="fas fa-trash"></i></a>
</td>
</tr>
<?php endforeach; ?>

就这帮助我解决了我的问题而言,但我仍然感到困惑,我是否遵循正确的方向

相关内容

  • 没有找到相关文章