MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old October 10th, 2003, 02:17 PM
mjlivelyjr mjlivelyjr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Idaho, US
Posts: 9 mjlivelyjr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
relations via PHP or MySQL

I am curious as to what everybodies thoughts on the following problem are.

I have two tables in a database. One of them holds information about the site's members. The other table holds several pictures with associated captions in another table. The table structure is similar to this:

member_table ------
PK_ID -(primary key)
memberName
memberEmail
(other personal info)

image_table -----
PK_ID -(primary key)
memberID (foreign key linking to the member table)
image
caption

Now using data on that table I need to generate a dynamic page listing all site members and showing thumbnails of each member's image. I can think of two obvious ways to do this.

+ 1st way
Query the database for all of the members. Then loop through each member and query the database for each member's images.

+ 2nd way
Query the database for all of the members and in that query do a JOIN on the image table so the query contains both members and their images. Then loop through the results and extract all the member information into an object or an array also add all of the image data to the object/array as member array.

The difference between these two methods is basically do you put the load of building the member object (complete with images) on the SQL server (1st way) or on the php engine (2nd way). The first way results in far cleaner code in my experience, however it scales horribly. I was hoping that you all could give me some good suggestions that I haven't thought of yet..

Reply With Quote
  #2  
Old October 10th, 2003, 03:03 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
I'd do the second one. I don't know how you figure the first makes for cleaner code, as it involves a couple of foreach loops. For the second, all you do is the one loop, and you make your query longer by 15 or 20 characters, plus it scales.

And for that matter, if you've got a one-to-one relationship here, why do you even have the image table broken out? If each member has exactly one image and one caption, and those images and captions can't overlap with those of other members, then I'd think you'd just put the image and caption in the member table.

If what you intend is for there to be a menu of images for members to choose from, and multiple members can have the same image, then you should have the images table broken out, but the foreign key should be in the members table and should point to the primary key in the images table corresponding to the chosen image. In this case, there should be no member_id in the images table.

Reply With Quote
  #3  
Old October 10th, 2003, 04:21 PM
mjlivelyjr mjlivelyjr is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Idaho, US
Posts: 9 mjlivelyjr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
There is actually a 1 to many relationship.

I have tended to go the second solution myself most of the time as well. The problem I run into (more of an annoyance) is this.

Due to the one to many nature of the relationship between the members and the images a query with a join results in the following

Code:
member.name, member.email, image.name, image.caption
---------------------------------------------------------------------------
melissa, melissa@test.com, picture1.jpg, caption1
melissa, melissa@test.com, picture2.jpg, caption2
melissa, melissa@test.com, picture3.jpg, caption3
mike, mike@test.com, picture4.jpg, caption4
mike, mike@test.com, picture5.jpg, caption5


The problem being you can't really do a simple foreach() or while() all the time with this because of the duplicated member data (name and e-mail). At this point what I usually do is loop through all of the record and as soon as I see the primary key for the member table change I save the specific member data into an array and then until it changes it again I only save the image data to another child array. An example (pseudo-code) is:

PHP Code:
 $memberArray = array();
$previousMemberKey 0;
while (
$memberRecord mysql_fetch_assoc($memberResult)) {
  if (
$memberRecord['PK_ID'] != $previousMemberKey) {
    
//save the old member (if exists)
    
if (is_array($tempMemberArray)) {
      
$memberArray[] = $tempMemberArray;
    }
     
//reinitialize the temp array
    
$tempMemberArray = array();
    
//set new member data
    
$tempMemberArray['name'] = $memberRecord['member.name'];
    
$tempMemberArray['email'] = $memberRecord['email'];
    
//Create an array to place image info in
     
$tempMemberArray['image'] = array();
 }
  
//now add the image
  
$tempMemberArray['image'][] = array(
    
'name' => $memberRecord['image.name'],
    
'caption' => $memberRecord['image.caption']
  );
}
//If $tempMemberArray is still an array it will need to be added
if (is_array($tempMemberArray)) {
  
$memberArray[] = $tempMemberArray;



Now this example it's not too bad...but now imagine a time when you need fields from four or five related tables and each of the relations is a 1-to-many relationship. This method could become very difficult. I am sure there is another, better way to do this. Which is where I need help.

Just to re-iterate however, the table relationship in my example is a one to many table where one member may have several images. It is not a many-to-many relationship. However I think the same issues would still apply to a many-to-many relationship.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > relations via PHP or MySQL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway