[eluser]IamPrototype[/eluser]
Hello.
I want my users be able to pick/write down their favorite movies in a input seperated with a comma. My problem is I don't know how to insert the movies into my database (I know how to insert data). The thing is, how should the table values look like? Right now I'm testing without a database and this is some code I just came up with. Feel free to suggest changes!
Code: $fav_movies = 'In Hell-Southpark-American Pie 1-Jackass';
$exp_movies = explode('-', $fav_movies);
echo implode(', ', $exp_movies);
$fav_movies should be the result of my query. I simple don't know if that would be a good way on how to do it. Ah, it's hard for me to explain, sorry.
Let's keep it simple: I'm in a need of script where my guests can pick their favorite movies, cars - whatever! I haven't used explode and implode for a long time, so I'm just practicing, you know. Any kind of help on how it should look like when I insert my movies and output them would be nice, thanks!
[eluser]Mischievous[/eluser]
I would use a 2 table setup in your database and not run with a commas separated field.
For example:
Users_table: id, name
Movies_table: id, user_id, movie
Then on the input form just grab the movies list... run your
Code: $moviesArr = explode(",",$_POST['movies']);
Then just do a while or for loop to run through the array and insert each movie into the database corresponding with the user_id.
Code: for($i = 0; $i <= count($moviesArr)-1; $i++)
{
$query = "INTO movies (user_id, movie) VALUES ('$user', '$moviesArr[$i]')";
$queryresult = mysql_query($query);
if($queryresult){
echo($moviesArr[$i]."Movie added to database, thanks for your submission");
}else{
echo("There was a problem adding ".$moviesArr[$i]." into the database");
}
}
[eluser]IamPrototype[/eluser]
So the input value would look like this: "In Hell, Simpsons, South Park". But if the user enters a comma without writing a movie like this: "In Hell, Simpsons, South Park, " it would insert a blank movie or cause an error?
PS: I just thought that putting the movies into a single line would be a lot better.. but now I realize if I would do some statistics on how many users like "South Park" it would be a lot easier to do it the way you're doing it, thanks.
[eluser]Mischievous[/eluser]
yeah, that's the whole reason for doing it the way I did it.... also for if there in a blank movie it would just insert a blank movie... to make this not happen just run an if statement
Code: if($moviesArr[$i] != ""){
run query
}
[eluser]IamPrototype[/eluser]
I see, but if the user writes "Film 1, Film 2, Film 3," without a space in the very end or "Film 1, Film 2, Film 3,,,,,," with a lot commas, it would insert "Film 3," or "Film 3,,,,," to the database and that wouldn't be nice either. You see?
[eluser]garymardell[/eluser]
Code: $movies = "hello, world,foo, bar,,,,";
$trimmed = rtrim($movies, ",");
$array = explode(",", $trimmed);
[eluser]TheFuzzy0ne[/eluser]
Why do the movies even need to be tokenised at all? I'm sorry, but I don't get it. If it's really that important, you can give them a textarea instead of a text input, and a new line will be the delimiter (since most titles don't have one in it ).
[eluser]slowgary[/eluser]
There's the problem with doing it like that. The only way I can see to get around using individual text inputs is to use a textarea and require each movie to be on it's own line. Otherwise, any character you use as a delimiter is eventually going to end up as part of a movie title and your site will be broken.
EDIT: That's the last straw Fuzzy! Take off the skates, it's on!! ;-P
[eluser]Dam1an[/eluser]
[quote author="slowgary" date="1245454399"]
There's the problem with doing it like that. The only way I can see to get around using individual text inputs is to use a textarea and require each movie to be on it's own line. Otherwise, any character you use as a delimiter is eventually going to end up as part of a movie title and your site will be broken.
EDIT: That's the last straw Fuzzy! Take off the skates, it's on!! ;-P[/quote]
True, although I'm sure there's a film called 10 (and if not, it's just a matter of time)
I definatly think new lines is the way to go
That or spaces, no movie title will ever have one of those
Edit: HAHA
[eluser]IamPrototype[/eluser]
Ah! New lines! But how would I be able to spot new lines? Just type in \n instead of my comma?
|