Skip to main content
Known Participant
October 16, 2010
Answered

Linking Image to User in seperate table

  • October 16, 2010
  • 1 reply
  • 2857 views

I am building a dynamic Image gallery where users can upload images to their profile for customers to view. The images are stored in a file with the path to the image stored in a different table than the details for the user. So far I have managed to do the image upload and link the user ID in the Images table to the ID in the details table so that all the images associated with that user is linked to his record in the details table. My problem comes in when joining these two tables for one recordset. I have managed to join them, only that each record with more than one image associated with it displays more than once. So if user 1 uploaded two images, his record comes up twice in the results. Any suggestions as to how I can force the query to display every record only once no matter how many images that user uploaded?

Hope this makes sense. Thanks

Reandré

This topic has been closed for replies.
Correct answer David_Powers

So in that case I will have to create a page for each establishment manually?

I just had a look at my facebook profile to try and learn something from this. My url for my facebook profile looks like this:

http://www.facebook.com/profile.php?id=762039663

While my images URL look like this:

http://www.facebook.com/profile.php?id=762039663&v=photos

I don't want to copy facebook's methods, but that only proves that there is a simpler way to do this. Do you maybe have any idea how I can do that?

Sorry I'm not very clued up with these things as you have probably noticed. lol. But I appreciate your help and patience very much.

Thanks


reandre68 wrote:

I don't want to copy facebook's methods, but that only proves that there is a simpler way to do this.

What makes you think that the back end of Facebook is simple? What makes a site like Facebook successful is making the front end simple for users, while hiding the complexity behind the scenes.

One of the difficulties of providing help in a forum like this is that there's no way of knowing the other person's level of knowledge or how far down the road they have gone with a particular strategy. It sounds as though you're just beginning with PHP and database-driven sites. It's a challenging and often frustrating time. I remember the problems I had trying to get my head around the best way to structure a database. What makes it difficult is there's no single "right" way to do it. Each database is different. You might develop a database that works just fine; but when you decide to add extra functionality, you discover the structure is too rigid. That happened to me with a major project I worked on several years ago.

Some of the best advice I received about working with databases was to spend a lot of time planning not only what you want the database to do now, but also what you might want it to do in future. The coding takes only a small proportion the total time spent on designing the project. A book that helped me greatly was "Database Design for Mere Mortals". The author's writing style is a bit tedious, but the information is rock solid.

Good luck with your redesign.

1 reply

Participating Frequently
October 17, 2010

>Hope this makes sense.

It really doesn't make sense, at least with the info you've provided so far. If you join tables with a one-to-many relationship, then you are going to get duplicated results from the user table.

>how I can force the query to display

>every record only once no matter how

>many images that user uploaded

You need to tell us more. If you are joining users to images, then I assume you must want image data in the recordset - right? If you only want one record for each user, then which image do you want?

reandre68Author
Known Participant
October 17, 2010


If you join tables with a one-to-many relationship, then you are going to get duplicated results from the user table.

So there is no way to get only one result per user?

Sorry about not making sense. I'm not sure how to put it or exactly what information you will need, but let me give it a shot:

I have two tables- 1.Details and 2.images

The details table is self-explanatory. A user can log in and  upload images to his gallery with the filename/path stored in the database - in the images table. The images table has 3 columns, picID, userID and filepath. The user ID is the ID of the user that uploaded the images so all the images with his user ID is all the images he uploaded and I want to display all those images accordingly with each user's ID. When a client does a search, I get more than one result per user at this time, because there is more than one image per user.

What I want to do, is to create a recordset where all the fields are present, including image data, but without displaying every record twice because there is more than one image linked to that user. I don't want any one specific image, I want all the images available for that user. I have created a recordset joining the 2 tables like this. eg:

SELECT details.id, details.name, images.`path`

FROM details, images

WHERE images.usrID = details.id;

Something like that. SELECT DISTINCT won't work obviously since every row is different because of the image path.

Hope this makes better sense. Please let me know if there is something specific you need. If there is a better way to do this that you know of, please let me know.

David_Powers
Inspiring
October 18, 2010

reandre68 wrote:

I don't want any one specific image, I want all the images available for that user.

When looping through the results, you need to suppress the display of duplicate names, but continue to display the images. You do this by creating a variable to store the previous name, and comparing the value with the current name. Display the name only if it's different from the previous one.

You start by initializing the variable as an empty string outside the repeat region, and assigning the current name to it each time the repeat region loop runs. It looks like this:

<?php
// initialize a variable to store the previous name
$previous = '';
do { ?>
<tr>
   <td><?php if ($row_rsName['name'] != $previous) {
     echo $row_rsName['name'];
     // store the current name in the $previous variable
     $previous = $row_rsName['name'];
} else {
  echo ' ';
} ?></td>
    <td><img src="<?php echo $row_rsName['path']; ?>"></td>
  </tr>
<?php } while ($row_rsName = mysql_fetch_assoc($rsName)); ?>