Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP : Formatting multiple arrays for database (laravel)
    primarykey
    data
    text
    <p>I've got this input form: (Using the blade template engine with Laravel, but the html should be easy to understand from this and ultimately trivial)</p> <pre><code>{{ Form::text('amount[]', Input::old('amount')) }} &lt;?php echo Form::select('unit[]', array( 'whole' =&gt; _('whole'), 'ml' =&gt; _('milliliter'), 'l' =&gt; _('liter'), 'dl' =&gt; _('deciliter'), 'mg' =&gt; _('milligram'), 'g' =&gt; _('gram'), 'kg' =&gt; _('kilogram'), 'tsp' =&gt; _('teaspoon'), 'tbs' =&gt; _('tablespoon'), )) ?&gt; {{ Form::text('ingredient[]', Input::old('ingredient')) }} </code></pre> <p>I'm trying to format this to my database to return it in a string like this :</p> <pre><code>&lt;li&gt;&lt;span&gt;1&lt;/span&gt; liter red wine&lt;/li&gt; </code></pre> <p>I'm considering making it a simpler form and eliminating the unit measurement forcing my users to type it in instead for flexibility, but I'll still have to cramp it all into one table for my database. The span tag is used in a jQuery to dynamically increase the number so is needed. I've been at this for quite a few days on and off but I can't crack how to do this. </p> <p>Here is my formatting logic:</p> <pre><code>$amount = Input::get('amount[]'); $unit = Input::get('unit[]'); $ingredient = Input::get('ingredient[]'); for ( $i = 0, $c = count(Input::get('ingredient[]')); $i &lt; $c; $i++ ) { $ingredients .= '&lt;li&gt;&lt;span&gt;'.$amount[$i].'&lt;/span&gt;'.$unit[$i].' '.$ingredient[$i].'&lt;/li&gt;'; } </code></pre> <p>and I send it using</p> <pre><code>$new = Recipe::create(array( 'title' =&gt; Input::get('title'), 'curiousity' =&gt; Input::get('curiousity'), 'ingredients' =&gt; $ingredients, 'steps' =&gt; Input::get('recipe') )); </code></pre> <p>I've tried numerous ways and I get errors like the $ingredients array not being defined or not being able to use [] in the variable. I tried defining the variable as an '$ingredients = '';' variable but that just produced an empty string. My problem must be in the logic.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload