获取Data Multidimensi数组Codeigniter Mysql

时间:2013-10-15 15:14:45

标签: mysql codeigniter

Hello每个人都有一个表结构像这样:

xl_Server id_server,hostname。 (表父)

xl_port id_port,id_server。 (表孩子)

xl_connection id_Server,hostname_dst。 (表孩子)

我的控制器:

$bc['dt_connectto'] = $this->server_model->getDataConnectto($param);

my_model:

public function getDataConnectto($param)
{   

    $this->db->select('*');   
    $this->db->from('xl_connection');  
    $this->db->join('xl_rack', 'xl_rack.id_rack = xl_connection.id_rack');
    $this->db->join('xl_server', 'xl_connection.id_server = xl_server.id_server');
    $this->db->join('xl_type_connection', 'xl_connection.id_type_connection = xl_type_connection.id_type_connection'); 
    $this->db->where('xl_server.id_server',$param); 

    $query_c = $this->db->get();

    if ( $query_c->num_rows() > 0 )
    {
       $rows = $query_c->result_array();
    }

    foreach ($rows as $rs) {

    $key = $rs['id_server'];

        }


    $this->db->select('*');   
    $this->db->from('xl_port'); 
    $this->db->join('xl_server', 'xl_port.id_server = xl_server.id_server');
    $this->db->where('xl_server.id_server',$key);

    $query_p = $this->db->get();

    if ($query_p->num_rows() > 0)
    {
       $rows['id_server']  = $query_p->result_array();
    }

    return $rows; 

} 

我的观点

 if($dt_connectto > 0)
            { 

                foreach($dt_connectto as $db)
                {
            ?>
            <tr id="<?php echo $db['id_server']; ?>" class="edit_tr"> 
                <!--td><?php //echo $no; ?></td-->
                <td><?php echo $db['id_lantai']; ?></td>
                <td><?php echo $db['equipment_type']; ?></td>
                <td><?php echo $db['hostname']; ?></td>   
                <td><?php echo $db['port']; ?></td>   
                <td><?php echo $db['nama_rack']; ?></td>
                <td><?php echo $db['hostname_dst']; ?></td>  
                <td><?php echo $db['board_slot_port']; ?></td> 
                <td><?php echo $db['card_equipment']; ?></td>  
                <td><?php echo $db['connect_via']; ?></td>   
                <td><?php echo $db['keterangan']; ?></td>
            </tr>
            <?php
                // $no++;
                }
            }
            else
            {
                ?>

            <tr style="text-align:center;">
                <td colspan="5">EMPTY DATA</td>
            </tr>
                <?php
            }
                            ?>

输出数据如下:

Array
(
 [0] => Array
    (
        [id_connection] => 22
        [id_rack] => 12
        [id_server] => 3
        [id_type_connection] => 1
        [board_slot_port] => FROM 01
        [card_equipment] => 
        [equipment_type] => 
        [hostname_dst] => BDG 01
        [ipaddress_dst] => -
        [keterangan] => 
        [id_lantai] => 2
        [nama_rack] => A.15.5
        [row] => A
        [column] => 15
        [no] => 5
        [u_shaft] => 2-5
        [id_userxl] => 8
        [id_product] => 302
        [hostname] => BDGDRCBSS04
        [project_name] => Mediaserver Cloud
        [ipaddress] => 10.24.125.17
        [connect_via] => FO
    )

[1] => Array
    (
        [id_connection] => 23
        [id_rack] => 12
        [id_server] => 3
        [id_type_connection] => 1
        [board_slot_port] => 02
        [card_equipment] => 
        [equipment_type] => 
        [hostname_dst] => BDG 02
        [ipaddress_dst] => -
        [keterangan] => 
        [id_lantai] => 2
        [nama_rack] => A.15.5
        [row] => A
        [column] => 15
        [no] => 5
        [u_shaft] => 2-5
        [id_userxl] => 8
        [id_product] => 302
        [hostname] => BDGDRCBSS04
        [project_name] => Mediaserver Cloud
        [ipaddress] => 10.24.125.17
        [connect_via] => FO
    )

[id_server] => Array
    (
        [0] => Array
            (
                [id_port] => 9
                [id_server] => 3
                [port] => 01
                [card_equipment] => 01
                [id_userxl] => 8
                [id_rack] => 12
                [id_product] => 302
                [hostname] => BDGDRCBSS04
                [equipment_type] => 
                [project_name] => Mediaserver Cloud
                [ipaddress] => 10.24.125.17
            )

        [1] => Array
            (
                [id_port] => 10
                [id_server] => 3
                [port] => 02
                [card_equipment] => 02
                [id_userxl] => 8
                [id_rack] => 12
                [id_product] => 302
                [hostname] => BDGDRCBSS04
                [equipment_type] => 
                [project_name] => Mediaserver Cloud
                [ipaddress] => 10.24.125.17
            )

    )

)

我的问题输出数组怎么样?帮助我...

Array
(
  [0] => Array
    (
        [id_connection] => 22
        [id_rack] => 12
        [id_server] => 3 
        [id_type_connection] => 1
        [board_slot_port] => FROM 01
        [card_equipment] => 
        [equipment_type] => 
        [hostname_dst] => BDG 01
        [ipaddress_dst] => -
        [keterangan] => 
        [id_lantai] => 2
        [nama_rack] => A.15.5
        [row] => A
        [column] => 15
        [no] => 5
        [u_shaft] => 2-5
        [id_userxl] => 8
        [id_product] => 302
        [hostname] => BDGDRCBSS04
        [project_name] => Mediaserver Cloud
        [ipaddress] => 10.24.125.17
        [connect_via] => FO
        [id_server] => Array 

           [0] => Array
             (
                [id_port] => 9
                [id_server] => 3
                [port] => 01
                [card_equipment] => 01
                [id_userxl] => 8
                [id_rack] => 12
                [id_product] => 302
                [hostname] => BDGDRCBSS04
                [equipment_type] => 
                [project_name] => Mediaserver Cloud
                [ipaddress] => 10.24.125.17
            )

    )

[1] => Array
    (
        [id_connection] => 23
        [id_rack] => 12
        [id_server] => 3
        [id_type_connection] => 1
        [board_slot_port] => 02
        [card_equipment] => 
        [equipment_type] => 
        [hostname_dst] => BDG 02
        [ipaddress_dst] => -
        [keterangan] => 
        [id_lantai] => 2
        [nama_rack] => A.15.5
        [row] => A
        [column] => 15
        [no] => 5
        [u_shaft] => 2-5
        [id_userxl] => 8
        [id_product] => 302
        [hostname] => BDGDRCBSS04
        [project_name] => Mediaserver Cloud
        [ipaddress] => 10.24.125.17
        [connect_via] => FO
        [id_server] => Array
           [1] => Array
            (
                [id_port] => 10
                [id_server] => 3
                [port] => 02
                [card_equipment] => 02
                [id_userxl] => 8
                [id_rack] => 12
                [id_product] => 302
                [hostname] => BDGDRCBSS04
                [equipment_type] => 
                [project_name] => Mediaserver Cloud
                [ipaddress] => 10.24.125.17
            )

    )   

   )

1 个答案:

答案 0 :(得分:0)

请在这里查看您的foreach()循环

foreach ($rows as $rs) {

    $key = $rs['id_server'];

}

这个循环在你的概念中是“错误的”,因为如果它运行不止一次,旧值就会“丢失”

这应该是您的答案,只需通过var_dump()

进行调试即可
public function getDataConnectto($param) {   

    $this->db->select('*');   
    $this->db->from('xl_connection');  
    $this->db->join('xl_rack', 'xl_rack.id_rack = xl_connection.id_rack');
    $this->db->join('xl_server', 'xl_connection.id_server = xl_server.id_server');
    $this->db->join('xl_type_connection', 'xl_connection.id_type_connection = xl_type_connection.id_type_connection'); 
    $this->db->where('xl_server.id_server',$param); 

    $query_c = $this->db->get();

    if ( $query_c->num_rows() > 0 ) {

       $rows = $query_c->result_array();

    } else {

        return FALSE; //no result is going back

    }

    foreach ($rows as $key => $rs) {

        //$key is position in $rows array (object)

        $this->db->select('*');   
        $this->db->from('xl_port'); 
        $this->db->join('xl_server', 'xl_port.id_server = xl_server.id_server');
        $this->db->where('xl_server.id_server', $rs['id_server']);

        $query_p = $this->db->get();

        if ($query_p->num_rows() > 0) {

           $rows[$key]['id_server'] = $query_p->result_array();

        } else {

            $rows[$key]['id_server'] = NULL; //no servers

        }

    }

    return $rows; 

}