2017-06-19 60 views
-1

多個ID在MySQL表如何expload並獲得另一個MySQL表中的特定ID值笨多個ID如何expload並獲得笨id值

頁面視圖圖像添加,

查看頁面

`<?php foreach($company_branch as $loop){ ?> 
    <tr> 
    <td></td> 
    <td><?=$loop->branch_name ?></td> 
    <td><?=$loop->branch_head ?></td> 
    <td><?=$loop->departments_list_id ?></td> 
    <td><?=$loop->write_date ?></td> 
    </tr> 
    <?php } ?>` 

控制器Page

 `public function company_settings() { 
     $data['company_branch'] = $this->settings_model->company_branch(); 
     $this->load->view('settings/company_settings',$data);` 

模型Page

`function company_branch(){  
    $this->db->select('company_branch.*, 
     company_departments.department_name as departments_name ')- 
    >from('company_branch'); 
    $this->db- >join('company_departments',' 
    company_branch.departments_list_id = company_departments.id'); 
    $query = $this->db->get();  
    return $query->result(); 
    }` 


    How to display the `departments_list_id ` to department names in view 
    page, 

The image is company_branch and company_department mysql table view

browser View page

+0

那麼問題是什麼? –

+0

如何在瀏覽器視圖頁面部門列中顯示department_names不在id中 – kathir

回答

1

查看頁面

  `<?php foreach($company_branch as $loop){ ?> 
      <tr> 
      <td></td> 
      <td><?=$loop->branch_name ?></td> 
      <td><?=$loop->branch_head ?></td> 
      <td> 
       <?php 
        foreach(explode(",",$loop->departments_list_id) as $department) 
        { $depart=$this->db->query("select department_name from company_departments where id=".$department)->row()->department_name; 
        echo $depart.',<br />'; 
       } 
       ?> 
      </td> 
      <td><?=$loop->write_date ?></td> 
      </tr> 
      <?php } ?>` 
0

不能進入某些ID爲1場。您可以將一些字段添加到另一個ID。示例字段:departement_id_1,departement_id_2,departement_id_4,departement_id_5等,視需要而定。

你可以改變你的SQL選擇這樣的:

select branc.*, 
(select dept.departement_name as departement_name_1 from company_departement as dept where branc.departement_id_1=dept.id), 
(select dept.departement_name as departement_name_2 from company_departement as dept where branc.departement_id_2=dept.id), 
(select dept.departement_name as departement_name_3 from company_departement as dept where branc.departement_id_2=dept.id) 
from company_branc as b 

那麼你可以使用腳本從表單

<?php foreach($company_branch as $loop){ ?> 
    <tr> 
    <td></td> 
    <td><?=$loop->branch_name ?></td> 
    <td><?=$loop->branch_head ?></td> 
    <td><?=$loop->departments_name_1 ?></br> 
<?=$loop->departments_name_2 ?></br> 
<?=$loop->departments_name_3 ?> 
</td> 
    <td><?=$loop->write_date ?></td> 
    </tr> 
    <?php } ?> 

我希望這有助於