1

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)

{{ Form::text('amount[]', Input::old('amount')) }}
<?php echo Form::select('unit[]',
    array(
    'whole' => _('whole'),
    'ml'    => _('milliliter'),
    'l'     => _('liter'),
    'dl'    => _('deciliter'),
    'mg'    => _('milligram'),
    'g'     => _('gram'),
    'kg'    => _('kilogram'),
    'tsp'   => _('teaspoon'),
    'tbs'   => _('tablespoon'),
    )) ?>
{{ Form::text('ingredient[]', Input::old('ingredient')) }}

I'm trying to format this to my database to return it in a string like this :

<li><span>1</span> liter red wine</li>

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.

Here is my formatting logic:

$amount = Input::get('amount[]');
$unit = Input::get('unit[]');
$ingredient = Input::get('ingredient[]');

for ( $i = 0, $c = count(Input::get('ingredient[]')); $i < $c; $i++ ) 
{
    $ingredients .= '<li><span>'.$amount[$i].'</span>'.$unit[$i].' '.$ingredient[$i].'</li>';
}

and I send it using

$new = Recipe::create(array(
    'title' => Input::get('title'),
    'curiousity' => Input::get('curiousity'),
    'ingredients' => $ingredients,
    'steps' => Input::get('recipe')
));

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.

1
  • I'm going to go in a different direction, I'll make a new database linked by recipe ID with one column per field. It'll make my ingredients tags. Thanks all the same, guys! I'll leave this question here in case someone has a similar problem. Commented Nov 20, 2012 at 16:44

1 Answer 1

3

Build your select list outside the form for brevity as well as (what I do anyway to keep controllers very slim) send the input to the model all at once.

$input = Input::all();
$new = Recipe::create($input);

Build the array for the ingredients elsewhere. In the model (perhaps?):

$ingredients = array(
'dbname1' => 'displayname1',
'dbname2' => 'displayname2'
);

And display it accordingly, then the form inputs should be sent over with the $input in an array that you can parse and then save to your db.

Other notes about Blade syntax. I'm not aware of a need to define the array brackets [].

{{Form::open()}}
{{Form::label('label1','Display Label 1')}}
{{Form::text('fieldname1',Input::old('fieldname1'))}}

With your ingredients array already built (your current syntax will produce a dropdown and I assume you want checkboxes)

{{Form::select('ingredientsFIELDNAME',$ingredients)}}
{{Form::close()}}

In your Input::all() array your ingredientsFIELDNAME field name will have an array if you've built it as checkbox instead of select. Hope this all makes sense.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.