dongzhuange2625 2017-10-04 03:44
浏览 33
已采纳

Laravel查询结果子数组没有说服力

please help me in my project at school. How can i query in laravel controller something like this situation. I have three tables: Shipping_table and Shipping_products and tbl_products, now my table structure is this:

Shipping table:

Ship_ID (INT AUTO INC)
AMOUNT (DOUBLE,2)
NAME (VARCHAR)
SHIP_DATE (DATE)
RECEIVER (VARCHAR)

Shipping_products:

ID (INT AUTO INC)
Ship_id (foreign key from shipping table)
Product_id

Products_table:

Product_id (Auto Inc)
name(varchar)
Qty(int)
Description (varchar)

now what i want is having a query result like this: I want to get all in the shipping table and in sub-array i want to get the products that is being listed in shipping_products with the desired shipping id.

Something a result like this: EXAMPLE I HAVE 2 Shipping_table values

Array(2) {
 [0] Array(4) {
  ['Ship_id'] "1"
  ['Amount'] "10000"
  ['Ship_date'] "1995-12-11"
  ['Ship_products'] Array(3)
      ['id'] "1" Array(2)
           ['product_id'] "5"
           ['name'] "Product 1"
      ['id'] "2" Array(2)
           ['product_id'] "6"
           ['name'] "Product 2"
      ['id'] "3" Array(2)
           ['product_id'] "10"
           ['name'] "Product 15"

 }
 [1] Array(4) {
   ['Ship_id'] "2"
   ['Amount'] "15000"
   ['Ship_date'] "1995-12-15"
   ['Ship_products'] Array(2)
      ['id'] "1" Array(2)
           ['product_id'] "5"
           ['name'] "Product 1"
      ['id'] "2" Array(2)
           ['product_id'] "6"
           ['name'] "Product 2"
 }
}
  • 写回答

1条回答 默认 最新

  • doutou7286 2017-10-04 04:09
    关注

    The SQL part is easy (using JOINS)

    SELECT * 
    FROM Shipping S
    LEFT JOIN Shipping_Products SP
      ON SP.Ship_Id=S.Ship_Id
    LEFT JOIN Products P
      ON P.Product_id=SP.Product_id
    

    The php part is more complicated, as you have to get the results in a loop and detect changes in Ship_id and Product_id and put in the resulting array.

    Since this is homework .. I'll leave it as an exercise ...


    THIS IS A QUICK ALGORITHM EXAMPLE - untested but logically sound.

    $cur_ship = '';
    $cur_prod = '';
    $results  = array();
    foreach ($resultset as $key => $row) {
       if ($cur_ship != $row['Ship_id']) {
          $cur_ship = $row['Ship_id'];
          $cur_prod = '';
          $results[$cur_ship] = array();
          // Fill ship info from $row
       }
       if ($cur_prod != $row['Product_id']) {
          $cur_prod = $row['Product_id'];
          $results[$cur_ship][$cur_prod] = array();
          // Fill Product info from $row
       }
       // FILL OUR $results[$cur_ship][$cur_prod] from $row
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题