查询:
SELECT
c.id campaign_id,
c.campaign_title,
c.start_date campaign_start_date,
c.end_date campaign_end_date,
cat.id category_id,
cat.category_title,
n.id nominee_id,
n.title nominee_title,
u.id voter_id,
u.fullname voter_name
FROM
campaign c
LEFT JOIN categories cat ON cat.campaign_id = c.id
LEFT JOIN category_nominees cn ON cn.category_id = cat.id
LEFT JOIN nominees n ON n.id = cn.nominee_id
LEFT JOIN category_votes cv ON cv.campaign_id = c.id
AND cv.category_id = cat.id
AND cv.nominee_id = n.id
LEFT JOIN users u on u.id = cv.user_id
WHERE
c.active = 1
ORDER BY
u.fullname,
c.campaign_title,
cat.category_order,
cat.category_title,
cn.nominee_order,
n.title
使用该查询,我将使用PHP在嵌套的关联数组中逻辑存储所有数据,然后无论何时循环数据或需要引用数据,您都会引用数组指针,而不是每次都执行新的查询。
例如: $ campaign [$ campaign_id] ['categories'] [$ category_id] ['nominees'] [$ nominee_id] ['title']是一种可能的方法。
我试过
$rows = [];
while($row = mysqli_fetch_array($query))
{
if (!isset($rows[$row['campaign_id']])) {
$rows[$row['campaign_id']] = array(
'campaign_id' => $row['campaign_id'],
'category_id' => array()
);
}
array_push ($rows[$row['campaign_id']]['category_id'],array(
'category_id' => $row['category_title'],
'nominees_id' => $row['nominee_id']
));
}
我无法理解。任何帮助将不胜感激!
答案 0 :(得分:0)
$campaigns = array(); while($row = mysqli_fetch_array($query)) {
$campaign_id = $row['campaign_id'];
$category_id = $row['category_id'];
$nominee_id = $row['nominee_id'];
$campaigns[$campaign_id]['categories'][$category_id]['nominees'][$nominee_id]['title']
= $row; }