2

I hope the title explains well, but i am having trouble, a) finding any reference to what i am looking to do, and b) figuring out the logic in my head, so all input, even if simply pointing me in the direction of the correct syntax, would be a great help.

my aim is this - each user i have has the ability to upload 10 photos. i want to show each users photos within a preset template, with all users "template" showing up in a list on one page, ie ill have user ones ten photos in the template, then immediately below ill have user twos, and so on down the list.

i have already designed the template, and i am able to populate it based on the current logged in user id, but i am having difficulty making it appear for EVERY user, so that every user can see each others photos.

i have tried:

$everyid = array($user_id) foreach ($user_id as $value) {include 'template.php';}

but this doesnt work so it would seem to be very wrong. in short, i guess i want to pick up each user_id in the database, check if they have photos uploaded (apply other parameters, but i wont go into that here since i should be able to sort that myself after), and then output their own templated area to a certain page if its relevant.

please bear in mind that i have all the photos path's entered to the mysql table in the relevant user's column, so there may be an easier way to autogenerate this content that im overlooking, so any suggestions of how to do this are welcome.

thanks.

EDIT: can provide template code and the like if necessary, but theyre long winded and would expand the page a great deal.

EDIT EDIT: as requested, here is how my able is set up (in simplified form):

user_id     username    image1               image2..... and so on.
    1           her    eduejdksk.jpg      werfwer9342.jpg
    2           him    234234234.jpg          null 

here is my sample code:

$user_id = $_SESSION['user_id'];
$queryone = mysql_query("SELECT `image1` FROM `users` WHERE `user_id` = $user_id");
$resone = mysql_fetch_array($queryone);
$valueone = $resone['image1'];

<div id="imageone">
<?php
  if($valueone != '') {
  echo '<img src="uploads/', $user_id, '/thumbsbig/', $valueone, '">';
  } else { ;}

  ?>
</div>

which enables me to, successfully, assuming "her" is logged in, recall image1 for "her" (eduejdksk.jpg) into a template based on the logged on user. what it doesnt allow me to do, is show said file in one template, and then 234234234.jpg in another immediately below.

as you can see it uses the session user id to pick the photos from the db. what i want to happen is for $user_id to equal 1 then 2 then 3 and so on...for the complete database of users. i am assuming its some sort of while or foreach loop, but as per above my efforts have, as of yet, been fruitless.

thanks.

Grimbonie
  • 23
  • 5
  • If you're seeking a sql query solution to your problem then consider to provide table schema, sample data and desired output. It may improve your chances of getting an answer big time. – peterm Aug 03 '13 at 22:31

1 Answers1

1

It seems you have created 10 fields for the images in the user table. This is not very manageable. You'd better create a seperate table for the images.

id  |   user_id   |   path_to_image
----+-------------+--------------------------------
1   |   1         |   some/image.jpg
2   |   1         |   some/other/image.jpg
3   |   1         |   some/other/image2.jpg
4   |   2         |   some/other/users/image.jpg
.... etc ....

As you can see, any user can now have zero to infinite number of images. If you truly wish to limit the number of images to 10, add a check if SELECT COUNT(*) FROM images WHERE user_id=$user_id is smaller than 10 before upload.

Querying this table for images of all users is now easy:

SELECT * FROM images

If you wish to get all images from a specific user:

SELECT * FROM images WHERE user_id=$user_id

For the internal database relations I suggest you read about InnoDB's foreign key relationship

Oh an most important: mysql_* functions are depracated, recommend you use mysqli or PDO instead, please see this question for more information.

Community
  • 1
  • 1
AmazingDreams
  • 3,136
  • 2
  • 22
  • 32
  • Glad I could be of help, I remember my 'eureka' moments like these all too well :) Good luck! – AmazingDreams Aug 04 '13 at 12:06
  • heh, ive spent the past hour converting to your suggestion of a seperate table - it definitely runs a great deal smoother. ive gone from 5 files worth of code to a simple 20 lines. on the mysql point - once i get it up ill go back and convert it all to mysqli. i think this makes it possible to autogen my content now too. would also upvote your answer, but it wont let me. thanks! – Grimbonie Aug 04 '13 at 12:10
  • 1
    ah, yeah. sorry, didnt know how this place worked. sorted now though. – Grimbonie Aug 04 '13 at 12:16