Tuddae Posted June 29, 2010 Share Posted June 29, 2010 Okay.. this might be simpler done than I know of, but I'm gonna give a shot at it myself though. I have a database containing a table with information about members, for example one column could be favourite music artists and here I have information stored in this way: Kiss, Oasis, Etc, Etc. While another member might only have one favourite artist: Sting Now I need to build a menu list of all the favourite artists (with the use of the distinct method so that I don't get the same artist twice) In some way I need to split up a string with more than one artist so that the menu won't list all these artists in one line. All that I can think of is to integrate some kind of detection for the commas in the tables, but I'm not sure how to turn it into any useful code, any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/206159-splitting-up-a-string-from-a-mysql-database-to-make-a-html-form-menu/ Share on other sites More sharing options...
trq Posted June 29, 2010 Share Posted June 29, 2010 Each artist should be stored in a separate row within the database. This way you would simply execute a query such as.... SELECT artistname FROM userfavs WHERE user_id = 22 Quote Link to comment https://forums.phpfreaks.com/topic/206159-splitting-up-a-string-from-a-mysql-database-to-make-a-html-form-menu/#findComment-1078618 Share on other sites More sharing options...
Tuddae Posted June 29, 2010 Author Share Posted June 29, 2010 Alright... then the problem is the way I chose to store the names in the first place.. I did that by having them choose from a list of artists and then I used the following code before storing to Mysql: $artists = implode(", ", $_POST[Artists]); Any tips on how to store each with a seperate row? Quote Link to comment https://forums.phpfreaks.com/topic/206159-splitting-up-a-string-from-a-mysql-database-to-make-a-html-form-menu/#findComment-1078620 Share on other sites More sharing options...
trq Posted June 29, 2010 Share Posted June 29, 2010 Something like.... $artists = $_POST['Artists']; foreach ($artists as $artist) { $artist = mysql_real_escape_string($artist); $id = $_SESSION['id']; $sql = "INSERT INTO tbl (artist_id, user_id) VALUES ($artist, $id)"; mysql_query($sql); } Quote Link to comment https://forums.phpfreaks.com/topic/206159-splitting-up-a-string-from-a-mysql-database-to-make-a-html-form-menu/#findComment-1078641 Share on other sites More sharing options...
Tuddae Posted June 29, 2010 Author Share Posted June 29, 2010 doesn't work for me.. the problem is $_POST['Artists']; is an array and when storing it to sql in your way, it will only store one value of $_POST['Artists']; Quote Link to comment https://forums.phpfreaks.com/topic/206159-splitting-up-a-string-from-a-mysql-database-to-make-a-html-form-menu/#findComment-1078661 Share on other sites More sharing options...
kickstart Posted June 29, 2010 Share Posted June 29, 2010 Hi The foreach of $artists should cope with it being an array, so it should cope with multiple artists being passed in. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/206159-splitting-up-a-string-from-a-mysql-database-to-make-a-html-form-menu/#findComment-1078663 Share on other sites More sharing options...
Tuddae Posted June 29, 2010 Author Share Posted June 29, 2010 Ok.. Here is my code: if (!empty($_POST[Artists])) { $artists = $_POST[Artists]; foreach ($artists as $artists) $artists = mysql_real_escape_string($artists); } else { die(); } And then later I have my query with $artists to the database No matter how many artists I select it will only add the last one I select. Quote Link to comment https://forums.phpfreaks.com/topic/206159-splitting-up-a-string-from-a-mysql-database-to-make-a-html-form-menu/#findComment-1078669 Share on other sites More sharing options...
trq Posted June 29, 2010 Share Posted June 29, 2010 The query needs to go within the loop just like my previous post showed. Your foreach syntax is way off too, + you have removed the quotes around the index Artist within the $_POST array. Quote Link to comment https://forums.phpfreaks.com/topic/206159-splitting-up-a-string-from-a-mysql-database-to-make-a-html-form-menu/#findComment-1078680 Share on other sites More sharing options...
Tuddae Posted June 29, 2010 Author Share Posted June 29, 2010 I had a feeling about that.. Is there no way to seperate those things? Later in my coding I have one big INSERT command with a lot of vars, I was kind of hoping to keep this at one place only in the coding Quote Link to comment https://forums.phpfreaks.com/topic/206159-splitting-up-a-string-from-a-mysql-database-to-make-a-html-form-menu/#findComment-1078682 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.