Jump to content

Display Listed Data


tonyawelch

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.