0

你好每个人我都有一个表结构像这样:

xl_Server id_server,主机名。(表父)

xl_port id_port,id_server。(餐桌孩子)

xl_connection id_Server,主机名_dst。(餐桌孩子)

我的控制器:

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

我的模型:

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
            )

    )   

   )

)

4

1 回答 1

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; 

}
于 2013-10-16T14:19:49.597 回答