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

  • Home
  • SEARCH
  • 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 7180163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:17:26+00:00 2026-05-28T17:17:26+00:00

I have a query which shows all images from a user. (In this case

  • 0

I have a query which shows all images from a user. (In this case the user_id = 3.)

I want to return the results of the query and display near the user record, a static picture which I have uploaded. This picture is a small icon.

Here is my sql query:

$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 = 3
        ORDER BY i.date DESC";

And here are the results:

[images_list] => Array
    (
        [0] => Array
            (
                [user] => 3333
                [user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_nsm5rixy14lexm9cy15wzyg9u_224.jpg
                [image] => http://127.0.0.1/auth_system_1/upload_images/224/224_nsm5rixy14lexm9cy15wzyg9u_224.jpg
                [image_id] => 5
                [text] => 
                [image_date] => 7 hours, 1 minute
                [comments] => 2
            )

        [1] => Array
            (
                [user] => 3333
                [user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_nsm5rixy14lexm9cy15wzyg9u_224.jpg
                [image] => http://127.0.0.1/auth_system_1/upload_images/224/224_gfbyjh6zf66g914e28bsfdkuf_f4d.jpg
                [image_id] => 3
                [text] => 
                [image_date] => 20 hours, 50 minutes
                [comments] => 0
            )

        [2] => Array
            (
                [user] => 3333
                [user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_nsm5rixy14lexm9cy15wzyg9u_224.jpg
                [image] => http://127.0.0.1/auth_system_1/upload_images/224/224_80jrg3z0xrh9isskc3yuhtqh1_163.jpeg
                [image_id] => 1
                [text] => test1
                [image_date] => 1 day, 22 hours
                [comments] => 0
            )

    )

I do not have any idea how to place a small icon near the first image that is returned from the query. If I had one more field with a 1 indicating it is the first record, or a 1 indicating it is not the first record, I think I could do what I want.

By the way, the fields of my images table are as follows:

  • id
  • user_id
  • image
  • date

Is it possible to add one more field to the query, which will indicate with a 1 or 0 whether or not it is the latest record in the result set when sorting by date?

Maybe you can show me an easier way to do this?

  • 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-28T17:17:27+00:00Added an answer on May 28, 2026 at 5:17 pm

    It would be silly to make your SQL database add this field when you can do it very easily in PHP.

    Just create a boolean variable that is set to false on the first iteration of your loop.

    $first = true;
    
    for( $data as $key => $value )
    {
        if( $first )
        {
            echo "I'm the first row";
            $first = false;
        }
    
        // Do other stuff
    }
    

    EDIT: How to identify first and last item in an array

    $keys = array_keys( $data );
    $values = array_values( $data );
    
    $count = count( $data );
    
    for( $a = 0; $a < $count; $a++ )
    {
        $key = $keys[ $a ];
        $value = $values[ $a ];
    
        if( $a == 0 )
        {
            // First
        }
        else if( $a+1 == $count )
        {
            // Last
        }
    
        // All
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query which shows all images from a user. (In this case
I have a user table from which I want all values, so I have
I need to have a query which list all the user according to the
I am running oracle and have a query which pulls some results from the
I have the following MYSQL query which when executed shows me all the invalid
I have a query which gets me all groups the current user belongs to.
I have a page called list.php which retrieves from database and shows all the
I have a query which produces a list of orders like this: Invoice Description
I have this query which works correctly in MySQL. More background on it here
I have a query which returns a series of cells of data from a

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.