0
我有一個問題,我只是編程方面的新手而且在邏輯問題上有點低。我想從MySQL查詢結果中生成數組值,並且它必須嵌套。插入一個數組的值取決於具有條件的數據表(嵌套數組)
我實際的代碼,我一直在努力:
<?php
$mostSold_arr = array();
$subData_array = array();
$day_arr = array();
$data = array();
// Connect to MySQL
$link = new mysqli('localhost', 'root', '', 'db');
if ($link->connect_errno) {
die("Failed to connect to MySQL: (" . $link->connect_errno . ") " . $link->connect_error);
}
$fetch_data = mysqli_query($link, "SELECT products.id as prodID, title, SUM(total_price) as value, orders.order_date as date FROM products LEFT JOIN order_details ON order_details.product_id=products.id LEFT JOIN orders ON orders.id=order_details.order_id GROUP BY order_details.product_id ORDER BY value DESC LIMIT 2 ") or die(mysqli_error($link));
while ($row_mainData = mysqli_fetch_assoc($fetch_data)) {
$mostSold_arr['category'] = $row_mainData['title'];
$mostSold_arr['value'] = $row_mainData['value'];
$prod_id['prodID'] = $row_mainData['prodID'];
$mostSold_arr['url'] = "#";
$mostSold_arr['description'] = "click to drill-down";
$mostSold_arr['data'] = array();
$fetch_subData = mysqli_query($link, "SELECT title, SUM(total_price) as value, MONTHNAME(orders.order_date) as date FROM products LEFT JOIN order_details ON order_details.product_id=products.id LEFT JOIN orders ON orders.id=order_details.order_id WHERE order_details.product_id=" . $prod_id['prodID'] ." GROUP BY title, date ORDER BY order_date LIMIT 3 ") or die(mysqli_error($link));
while ($row_subData = mysqli_fetch_assoc($fetch_subData)) {
$subData_arr['category']=$row_subData['date'];
$subData_arr['value']=$row_subData['value'];
$day_arr['data'] = array();
array_push($mostSold_arr['data'],$subData_arr);
$fetch_subDataDay = mysqli_query($link, "SELECT title, SUM(total_price) as value, DATE_FORMAT(orders.order_date, '%M-%d') as date FROM products LEFT JOIN order_details ON order_details.product_id=products.id LEFT JOIN orders ON orders.id=order_details.order_id WHERE order_details.product_id=" . $row_mainData['prodID'] ." AND DATE_FORMAT(orders.order_date, '%M-%d') LIKE '%March%' GROUP BY date ORDER BY order_date LIMIT 3 ") or die(mysqli_error($link));
while ($row_subDataDay = mysqli_fetch_assoc($fetch_subDataDay)) {
$subDataDay_arr['category']=$row_subDataDay['date'];
$subDataDay_arr['value']=$row_subDataDay['value'];
//array_push($day_arr['data'],$subDataDay_arr);
if($subData_arr['category'] == 'March'){
array_push($mostSold_arr['data'],$subDataDay_arr);
}
//array_push($day_arr['data'],$subDataDay_arr);
//var_dump($subData_arr);
}
}
array_push($data, $mostSold_arr);
}
$jsonData = json_encode($data, JSON_PRETTY_PRINT);
$fp = fopen('dataColMultiNested.json', 'w');
fwrite($fp, $jsonData);
fclose($fp);
die('<pre>' . print_r($jsonData, 1));
mysqli_close($link);
?>
上面的代碼的實際輸出:
[
{
"category": "ACER E5-473-53C0",
"value": "2455200.00",
"url": "#",
"description": "click to drill-down",
"data": [
{
"category": "March",
"value": "809100.00"
},
{
"category": "March-02",
"value": "27900.00"
},
{
"category": "March-03",
"value": "27900.00"
},
{
"category": "March-04",
"value": "55800.00"
},
{
"category": "April",
"value": "781200.00"
},
{
"category": "May",
"value": "418500.00"
}
]
},
{
"category": "ACER ASPIRE E5-551G-F57K LAPTOP",
"value": "2339220.00",
"url": "#",
"description": "click to drill-down",
"data": [
{
"category": "March",
"value": "449850.00"
},
{
"category": "March-02",
"value": "29990.00"
},
{
"category": "March-03",
"value": "59980.00"
},
{
"category": "March-07",
"value": "59980.00"
},
{
"category": "April",
"value": "689770.00"
},
{
"category": "May",
"value": "779740.00"
}
]
}
預期輸出(它應該是這樣的)
[
{
"category": "ACER E5-473-53C0",
"value": "2455200.00",
"url": "#",
"description": "click to drill-down",
"data": [
{
"category": "March",
"value": "809100.00",
"data": [
{
"category": "March-02",
"value": "27900.00"
},
{
"category": "March-03",
"value": "27900.00"
},
{
"category": "March-04",
"value": "55800.00"
}
]
},
{
"category": "April",
"value": "781200.00"
},
{
"category": "May",
"value": "418500.00"
}
]
},
{
"category": "ACER ASPIRE E5-551G-F57K LAPTOP",
"value": "2339220.00",
"url": "#",
"description": "click to drill-down",
"data": [
{
"category": "March",
"value": "449850.00",
"data": [
{
"category": "March-02",
"value": "29990.00"
},
{
"category": "March-03",
"value": "59980.00"
},
{
"category": "March-07",
"value": "59980.00"
}
]
},
{
"category": "April",
"value": "689770.00"
},
{
"category": "May",
"value": "779740.00"
}
]
}
]
我正在爲此工作約2晚或更長時間,但可以爲我的問題找到正確的解決方案。 我希望你們能幫助我。謝謝。
我需要它json格式,所以輸出應該有'{}'。順便謝謝你的回覆。我會試試.. – Marksmanship
@Marksmanship無論如何,在PHP中,你會像我展示的那樣得到它。如果你想用JSON格式,你必須實際做'$ json_year = json_encode($ year);'。它會導致只使用'{}'而不使用'[]'的東西。 – cFreed
沒有'[]'它仍然算作JSON格式?對不起,我只是一個新手。 – Marksmanship