将数据从不同的表sql返回到html

时间:2012-02-06 22:19:52

标签: php mysql sql

我的用户有网页:

  • 用户收藏夹
  • 用户图片

我尝试做一个页面“所有”,它将从2个sql表(已经有sql代码)获取数据到一个按日期排序的html页面。问题是“用户图像”有一个html结构,“用户收藏”有另一个。许多网站都有这样的页面,从不同的表格中输出不同html结构的不同数据。今天我第一次尝试这样做,并且不知道正确的方法。

我的桌子:

用户

  • ID
  • 用户名

图片

  • ID
  • USER_ID
  • 图像
  • 描述
  • date< --- uplaod date

user_favorites

  • id
  • user_id< ---喜欢图片的用户ID
  • image_id< ---喜欢图片的ID
  • 日期< ---点击图像的日期“已赞”

我用这个sql获取用户图像

function users_pictures($user_id)
{
    $sql = "SELECT username as user, p.image as user_image, i.image, i.id as image_id, i.description as text, UNIX_TIMESTAMP(i.date) as image_date, COALESCE ( imgcount.cnt, 0 ) as comments
            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
            WHERE i.user_id = ?
            ORDER BY i.date DESC";

    $query = $this->db->query($sql, $user_id);

    return $query->result_array();
}

我获取所有用户图像和用户当前图像 - 头像(上次上传图像是用户头像)

返回示例:

[images_list] => Array
    (
        [0] => Array
            (
                [user] => 8888
                [user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [image] => http://127.0.0.1/auth_system_1/upload_images/224/224_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [image_id] => 4
                [text] => 
                [image_date] => 50 minutes
                [comments] => 0
                [user_first_image] => 1
            )

    )

用户最喜欢的表是:

function users_favorites_list($user_id)
{
    $sql = "SELECT
                    u.username as user, 
                    p.image as user_image,
                    fav.id as favorite_id, 
                    UNIX_TIMESTAMP(fav.date) as favorite_date, 
                    i.id as images_id,
                    i.image,
                    i.description as text,
                    u2.username as favorite_user,
                    t.image as favorite_user_image

            FROM users u
            LEFT JOIN user_favorites fav ON fav.user_id = u.id
            LEFT JOIN user_follow f ON f.follow_id = fav.user_id
            LEFT JOIN images i ON i.id = fav.image_id
            LEFT JOIN users u2 ON u2.id = i.user_id
            LEFT JOIN images p ON p.id = (SELECT b.id FROM images AS b where fav.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
            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 fav.user_id = ?
            GROUP BY fav.id
            ORDER BY fav.date DESC";

    $query = $this->db->query($sql, array($user_id, $user_id));

    return $query->result_array();        
}

返回示例:

Array
(
    [0] => Array
        (
            [user] => 8888
            [user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
            [favorite_id] => 5
            [favorite_date] => 18 minutes ago
            [images_id] => 2
            [image] => http://127.0.0.1/auth_system_1/upload_images/100/100_flw3utn9igiqh7dtt2o61ydf8_174.jpeg
            [text] => 3
            [favorite_user] => 6666
            [favorite_user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_flw3utn9igiqh7dtt2o61ydf8_174.jpeg
        )

    [1] => Array
        (
            [user] => 8888
            [user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
            [favorite_id] => 2
            [favorite_date] => 1 week ago
            [images_id] => 4
            [image] => http://127.0.0.1/auth_system_1/upload_images/100/100_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
            [text] => 
            [favorite_user] => 8888
            [favorite_user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
        )

)

![我的html结构,我尝试从2 sql返回数据] [1]

我需要从一个页面中的2个表中选择具有不同html结构的用户活动(例如见图片)。我想很多人都会这样做。请告诉我怎么做?

[everything_list] => Array
    (
        [0] => Array
            (
                [user] => 8888
                [user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [favorite_id] => 5
                [favorite_date] => 1328565406
                [images_id] => 2
                [image] => flw3utn9igiqh7dtt2o61ydf8_174.jpeg
                [text] => 3
                [favorite_user] => 6666
                [favorite_user_image] => flw3utn9igiqh7dtt2o61ydf8_174.jpeg
            )

        [1] => Array
            (
                [user] => 8888
                [user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [favorite_id] => 2
                [favorite_date] => 1327856547
                [images_id] => 4
                [image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [text] => 
                [favorite_user] => 8888
                [favorite_user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
            )

    )

1 个答案:

答案 0 :(得分:1)

以下查询会返回您需要的内容吗?

SELECT 
    username as user, 
    p.image as user_image, 
    i.image, 
    i.id as image_id, 
    i.description as text, 
    UNIX_TIMESTAMP(i.date) as image_date, 
    COALESCE ( imgcount.cnt, 0 ) as comments,
    fav.id as favorite_id,
    UNIX_TIMESTAMP(fav.date) as favorite_date,
    u2.username as favorite_user,
    t.image as favorite_user_image
FROM users u
LEFT JOIN user_favorites fav ON fav.user_id = u.id
LEFT JOIN user_follow f ON f.follow_id = fav.user_id
LEFT JOIN images i ON i.user_id = u.id
LEFT JOIN users u2 ON u2.id = i.user_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 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 u.user_id = ?
GROUP BY fav.id
ORDER BY i.date DESC