如何将重复数据嵌入到单个JSON对象中

时间:2014-05-28 09:39:42

标签: mysql json database angularjs codeigniter

如何以JSON格式嵌入数据以下方式,我只能为每个大厅获取一个JSON对象。

正如你所看到的,我有来自两个表的数据,一个是来自“hall_information”的表,因为hall_name,service_provider_id& id将由用户输入一次。

但是在第二张表中,我有slot_name,slot_ending_time,slot_starting_time,hall_type的“hall_price”,作为用户重复可以描述他早上,晚上和整天的大厅价格,以及hall_type mini,mantap和bunquet 。所以这些行可以为每个大厅重复9次,每个大厅有3个hall_types,每个hall_type 3定价。

所以我想只将一行合并为json格式的查询结果。我正在使用PHP与Codeigniter&我的项目的angular.js。如何将此查询结果嵌入到单个json对象中。因此,当使用ng-repeat进行循环时,我只能得到一个大厅而不是9个同名的大厅。提前谢谢。

var hallinfo =
[
   {
    "hall_name":"Sai mantap","id":"12","service_provider_id":"35",
    "slot_name":"morning","slot_charge":"5000","slot_starting_time":"09:00:00","slot_ending_time":"03:00:00",
    "hall_info_id":"45","hall_type":"mantap"
   },

   {
    "hall_name":"Sai mantap","id":"12","service_provider_id":"35",
    "slot_name":"evening", "slot_charge":"10000","slot_starting_time":"05:00:00","slot_ending_time":"10:00:00",
    "hall_info_id":"45","hall_type":"mini"
   },

  {
    "hall_name":"Sai mantap","id":"12","service_provider_id":"35",
    "slot_name":"full_day","slot_charge":"15000","slot_starting_time":"09:00:00","slot_ending_time":"10:00:00",
    "hall_info_id":"45","hall_type":"bunquet"
  },

  {
    "hall_name":"Raman mantap ","id":"13","service_provider_id":"36",
    "slot_name":"morning","slot_charge":"5000","slot_starting_time":"09:00:00","slot_ending_time":"03:00:00",
    "hall_info_id":"46","hall_type":"mantap"
   },

   {
    "hall_name":"Raman mantap","id":"13","service_provider_id":"36",
    "slot_name":"evening", "slot_charge":"10000","slot_starting_time":"05:00:00","slot_ending_time":"10:00:00",
    "hall_info_id":"46","hall_type":"mini"
   },
  {
    "hall_name":"Raman mantap","id":"13","service_provider_id":"36",
    "slot_name":"full_day","slot_charge":"25000","slot_starting_time":"09:00:00","slot_ending_time":"10:00:00",
    "hall_info_id":"46","hall_type":"bunquet"
  },

];

这是表格模式。

CREATE TABLE hall_information (
    id integer NOT NULL,
    hall_name character varying(30),
    service_provider_id REFERENCES service_provider(id);
);

CREATE TABLE hall_pricing (
    id integer NOT NULL,
    slot_name slot_type NOT NULL,
    slot_charge integer NOT NULL,
    slot_starting_time time without time zone NOT NULL,
    slot_ending_time time without time zone NOT NULL,
    hall_info_id integer REFERENCES hall_information(id),
    hall_type character varying(20)
);

我获取所有记录的查询是

 public function get_hall_info(){
    $this->db->select('*');
    $this->db->from('hall_info');
    $this->db->join('hall_pricing', 'hall_info.id = hall_pricing.hall_info_id');
    $query =$this->db->get();
    return $query->result();
}

我想这会完成我的工作,但我不知道将我的数据转换成下面的表格。

var hallinfo =
[
       {
        "hall_name":"Sai mantap","id":"12","service_provider_id":"35",
         "slots":[
                  {
                    "slot_name":"morning","slot_charge":"5000","slot_starting_time":"09:00:00","slot_ending_time":"03:00:00",
                    "hall_info_id":"45","hall_type":"mantap"
                  },
                  {
                    "slot_name":"evening", "slot_charge":"10000","slot_starting_time":"05:00:00","slot_ending_time":"10:00:00",
                    "hall_info_id":"45","hall_type":"mini"
                  },
                  {
                    "slot_name":"full_day","slot_charge":"15000","slot_starting_time":"09:00:00","slot_ending_time":"10:00:00",
                    "hall_info_id":"45","hall_type":"bunquet"
                  }
               ]
       },
       {
        "hall_name":"Raman mantap ","id":"13","service_provider_id":"36",
         "slots":[ 
                        {
                          "slot_name":"morning","slot_charge":"5000","slot_starting_time":"09:00:00","slot_ending_time":"03:00:00",
                          "hall_info_id":"46","hall_type":"mantap"
                        },
                        {
                          "slot_name":"evening", "slot_charge":"10000","slot_starting_time":"05:00:00","slot_ending_time":"10:00:00",
                          "hall_info_id":"46","hall_type":"mini"
                        },
                        {
                         "slot_name":"full_day","slot_charge":"25000","slot_starting_time":"09:00:00","slot_ending_time":"10:00:00",
                         "hall_info_id":"46","hall_type":"bunquet"
                        }
               ]
       }                  
      ];

0 个答案:

没有答案