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 9014621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:33:14+00:00 2026-06-16T03:33:14+00:00

I have a pictures table where users pictures are saving with their ID and

  • 0

I have a pictures table where users pictures are saving with their ID and pic physical link.

userID  |  picture
1       |  picnameLink
1       |  picnameLink
2       |  picnameLink
1       |  picnameLink
2       |  picnameLink
3       |  picnameLink

Now, I want to show maximum 3 pictures in a jquery picture gallery block where one block should show all 3 pictures from one same user and if a user have less than 3 pictures, it should show no image text.

I have tried to do with group by mysql query but I am not getting desired result. Do I have to use two loops?

–Edit for fthiella– Here is code

$query = "SELECT * FROM pictures GROUP BY userID";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
    $image_array[] = $row['picLink'];
    $id_array[] = $row['pic_id'];
}
$num_images_to_display = 3; /* MODIFY TO REFLECT NUMBER OF IMAGES TO SHOW PER SCREEN */
$num_images = count($image_array);
$image_path = "../images/"; /* MODIFY TO REFLECT THE PATH TO YOUR IMAGES */
$y = $num_images_to_display;
if(!isset($_GET['first'])){
        $first = 0;
    }else{
        $first = (int) $_GET['first'];
}
$x = $num_images - 1;
$z = $x - $y;
if($first>$z) {
    $first = $z;
}
$last = $first + $num_images_to_display;

And here is HTML area:

<div style="position:absolute; top:50px; left:100px; width:800px; text-align: center;">
    <?PHP
    $i = $first;
    while($i<$last) { $showme = $image_path . $image_array[$i]; ?>

<?php if($image_array[$i]!="") { ?><img src="<?PHP echo $showme; ?>" width="176px" height="197px"><?php } else { ?><img src="../image/no_image.jpg" width="176px" height="197px"><?PHP } ?>

    $prev = $first-1;
    $next = $first +1;
    if($prev<0){ $prev = 0; }
    ?>
</div>

Result of this query shows pictures in groups but I want maximum three pictures of each user where no image shows if a user has less than three images.

  • 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-06-16T03:33:16+00:00Added an answer on June 16, 2026 at 3:33 am

    I don’t know if there’s a better solution, but i think you could use this:

    SELECT p1.userID, p1.picture as pic1, p2.picture as pic2, p3.picture as pic3
    FROM
      pictures p1 left join pictures p2
      on p1.userID=p2.userID and p1.picture<>p2.picture
      left join pictures p3
      on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
    GROUP BY p1.userID
    

    This will select three images for each user. If a user has less than three images, it will show nulls, if it has more, it chooses three between all of them.

    An alternative, that show three images each one in a different row, is this query that makes use of variables:

    SELECT userid, picture
    FROM (
      SELECT
        userid,
        picture,
        case when @prec_id=userid then @row:=@row+1 else @row:=1 end as row,
        @prec_id:=userid
      FROM 
        `pictures`,
        (SELECT @prec_id:=0, @row:=0) s
      ORDER BY userid) s
    WHERE row<=3
    

    EDIT: to show three images for each user at a time I would use my first query, and I would start with some code like this:

    <?php
    $mysqli = new mysqli("localhost", "username", "password", "test");
    
    $image_path = "../images/";
    $no_image = "../image/no_image.jpg";
    
    if(!isset($_GET['first'])){
      $first = 0;
    } else {
      $first = (int) $_GET['first'];
    }
    
    if ($stmt = $mysqli->prepare("SELECT p1.userID, p1.picture as pic1, p2.picture as pic2, p3.picture as pic3
    FROM
      pictures p1 left join pictures p2
      on p1.userID=p2.userID and p1.picture<>p2.picture
      left join pictures p3
      on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
    GROUP BY p1.userID
    LIMIT ?,1")) {
    
        $stmt->bind_param("i", $first); 
        $stmt->execute();
        $stmt->bind_result($user, $pic1, $pic2, $pic3);
        $stmt->fetch();
        $stmt->close();
    }
    $mysqli->close();
    ?>
    <div style="position:absolute; top:50px; left:100px; width:800px; text-align: center;">
      <img src="<?PHP echo (isset($pic1) ? $image_path.$pic1 : $no_image); ?>" width="176px" height="197px">
      <img src="<?PHP echo (isset($pic2) ? $image_path.$pic2 : $no_image); ?>" width="176px" height="197px">
      <img src="<?PHP echo (isset($pic3) ? $image_path.$pic3 : $no_image); ?>" width="176px" height="197px">
    </div>
    

    (it has be improved, but you could start with it. I’m using mysqli instead of mysql)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a picture table which consists for users pictures Table Name:picture Columns: picture_id
I have Two tables. 1.Users table (Username , Name) 2.Picture table( ID , Username
I have a table in SQL Server 2005 with the following properties: Users (UserID,
I have registered members and I want to display their names and profile pictures
i have a pictures table : pictures(articleid,pictureurl) And an articles table : articles(id,title,category) So,
I have a table created like this: CREATE TABLE rh857_omf.picture(MeasNr TINYINT UNSIGNED, ExperimentNr TINYINT
I have a few pictures for a product and I want the user to
I have 2 tables: User and Picture. The Picture table has the key of
I'm making standard profile pictures for 5000 users, and right now i need to
I want to create an image upload for users to upload a profile picture

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.