Viktors
Viktors

Reputation: 935

return data from different table sql to html

My users have pages:

I try to do a page "everything" which will get data from 2 sql tables (already have sql code) to one html page ordering all by date. Problem is that "user image" have one html structure and "user favorites" another. Many website have such pages where is output different data in different html structure from different tables. Today I first time tried to do this and do not know the correct way.

My tables:

users

images

user_favorites

I get user images with this 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();
}

I get all users image and user current image - avatar (last upload image is user avatar)

return example :

[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
            )

    )

User favorite table is :

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();        
}

return example:

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
        )

)

:

![My html structure where I try to return data from 2 sql][1]

I need select user activities from 2 table in one page with different html structures (example see image) . I think many peole do it. Please tell me how to do this?

[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
            )

    )

Upvotes: 1

Views: 368

Answers (1)

user800014
user800014

Reputation:

The query below return what you need?

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

Upvotes: 1

Related Questions