Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7417715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:45:07+00:00 2026-05-29T07:45:07+00:00

My users have pages: user favorites users image I try to do a page

  • 0

My users have pages:

  • user favorites
  • users image

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

  • id
  • username

images

  • id
  • user_id
  • image
  • description
  • date <— uplaod date

user_favorites

  • id
  • user_id <—user id who like image
  • image_id <—id of liked image
  • date <—date when image was clicked “liked”

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
            )

    )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:45:08+00:00Added an answer on May 29, 2026 at 7:45 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have page where user upload they file to my web server which get
I have created a user login page and am using Forms Authentication. Users are
Let's say I have a RoR app with 3 pages. The user can get
I have a page where a user can import data to the site. either
I have a model Book which can be added to user's Favorites. So I
I have two pages, NonMember.aspx and Member.aspx. If a user comes to the site,
I have a private site that requires login for all pages. When a user
I have a page where user dynamically add search condition(s) to filter out records.
I would like to have the page on user/sidebar on the right in the
I'm trying to find out if friends of a user have liked a page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.