Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/159426-display-listed-data/
Share on other sites

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);

Link to comment
https://forums.phpfreaks.com/topic/159426-display-listed-data/#findComment-840992
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/159426-display-listed-data/#findComment-841030
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.