如何在另一个表列中插入一个表数据

时间:2017-12-26 05:51:04

标签: php mysql sql

我有表server_menuresponse,列id为menu_order menu_name menu_link data_icon sub_menu 和另一个表sub_menu,列id为mainmenu_id submenu_order submenu_name submenu_link submenu_icon

我想在server_menuresponse的sub_menu列中获取sub_menu表的所有列

Result should be like this image

我希望php结果应该像:

<?php
stdClass::__set_state(array(

   'server_menuresponse' => 
  array (
    0 => 
    stdClass::__set_state(array(
       'id' => '1',
       'menu_order' => '1',
       'menu_name' => 'Home',
       'menu_link' => '',
       'data_icon' => '',
       'sub_menu' => 
      array (
      ),
    )),
    1 => 
    stdClass::__set_state(array(
       'id' => '2',
       'menu_order' => '2',
       'menu_name' => 'New',
       'menu_link' => '',
       'data_icon' => '',
       'sub_menu' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'id' => '5',
           'mainmenu_id' => '2',
           'submenu_order' => '3',
           'submenu_name' => 'Auto',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/auto.png',
        )),
        1 => 
        stdClass::__set_state(array(
           'id' => '4',
           'mainmenu_id' => '2',
           'submenu_order' => '2',
           'submenu_name' => 'Bike',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/bike.png',
        )),
        2 => 
        stdClass::__set_state(array(
           'id' => '6',
           'mainmenu_id' => '2',
           'submenu_order' => '4',
           'submenu_name' => 'Bus',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/bus.png',
        )),
        3 => 
        stdClass::__set_state(array(
           'id' => '3',
           'mainmenu_id' => '2',
           'submenu_order' => '1',
           'submenu_name' => 'Car',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/car.png',
        )),
        4 => 
        stdClass::__set_state(array(
           'id' => '1',
           'mainmenu_id' => '2',
           'submenu_order' => '6',
           'submenu_name' => 'Find All New Vehicle',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/new_vehicle.png',
        )),
        5 => 
        stdClass::__set_state(array(
           'id' => '7',
           'mainmenu_id' => '2',
           'submenu_order' => '5',
           'submenu_name' => 'Lorry',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/lorry.png',
        )),
        6 => 
        stdClass::__set_state(array(
           'id' => '2',
           'mainmenu_id' => '2',
           'submenu_order' => '7',
           'submenu_name' => 'New Vehicle Offer',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/new_offer.png',
        )),
      ),
    )),
    2 => 
    stdClass::__set_state(array(
       'id' => '3',
       'menu_order' => '3',
       'menu_name' => 'Used',
       'menu_link' => '',
       'data_icon' => '',
       'sub_menu' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'id' => '11',
           'mainmenu_id' => '3',
           'submenu_order' => '3',
           'submenu_name' => 'Auto',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/auto.png',
        )),
        1 => 
        stdClass::__set_state(array(
           'id' => '10',
           'mainmenu_id' => '3',
           'submenu_order' => '2',
           'submenu_name' => 'Bike',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/bike.png',
        )),
        2 => 
        stdClass::__set_state(array(
           'id' => '12',
           'mainmenu_id' => '3',
           'submenu_order' => '4',
           'submenu_name' => 'Bus',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/bus.png',
        )),
        3 => 
        stdClass::__set_state(array(
           'id' => '9',
           'mainmenu_id' => '3',
           'submenu_order' => '1',
           'submenu_name' => 'Car',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/car.png',
        )),
        4 => 
        stdClass::__set_state(array(
           'id' => '8',
           'mainmenu_id' => '3',
           'submenu_order' => '5',
           'submenu_name' => 'Find All Used Vehicle',
           'submenu_link' => '',
           'submenu_icon' => 'menu_icon/new_vehicle.png',
        )),
      ),
    )),
    3 => 
    stdClass::__set_state(array(
       'id' => '4',
       'menu_order' => '4',
       'menu_name' => 'News & Reviews',
       'menu_link' => '',
       'data_icon' => '',
       'sub_menu' => 
      array (
        0 => 
        stdClass::__set_state(array(
           'id' => '15',
           'mainmenu_id' => '4',
           'submenu_order' => '3',
           'submenu_name' => 'Car Videos',
           'submenu_link' => '',
           'submenu_icon' => '',
        )),
        1 => 
        stdClass::__set_state(array(
           'id' => '17',
           'mainmenu_id' => '4',
           'submenu_order' => '5',
           'submenu_name' => 'Feature Stories',
           'submenu_link' => '',
           'submenu_icon' => '',
        )),
        2 => 
        stdClass::__set_state(array(
           'id' => '14',
           'mainmenu_id' => '4',
           'submenu_order' => '2',
           'submenu_name' => 'Road Test',
           'submenu_link' => '',
           'submenu_icon' => '',
        )),
        3 => 
        stdClass::__set_state(array(
           'id' => '13',
           'mainmenu_id' => '4',
           'submenu_order' => '1',
           'submenu_name' => 'User Reviews',
           'submenu_link' => '',
           'submenu_icon' => '',
        )),
        4 => 
        stdClass::__set_state(array(
           'id' => '16',
           'mainmenu_id' => '4',
           'submenu_order' => '4',
           'submenu_name' => 'Write Review',
           'submenu_link' => '',
           'submenu_icon' => '',
        )),
      ),
    )),
  ),
 ),
));
?>

1 个答案:

答案 0 :(得分:0)

我完全改变了我的回答!

根据这个问题的答案:How to group array data returned by left join query in php

我认为你做了两个查询。像这样:

$data = array();

// get all the menu items 
$rows = $db->query("SELECT * from server_menuresponse order by id");
while ($row = mysqli_fetch_assoc($rows)) {
    $row['sub_menu'] = []; // blank array for sub menu
    $data[$row['id']] = $row;
}

// get sub menu items, add them to the correct menu item
$rows = $db->query("SELECT * from sub_menu order by mainmenu_id, id");
while ($row = mysqli_fetch_assoc($rows)) {
    $data[$row['mainmenu_id']]['sub_menu'][] = $row;
}

print_r($data);