[英]Make everything page representing data from different tables in different html structure
I want to make a page "everything" on my website where will be all my users activity presented. 我想在我的网站上创建一个“一切”页面,其中将显示所有用户活动。 So I need to make sql from two tables "images" and "user_favorites" and output all to this html design 因此,我需要从两个表“ images”和“ user_favorites”中制作sql并将其全部输出到此html设计中
My tables structure: 我的表结构:
users 使用者
id
images 图片
id
user_id
image
description
date
user_favorites user_favorites
id
date
I have got this sql query 我有这个SQL查询
function users_everything($user_id)
{
$sql = "SELECT
i.description as text,
UNIX_TIMESTAMP(i.date) as image_date,
COALESCE ( imgcount.cnt, 0 ) as comments,
fav.id as favorite_id,
r.image as favorited_image,
u2.username as favorite_user,
t.image as favorite_user_image
FROM users u
LEFT JOIN images i ON i.user_id = u.id
LEFT JOIN images p ON p.id = (SELECT b.id FROM images AS b where u.id = b.user_id ORDER BY b.id DESC LIMIT 1)
LEFT JOIN (SELECT image_id, COUNT(*) as cnt FROM commentaries GROUP BY image_id ) imgcount ON i.id = imgcount.image_id
LEFT JOIN user_favorites fav ON fav.user_id = u.id
LEFT JOIN images r ON r.id = fav.image_id
LEFT JOIN users u2 ON u2.id = r.user_id
LEFT JOIN images t ON t.id = (SELECT b.id FROM images AS b where u2.id = b.user_id ORDER BY b.id DESC LIMIT 1)
WHERE i.user_id = ?
";
$query = $this->db->query($sql, $user_id);
return $query->result_array();
}
My "images" table have 1 record and "user_follow" 2 records but if I do foreach it returns two favorites and two images (image) was duplicated. 我的“图像”表有1条记录,“ user_follow”有2条记录,但是如果我这样做,它将返回两个收藏夹,并且复制了两个图像(图像)。
I do not make such things like everything page before where will be presented data from different pages to different html structure. 我不会像所有页面那样制作东西,然后再将不同页面的数据呈现给不同的html结构。 I suppose you have experience with it and helps me 我想你有经验,可以帮助我
You need to convert your array of flat records into a tree structure in which each image contains a list of favorites. 您需要将平面记录数组转换为树结构,其中每个图像都包含一个收藏夹列表。 Something like: 就像是:
$images = array();
foreach($everything_list as $item)
{
// Check to see if already encountered this image
$imageId = $item['image_id'];
if (!isset($images[$imageId]))
{
// New image
$image = array();
// Do this for each image attribute
$image['image_id'] = $item['image_id'];
$image['favorites'] = array();
$images[$imageId] = $image;
}
// Add Favorite
$favorite = array();
// Do this for each favorite attribute
$favorite['favorite_id'] = $item['favorite_id'];
$images[$imageId]['favorites'][] = $favorite;
}
print_r($images);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.