Connecting Tech Pros Worldwide Forums | Help | Site Map

I want to know how I can do this using PHP & MySQL

Newbie
 
Join Date: Jul 2008
Posts: 21
#1: Jul 31 '08
I want to add tags for a paricular image in my db table.
I want these tags to be stored in one table field.
lets say I entered the following tags for an image:

house
mountainside
lake
greenery

I want these tags to be saved in 1 field named Tags separated by a delimiter.

house:mountainside:lake:greenery

the above is wat shld be stored in my database.

can someone pls tell me how to do this?

like using somethin opposite as the explode() in php.

thanks in advance!!!

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jul 31 '08

re: I want to know how I can do this using PHP & MySQL


The opposite of explode is implode()
Newbie
 
Join Date: Jul 2008
Posts: 21
#3: Jul 31 '08

re: I want to know how I can do this using PHP & MySQL


sorry guys i guess thats ike really simple. i used implode
Newbie
 
Join Date: Jul 2008
Posts: 21
#4: Jul 31 '08

re: I want to know how I can do this using PHP & MySQL


thanks.....................................
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Jul 31 '08

re: I want to know how I can do this using PHP & MySQL


Quote:

Originally Posted by nse111

sorry guys i guess thats ike really simple. i used implode

No problem. Just remember to refuse to write any code if you don't have the manual with you.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#6: Jul 31 '08

re: I want to know how I can do this using PHP & MySQL


I should mention that you really shouldn't store more than one piece of data in a single field, like you do with your tag field.
(Check out Database normalization and Table structure for details on that).

Each single piece of data should be placed in it's own field, which in your case, would require a new table to be created.

Consider this table layout. It's not perfect but it's a big improvement:
Expand|Select|Wrap|Line Numbers
  1. Image
  2. ----------
  3. ImageID Int (PK)
  4. ImageName Char(Unique)
  5. Etc...
  6. ----------
  7.  
  8. ImageTag
  9. ----------
  10. TagID Int (PK)
  11. ImageID Int (FK)(Unique)
  12. TagName Char (Unique)
  13. ----------
  14.  
In the second table the ImageID and TagName fields would share a Unique key, making it impossible to insert the same tag twice for the same image.

This may seem unnecessary extra trouble, but you should always try to normalize your database. It makes it much easier to work with, especially if it ever needs to be extended or altered.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#7: Aug 1 '08

re: I want to know how I can do this using PHP & MySQL


Quote:

Originally Posted by Atli

I should mention that you really shouldn't store more than one piece of data in a single field, like you do with your tag field.
(Check out Database normalization and Table structure for details on that).

Each single piece of data should be placed in it's own field, which in your case, would require a new table to be created.

Consider this table layout. It's not perfect but it's a big improvement:

Expand|Select|Wrap|Line Numbers
  1. Image
  2. ----------
  3. ImageID Int (PK)
  4. ImageName Char(Unique)
  5. Etc...
  6. ----------
  7.  
  8. ImageTag
  9. ----------
  10. TagID Int (PK)
  11. ImageID Int (FK)(Unique)
  12. TagName Char (Unique)
  13. ----------
  14.  
In the second table the ImageID and TagName fields would share a Unique key, making it impossible to insert the same tag twice for the same image.

This may seem unnecessary extra trouble, but you should always try to normalize your database. It makes it much easier to work with, especially if it ever needs to be extended or altered.

I could go either way, actually. If images had an average of 5 tags, your image table would be five times larger.

when searching, you could always use the "LIKE" clause to search for something like "%:mountain:%", in the tag field.

As for "searching" to eliminate duplicates. The tag field will be an array of only several fields, array searching would be fast.

Just some thoughts, It depends on the use and your particular needs,



Dan
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,748
#8: Aug 1 '08

re: I want to know how I can do this using PHP & MySQL


Quote:

Originally Posted by dlite922

I could go either way, actually. If images had an average of 5 tags, your image table would be five times larger.

when searching, you could always use the "LIKE" clause to search for something like "%:mountain:%", in the tag field.

As for "searching" to eliminate duplicates. The tag field will be an array of only several fields, array searching would be fast.

Just some thoughts, It depends on the use and your particular needs,



Dan

Using the LIKE clause to search through a large database will most likely take a very long time. Searching through a long string will usually take much longer than simply matching two short strings. Not to mention that searching will render indexes pretty much useless (although I'm not 100% sure on that).

But putting that aside, normalization is less about ease of use and more about maintaining data integrity, reducing the chance of data corruption and other abnormalities, making the database easy to maintain and extend. (among other things.)

Like with the field we are discussing in this thread, it would be extremely easy to corrupt the data or insert duplicates. MySQL considers it a single text field, which leaves it completely in the hands of the API to maintain. The developer of each application that is to use this data must develop their own code to maintain it, which is just silly considering you already have an open connection to a highly refined database engine that specializes in such tasks.
Newbie
 
Join Date: Jul 2008
Posts: 21
#9: Aug 5 '08

re: I want to know how I can do this using PHP & MySQL


Hmmm really interesting facts guys.. thanks so much I will really consider them! thanks again
Reply