aQ Posted May 22, 2007 Share Posted May 22, 2007 Hi again! I have this value in a table in my database called wardrobe: Socks|Pants|Cap I use explode to cut it into pieces. explode("|", $wardrobe); Then, I want to print all the pieces from "wardrobe". I do not want to do this manually, because each user has different amount of stuff in his wardrobe. Am I able to create a loop for these? Example (this doesn't work) while($wardrobe) print $wardrobe[$number]; } Thank you for a great forum! Quote Link to comment https://forums.phpfreaks.com/topic/52526-explode-loop-from-mysql/ Share on other sites More sharing options...
chigley Posted May 22, 2007 Share Posted May 22, 2007 <?php $q = mysql_query("SELECT wardrobe FROM table WHERE user = '$username'"); echo "<pre>"; while($row = mysql_fetch_assoc($q)) { $bits = explode("|", $row["wardrobe"]); foreach($bits as $item) { echo "$item\n"; } } echo "<pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52526-explode-loop-from-mysql/#findComment-259183 Share on other sites More sharing options...
aQ Posted May 22, 2007 Author Share Posted May 22, 2007 Well. It prints all the items in wardrobe, BUT it prints it hundreds of times. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/52526-explode-loop-from-mysql/#findComment-259188 Share on other sites More sharing options...
chigley Posted May 22, 2007 Share Posted May 22, 2007 Probably because there is more than one entry in the database? Quote Link to comment https://forums.phpfreaks.com/topic/52526-explode-loop-from-mysql/#findComment-259193 Share on other sites More sharing options...
aQ Posted May 22, 2007 Author Share Posted May 22, 2007 The wardrobe-field in the users table has info|info|and some other info Quote Link to comment https://forums.phpfreaks.com/topic/52526-explode-loop-from-mysql/#findComment-259211 Share on other sites More sharing options...
chigley Posted May 22, 2007 Share Posted May 22, 2007 Yes but if there are duplicate rows in the database, more than one will be outputed. So if this is your database: Table: table +---------+ |wardrobe | +---------+ |value1 | |value2 | +---------+ Then both values will be outputted! Quote Link to comment https://forums.phpfreaks.com/topic/52526-explode-loop-from-mysql/#findComment-259216 Share on other sites More sharing options...
marcus Posted May 22, 2007 Share Posted May 22, 2007 How many "wardrobe" pieces are actually being printed out, and how many do you actually have it in your field. Quote Link to comment https://forums.phpfreaks.com/topic/52526-explode-loop-from-mysql/#findComment-259219 Share on other sites More sharing options...
aQ Posted May 22, 2007 Author Share Posted May 22, 2007 There is two pieces. with a | in between. That is where I want it to explode. Quote Link to comment https://forums.phpfreaks.com/topic/52526-explode-loop-from-mysql/#findComment-259296 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.