我是MySQL的新手,正在使用CodeIgniter。我有三个表,分别为lead, document
和bank
。
现在我正在做的是,在lead
表中,我插入了唯一的客户个人信息。在document
表中,我要插入用户文档。在bank
表中,我要插入银行记录。可以是多个。
插入没有问题。从数据库中获取数据时出现问题。
现在的表结构是
潜在客户
id | name | mobile | email
1 | asdff |1234567831 |asd@gmail.com
2 | kjhgg |1231231231 |mnhg@gmail.com
3 | qwsde |1231233212 |oiuk@gmail.com
<!--many more-->
文档
doc_id |doc_name | doc_date | lead_id |date_of_added
1 |asdasd |2019-06-24 | 1 |2019-06-16 17:31:07
2 |oiuytr |2019-06-24 | 2 |2019-06-16 17:31:07
3 |okjhyt |2019-06-25 | 3 |2019-06-20 20:12:09
<!--many more-->
银行
bank_id | bankname | lead_id |date_of_added
1 | sdasdas | 1 |2019-06-20 11:41:34
2 | asdasdasd | 1 |2019-06-21 10:41:34
3 | asdakjkh | 2 |2019-06-21 14:23:12
4 | qwerfgvf | 1 |2019-06-21 23:56:25
<!--many more-->
到目前为止,单据与银行之间没有任何联系。我应该使用ID进行连接吗?
模型
public function getconfirmLeadinfo($id){
return $result = $this->db->select('*')
->from('lead')
->join('document','lead.id=document.lead_id','LEFT')
->join('bank','lead.id=bank.lead_id','LEFT')
->where('lead.id',$id)
->get()
->result();
}
我得到的输出是
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => asdff
[mobile] => 1234567831
[email] => asd@gmail.com
[doc_id] => 1
[doc_name] => asdasd
[doc_date] => 2019-06-24
[lead_id] => 1
[date_of_added] => 2019-06-16 17:31:07
[bank_id] => 1
[bankname] => sdasdas
[lead_id] => 1
[date_of_added] => 2019-06-20 11:41:34
)
[1] => stdClass Object
(
[id] => 1
[name] => asdff
[mobile] => 1234567831
[email] => asd@gmail.com
[doc_id] => 1
[doc_name] => asdasd
[doc_date] => 2019-06-24
[lead_id] => 1
[date_of_added] => 2019-06-16 17:31:07
[bank_id] => 2
[bankname] => asdasdasd
[lead_id] => 1
[date_of_added] => 2019-06-21 10:41:34
)
[2] => stdClass Object
(
[id] => 1
[name] => asdff
[mobile] => 1234567831
[email] => asd@gmail.com
[doc_id] => 1
[doc_name] => asdasd
[doc_date] => 2019-06-24
[lead_id] => 1
[date_of_added] => 2019-06-16 17:31:07
[bank_id] => 4
[bankname] => qwerfgvf
[lead_id] => 1
[date_of_added] => 2019-06-21 23:56:25
)
)
现在我在编辑页面上,我必须显示数据。
if ($post){?>
<input type="text" name="name" value="<?php echo $post->name;?>">
<input type="text" name="mobile" value="<?php echo $post->mobile;?>">
<input type="text" name="email" value="<?php echo $post->email;?>">
<input type="text" name="doc_name" value="<?php echo $post->doc_name;?>">
<input type="text" name="doc_date" value="<?php echo $post->doc_date;?>">
how do I display my bank details here? should I use something like this
<?php
foreach ($result as $key => $value) {
<input type="text" name="bank[]" value="$value->bankname">
}
}?>
你能帮我吗?
答案 0 :(得分:0)
使用foreach
<?php
foreach ($result as $key => $value) {
if ($value){?>
<input type="hidden" name="id[]" value="<?php echo $value->id;?>">
<input type="text" name="name[]" value="<?php echo $value->name;?>">
<input type="text" name="mobile[]" value="<?php echo $value->mobile;?>">
<input type="text" name="email[]" value="<?php echo $value->email;?>">
<input type="text" name="doc_name[]" value="<?php echo $value->doc_name;?>">
<input type="text" name="doc_date[]" value="<?php echo $value->doc_date;?>">
<?php }
}
?>
答案 1 :(得分:0)
使用两个单独的查询,如下所示:
$data = [];
$lead = $this->db->select('*')->from('lead')->get()->row();
$data['lead'] = $lead;
if($lead){
$bank = $this->db->select('*')->where('lead_id', $lead->lead_id)->from('bank')->get()->result();
$data['bank '] = $bank ;
}
// you can retrive documet like bank details
return $data
然后,您可以传递$data
来查看和使用lead
和bank
变量(如果存在)。
答案 2 :(得分:0)
//控制器:您可以在视图中使用的这些数组
function leadInfo($id){
$data=array();
$data['lead_detail']=$this->model_name->get_lead_detail_by_id($id);
$data['bank_details']=$this->model_name->get_bank_detail_by_lead_id($id);
$this->load-view('view_page_name',$data);
}
//型号
function get_lead_detail_by_id($id){
$this->db->join('document','document.lead_id=lead.id');
return $this->db->where('lead.id',$id)->get('leads')->row_array();
}
function get_bank_detail_by_lead_id($id){
return $this->db->where('lead_id',$id)->get('bank')->row_array();
}