Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grouping images in a profile

Tags:

php

I have a profile that shows profiles in a list. as shown in the image below.

enter image description here

users table

 id    |  email  |  full_name  |  job_title  | bio |  profile_photo  

images table

 image_id  | id  | artist_img

CODE

<?php 

$db = dbconnect();
$stmt = $db->prepare('SELECT
 users.email,
 users.full_name,
 users.job_title,
 users.bio,
 users.profile_photo,
 images.id,
 images.artist_img
FROM users
INNER JOIN images ON users.id=images.id GROUP BY images.id');
$stmt->execute();
$result = $stmt->get_result();

while (($row = mysqli_fetch_assoc($result)) != false) {

        $id = $row['id'];        
        $full_name = $row['full_name'];    
        $email = $row['email'];   
        $job_title = $row['job_title'];
        $bio = $row['bio'];
        $ProfilePhoto = $row['profile_photo'];
        $artist_img = $row['artist_img'];    


        if (isset($ProfilePhoto) && ! empty($ProfilePhoto)) {
            $image = "$ProfilePhoto";
        } else {
            $image = "avatar.jpg";
       }

   echo "<div class='container team-wrap'>
           <div class='row'>
             <div class='col-md-6'>
                <img class='img-responsive' src='artist/$image'>
               </div>
                 <div class=\"col-md-6\">
                    <strong>$full_name<br>$job_title</strong>
                      <br>
                      <p>$bio</p>
                      <a href='mailto:$email' class='btn btn-info'>Contact Me</a>
                </div>
             </div>
          </div>

          <div class=\"container space team-wrap\">
           <div class=\"row\">
              <div class=\"col-lg-12\">
                    <div id=\"gallery-slider\" class=\"slider responsive\">
                      <div>";




                          echo"
                        <img src=\"gallery/$artist_img\" alt=\"\"></a>";  




                      echo "</div>
                    </div>
                  <hr>
               </div>
            </div>
        </div>";
    }        
 ?>

Problem area

echo"<img src=\"gallery/$artist_img\" alt=\"\"></a>"; 

The issue I am having is that it repeats the profile for each image if the user has 5 images it will add 5 profiles 1 for each img.

and does not show the other users profile at all. show how it shows up is look at the image for example its got 4 images under profile 1 and it shows there profile pic.. well it repats all that info for each image I want the pics that have the same id as the user to show up as a slider like below..

and it also refuses to show the other profiles of other users.

like image 629
Codi Avatar asked Feb 02 '17 05:02

Codi


People also ask

What is it called when you group photos together?

Photomontage is the process and the result of making a composite photograph by cutting, gluing, rearranging and overlapping two or more photographs into a new image. Sometimes the resulting composite image is photographed so that the final image may appear as a seamless physical print.

What is image grouping?

Grouping pictures lets you rotate, flip, resize, or arrange them together, as though they were a single picture. Group shapes, pictures, or other objects. Press and hold Ctrl while you click the shapes, pictures, or other objects to group. Tip: For help selecting a shape or picture, see Select a shape or other object.


2 Answers

This is because you are grouping the data in wrong fashion.

As your question describes you have multiple rows for a user id and you are trying to display these multiple results in a single row using the GROUP BY. So, This is not possible for database to group data like this because it does not know any relation between data and what is to do with multiple results.

However, you can use JOIN here as it is what you trying to do. Use the following query

SELECT * FROM `users` LEFT JOIN `images` ON images.id = users.id

You should then process the result like this:

$users = [];
foreach($results as $row){
   if(isset($users[$row->userId]))
       $users[$row->userId][] = $row->imageUrl;

    else
        $users[$row->userId] = [$row->imageUrl];
}

$users is what you want.It should do the work.

like image 151
Parantap Parashar Avatar answered Sep 24 '22 21:09

Parantap Parashar


You need to use an array example below. the way your calling it now is as if it were just a variable so its only pulling 1.

$artist_img1=array();
$artist_img1 = $row['images']; 

$artist_img2=row2['id'];
echo "<img src=\"gallery/".$artist_img2."\" alt=\"\"></a>"; 
like image 20
Epic Avatar answered Sep 24 '22 21:09

Epic