 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
01-25-2008, 06:26 AM
|
#1 (permalink)
|
|
The Acquainted
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
|
Adding data - more than one piece in a column?
Hi All
I was just wondering what the best way of doing this would be.
Say I had a database of Actors (each one gets an id as the primary key in the actors table: So would be Actor_id, First Name, Last Name, etc).
Then say I had a page of info (say a movie info page) and I wanted to list the cast on that page. Would I simply have a column called 'actors', then just put the actor_ids in there seperated by commas?
Eg:
Actors: 124,366,697,125,123
But how would I retrieve their names on the page when it is shows to users? I guess I need the select query to know that the comma acts as a marker of sorts?
For a real life example take a look at this page: Play.com (UK) : Spider-Man 1, 2 And 3 (Blu-ray) : Blu-ray - Free Delivery towards the bottom under technical details it lists the actors.
I know it's probably a really simple silly nube question! (Sorry!) Appreciate your help!
__________________
PS3 Forums on GameSlurp - the site for gaming fans!
|
|
|
|
01-25-2008, 06:59 AM
|
#2 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
To display the values from the MySQL rows from the columns you would do:
PHP Code:
while ($row = mysql_fetch_array($query) ) { echo $row['actorfirstname']; echo $row['actorlastname']; }
Also the id column would be set to null considering it increments whenever there are new values put inside the other columns, so when you put a new actor's first and last name in, and submit the form inputs
It will put them into the database along with an id knowing already that it increments +1.
You could get the $_GET global for use of the ID column doing:
PHP Code:
$id = $row['actor_id']; $_GET['actid'] = $id;
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
|
The Following User Says Thank You to Orc For This Useful Post:
|
|
01-25-2008, 07:22 AM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
|
Hi Orc - thanks for the reply.
I probably didn't explain myself properly, but I was specifically referring the page where I want to list the actors associated with that movie. So for eg, on the link I posted above, (once I already have the database of actors set-up), how do I add them to movies they are in? And then how do I get mysql to know how to 'decode' the actor_ids?
EG:
Actors Column/field on Movie Page: 124,366,697,125,123
These are added to the movie page when the page is being created. So the person writing the page will simply enter the actor_ids of all the actors in that movie.
I need to know how to transform all those ids that are seperated by a comma into individually ids that I can feed into mysql via a query.
(Am I making any more sense?)
__________________
PS3 Forums on GameSlurp - the site for gaming fans!
|
|
|
|
01-25-2008, 07:23 AM
|
#4 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Brook
Hi Orc - thanks for the reply.
I probably didn't explain myself properly, but I was specifically referring the page where I want to list the actors associated with that movie. So for eg, on the link I posted above, (once I already have the database of actors set-up), how do I add them to movies they are in? And then how do I get mysql to know how to 'decode' the actor_ids?
EG:
Actors Column/field on Movie Page: 124,366,697,125,123
These are added to the movie page when the page is being created. So the person writing the page will simply enter the actor_ids of all the actors in that movie.
I need to know how to transform all those ids that are seperated by a comma into individually ids that I can feed into mysql via a query.
(Am I making any more sense?)
|
Hmm, I just started learning more MySQL, so I don't really know how to tell you this :[ Sorry..
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
|
The Following User Says Thank You to Orc For This Useful Post:
|
|
01-25-2008, 10:48 AM
|
#5 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
You will want to use the explode function to seperate the commas. I can't explain exactly how it works, because I don't know.
But the SQL later would look like:
PHP Code:
$aActors = array(); foreach($aIds as $iId) { $sql = "SELECT * FROM `actors` WHERE `id` = '".$iId."' LIMIT 1"; $query = mysql_query($sql);
if($query) { $aActors[] = mysql_fetch_array($query); } else { echo 'Could not fetch data for actor id: ' .$iId; } }
foreach($aActors as $aActor => $aInfo) { echo $aInfo['First Name']; }
That would fetch all the data of the actors in the array of numbers.
That's where explode comes in, which I'm sure that someone else can explain.
Then it places all the data in a multi-dimensional array.
Code:
$array = array(
'0' => array('id' => 3, 'first name' => 'Jessica', 'last name' => 'Alba'),
'1' => array('id' => 4, 'first name' => 'Arnold', 'last name' => 'Schwarzenegger')
);
And then we foreach that array, and echo out the names :)
|
|
|
|
|
The Following User Says Thank You to Tanax For This Useful Post:
|
|
01-25-2008, 11:03 AM
|
#6 (permalink)
|
|
The Prestige
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
|
PHP Code:
$seperated = explode(',', '124,366,697,125,123');
gives you an array of
Code:
Array
(
[0] => 124
[1] => 366
[2] => 697
[3] => 125
[4] => 123
)
explode, explodes an array at a given identifier, in this case ',' and essentially splits it up into an array
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
|
|
|
|
|
The Following User Says Thank You to sketchMedia For This Useful Post:
|
|
01-25-2008, 11:12 AM
|
#7 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Yah, so in your case, you would want to do:
PHP Code:
$aSep = explode(',', $aQuery['actors']);
$aActors = array(); foreach($aSep as $iId) { $sql = "SELECT * FROM `actors` WHERE `id` = '".$iId."' LIMIT 1"; $query = mysql_query($sql);
if($query) { $aActors[] = mysql_fetch_array($query); } else { echo 'Could not fetch data for actor id: ' .$iId; } }
foreach($aActors as $aActor => $aInfo) { echo $aInfo['First Name']; }
Where $aQuery is an array of the movie info..
|
|
|
|
|
The Following User Says Thank You to Tanax For This Useful Post:
|
|
01-25-2008, 01:03 PM
|
#8 (permalink)
|
|
The Acquainted
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
|
Why not use a relations table to hook up actors with a movie.
Lets say you have your actor table with:
ID | firstname | lastname | info
And your movie table:
ID | title | info
And your relations table where the actors get related to a movie.
ID | movie_ID | actor_ID
so for every actor you would want to link to a movie you just make a row entry in the relation table containing the "movie_ID" of the movie and the "actor_ID" of the actor who should be related to that movie.
And ofcourse you would have to construct a mechanism that would take the movie ID when the movie details page is called and use that movie_ID to query the relation table for all the rows containg a matching movie_ID.
Then take all the actor_ID:s that would be the outcome from that query and then list the actors getting the individual actors Info from the actor_ID itself while listing them.
Did that sound complicated ?, well at first i would say it sounds scary. but i think this approach would bring more flexibility to the handling of information.
Thats my opinion anyway.
Good Luck to ya.
/EyeDentify
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
|
|
|
|
|
The Following User Says Thank You to EyeDentify For This Useful Post:
|
|
01-25-2008, 01:58 PM
|
#9 (permalink)
|
|
how quixotic are you?
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
|
EyeDentify's idea makes the most sense. You should have different tables for the main movie information and actor information.
|
|
|
|
|
The Following User Says Thank You to ETbyrne For This Useful Post:
|
|
01-25-2008, 04:03 PM
|
#10 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
It is never a good idea to store data like that. In this instance, you will want to make a relational table, Evy was spot on with how to do it.
|
|
|
|
|
The Following User Says Thank You to Village Idiot For This Useful Post:
|
|
01-25-2008, 08:12 PM
|
#11 (permalink)
|
|
The Acquainted
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
|
Thanks all!
Yes the relations table makes sense (I don't think I have seen it mentioned before).
I guess I can still have an input field where the person creating the page puts the actor ids seperated by comma and then use the $seperated = explode(',', '124,366,697,125,123'); to create an array which is then used to enter the actor ids into the relations table (?).
Is there any time when I should skip the relations table and just use the 'explode' method?
What if I simply want to add 'related actors' to a news item or a feature - would the relations table be overkill?
__________________
PS3 Forums on GameSlurp - the site for gaming fans!
|
|
|
|
01-25-2008, 08:22 PM
|
#12 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by Brook
Thanks all!
Yes the relations table makes sense (I don't think I have seen it mentioned before).
I guess I can still have an input field where the person creating the page puts the actor ids seperated by comma and then use the $seperated = explode(',', '124,366,697,125,123'); to create an array which is then used to enter the actor ids into the relations table (?).
Is there any time when I should skip the relations table and just use the 'explode' method?
What if I simply want to add 'related actors' to a news item or a feature - would the relations table be overkill?
|
There is never a good time to store data like that. Its simply bad database design.
|
|
|
|
01-25-2008, 10:15 PM
|
#13 (permalink)
|
|
The Acquainted
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
|
Thanks VI.
Although it does make things harder for me - as I was simply going to use an articles hack and put the actor_ids in a field for each article.
I am going to have to think of a way to add my own relational table in there now.
Is it really bad if i go with the original way? It would be much easier for me... *runs!
__________________
PS3 Forums on GameSlurp - the site for gaming fans!
|
|
|
|
01-25-2008, 11:40 PM
|
#14 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
It will take some getting used to, but once you get used to that setup you will love it.
|
|
|
|
|
The Following User Says Thank You to Village Idiot For This Useful Post:
|
|
01-27-2008, 06:56 PM
|
#15 (permalink)
|
|
The Acquainted
Join Date: Jul 2005
Location: UK
Posts: 121
Thanks: 41
|
Would it be really bad if I did it my original way?
I'm not sure I can do it the other way yet tbh. (I'll be using an articles hack where I was going to simply add the actor_ids into a field. Most of the time it will only be one id being entered (only used movies/actors as an example)).
__________________
PS3 Forums on GameSlurp - the site for gaming fans!
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|