468,505 Members | 1,728 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

Cannot display multiple images per same id in one entry-Need some help

58
I need to dispaly a user's multiple images in one entry.Right now,say if a user uploads 3 images,three entries for the same id display to match 3 images.
I only want 1 entry to display all of a user's related images like below:
Id
First Name
Last Name
Title
Image1
Image2
Image3

Currently I get;
1
First Name
Last Name
Title
Image1

1
First Name
Last Name
Title
Image2

1
First Name
Last Name
Title
Image3

My select code is as follows:

[php]
include 'Con.inc.php';
$query = "SELECT Members.first_name,Members.last_name,Members.categ ory,Members.title,Members.message,Images.name,Imag es.type,Images.size,Images.path FROM Members LEFT JOIN Images ON Members.member_id=images.member_id";
$result = mysql_query($query);

while(list($first_name,$last_name,$category,$title ,$message,$file,$Type,$Size,$path)= mysql_fetch_row($result))
{
echo "first_name :$first_name <br>" .
"last_name :$last_name<br><br>".
"category :$category <br><br>".
"title :$title <br><br>".
"message:$message<br><br>".
"<img src='../FileDir/$path' alt='$first_name s profile photo' /><br>";
}

mysql_close($conn);
?>
[/php]

Thank you very much for your response in advance!
Nov 19 '07 #1
11 3514
MarkoKlacar
296 Expert 100+
Hi,

if you want the output you desire, you can't read/write all the information in one single loop. I'm guessing you need at least one more loop where you go though all the images that a user with the specific userid has linked to him/her.

Hope this helps.

Cheers
Nov 19 '07 #2
Jankie
58
Thank you MarkoKlacar,for your response !
I havent done this before so I have no clue how to add a conditioned second loop in my code above
Nov 19 '07 #3
MarkoKlacar
296 Expert 100+
Hi,

just to get an understanding of your database structure. The $path variable reads from the "Images.path" in the database. How is this connected to the user id?

As I see it, it's from .."Members.member_id=images.member_id", right?

Now what you could do is not select the images, but instead inside the while-loop you have execute one more query saying:

[PHP]$queryForMatching = "SELECT Images.part FROM Images WHERE Images.member_id = '$firstName'";[/PHP]

In the case above I'm assuming that the $firstName is the id used to map a user with his uploaded images.

After this you loop though the result that you receive and print out all the pictures.

Hope this helps.

Cheers
Nov 20 '07 #4
Jankie
58
thanks for your help

"As I see it, it's from .."Members.member_id=images.member_id", right?"

Yes.

member_id is what links a user to their images,not $firstname (I just missed it above)
Nov 21 '07 #5
MarkoKlacar
296 Expert 100+
Hi Jankie,

did you solve your problem, was I clear enough in my previous post?

Cheers
Nov 21 '07 #6
Jankie
58
Sorry for not updating you
Not yet really,do you mean a nested loop ?
If i understood you correctly,I have to delete the image selection info from the select statement(then I dont need the join statement,too) .Instead,I should include your query code inside the outer loop,correct ?
or how does your code fit in my code above ?
Sorry to sound dumb,didnt do it before
and thank you very much for your follow up ! Appreciate it.
Nov 21 '07 #7
MarkoKlacar
296 Expert 100+
Hi Jankie,

Yes I mean a nested loop. The nested loop goes through a new result array that was received from a new query where you select all the image.ids bellonging to a key that you're currently working with in the outer loop.

Hope I didn't confuse you with my mediocre description.

Let me know.
Nov 21 '07 #8
Jankie
58
MarkoKlacar
Thank you very much indeed
No,no your description is fine
yes,I got it to work with a nested loop inside the second query.
But I had to remove the join code along with image selection info from first query.

they are both linked now but just a where clause after I have stripped the table name part from the columns in the query.
[php]
"SELECT first_name,last_name,category,title,message FROM Members WERE member_id=member_id";
[/php]
[php]
"Select name,type,size,path FROM Images WERE member_id=member_id";

[/php]

I am just concerned the 2 tables are not now technically linked


Again,I do appreciate your staying with me,thanks a lot !
Nov 21 '07 #9
MarkoKlacar
296 Expert 100+
Hi Jankie,

they are linked using the primary keys, member id in this case.

Cheers
Nov 21 '07 #10
andho
34
I am just concerned the 2 tables are not now technically linked
it is technically linked from the php code right :D

and you dont need the WHERE clause in your first query now so they will be something like this:

Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT member_id, first_name,last_name,category,title,message FROM Members WERE member_id=member_id";
NOTE: that member_id is in the Select Column list.

now for the loop you should take the member_id like so:
Expand|Select|Wrap|Line Numbers
  1. while (list($member_id, $first_name, ....) {
and within the loop:

Expand|Select|Wrap|Line Numbers
  1. $sql = "Select name,type,size,path FROM Images WERE member_id=" . $member_id;
Nov 21 '07 #11
MarkoKlacar
296 Expert 100+
Hi,

sure a statement like that would be useless. But you have a table for storing users and you have a table for storing the images that a user has uploaded, right?

Even dough the id "is" the same the are the used as the primary keys in order to be able to match them.

So when execuring the nested query you need to store what user you are interested in and matching that one against the one in the images table.

By the way have you had any progress since last time?

Cheers
Nov 22 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by kingofkolt | last post: by
26 posts views Thread by Martin Jørgensen | last post: by
9 posts views Thread by Confused but working on it | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.