tonyawelch Posted May 24, 2009 Share Posted May 24, 2009 I am creating a recipe script, and one of the (obvious) fields in the submission form is "Ingredients". I give instructions to the user to put each ingredient on a separate line, so the list goes into the database like so: 1 cup whatever 3 oz. whatever 4 tbsp whatever 8 whatever Pinch of whatever ...and so on. I'd like to be able to take that list and separate it into two columns so it would look like this: 1 cup whatever 3 oz. whatever 4 tbsp whatever 8 whatever Pinch of whatever I've thought of alternative ways to submit the data to the database to take care of this issue on the front end, but a single textarea submission seems the most efficient. So there is surely a way to do this on the printout? Thanks guys for all the help I've gotten here over the last few weeks Quote Link to comment https://forums.phpfreaks.com/topic/159426-display-listed-data/ Share on other sites More sharing options...
Michdd Posted May 24, 2009 Share Posted May 24, 2009 If they submit it with each ingredient on a separate line you could preform nl2br() on it which will insert line breaks (<br />) between the lines. Then you could use explode() to separate it into an array. Quote Link to comment https://forums.phpfreaks.com/topic/159426-display-listed-data/#findComment-840985 Share on other sites More sharing options...
RussellReal Posted May 24, 2009 Share Posted May 24, 2009 yes.. just explode by \n $ingredients = explode("\n",$row['ingredients']); for ($i = 0; $i < count($ingredients); $i++) { if (($i + 1) % 2) $rightSide[] = $ingredients[$i]; else $leftSide[] = $ingredients[$i] } print_r($leftSide); print_r($rightSide); Quote Link to comment https://forums.phpfreaks.com/topic/159426-display-listed-data/#findComment-840992 Share on other sites More sharing options...
tonyawelch Posted May 24, 2009 Author Share Posted May 24, 2009 OK, I'm having some kind of a brain lapse... The code worked correctly and the print_r printed out the array. But I tried to print out the values and got an 'invalid argument' error. Here is the code I used to print the leftside values... foreach ($leftSide as $key=>$value) { echo "$value<br>"; } Looking back at your code, I realized (or at least think I realized) that the arrays are not assigned to a name variable? Or I'm just an idiot. I know this is beginner PHP...I probably need sleep. Quote Link to comment https://forums.phpfreaks.com/topic/159426-display-listed-data/#findComment-841030 Share on other sites More sharing options...
RussellReal Posted May 24, 2009 Share Posted May 24, 2009 I'm thinking that foreach loop should work =\ Quote Link to comment https://forums.phpfreaks.com/topic/159426-display-listed-data/#findComment-841341 Share on other sites More sharing options...
tonyawelch Posted May 25, 2009 Author Share Posted May 25, 2009 Me too, but it doesn't ??? What else in the code can I show you to help me figure out why? Quote Link to comment https://forums.phpfreaks.com/topic/159426-display-listed-data/#findComment-841457 Share on other sites More sharing options...
RussellReal Posted May 25, 2009 Share Posted May 25, 2009 you have msn? add me: RussellonMSN@hotmail.com Quote Link to comment https://forums.phpfreaks.com/topic/159426-display-listed-data/#findComment-841540 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.