0

Hello Guys, I have a form that i used array name.

ei. name[]

this is my code for input form

{{ Form::open(array('url' => 'create')) }}

        <TR>
            <TD><INPUT type="checkbox" name="chk[]"/></TD>

            <TD class="rating">
            {{ Form::text('core_values[]', Input::old('core_values[]'), array('placeholder' => 'Criteria Here')) }}</TD>
            <TD>
            {{ Form::textarea('behavioral[]', Input::old('behavioral[]'), array('placeholder' => 'Description Here', 'cols' => '75', 'rows' => '4')) }}
            </TD>
            <td class="rating">
            {{ Form::text('rating[]', Input::old('rating[]'), array('placeholder' => 'Percentage')) }}</td>
        </TR>
    </TABLE>

    </br>
    {{ Form::submit('Submit!', array('class' => 'pull-right btn-warning')) }}
    {{ Form::close() }}

and this is my controler code

class SaveController extends BaseController {

    public function saveData()
    {
            $input = Input::all();
            for ($idx = 0; $idx < count(Input::get('core_values')); $idx++)
            {
                $values = new question;
                $values->core_values = $input['core_values'][$idx];
                $values->behavioral = $input['behavioral'][$idx];
                $values->rating = $input['rating'][$idx];
                $values->save();
            }
            // redirect
            Session::flash('message', 'Added!');
            return Redirect::to('evaluation');


    }   


}

my problem is, when i add 2 rows into database, only 1 rows added. whats wrong with my codes? iam not good in laravel. help me guys, thanks in advance. your help appreciated

3
Contributors
2
Replies
29
Views
3 Years
Discussion Span
Last Post by veedeoo
0

I don't know laravel, but I think I would need to take a look at the method save() in the question class to see what it's doing with those values. Although, since you are in a loop, saving one item should lead to saving another. You might also want to var_dump($values); to see what you have in there.

I don't know if I'm even close to the answer, I don't know Laravel as I mentioned and I haven't even used a MVC framework before, so keep that in mind when reading my posts here.

0

I already uninstalled the laravel on my Development stack, because I am moving on to the Fuel PHP. However, I am worried about the for loop on your script. Will it be possible for you to use foreach loop which is more fitting to your situation? I do understand if you are coming from JAVA, C, and other languages.

In some cases, for loop will give you an error of ' undefined index ' on the last count position on the count function. To investigate it for yourself, please run this.. ( I don't have the chance to test it, but it should point out the points I am trying to convey).

<?php

    $array = array('a','b','c','d','e','f','g');
        for($y=0; $y <= count($array) ;$y++){
         echo $array[$y].'['.$y.']<br/>';
  }

While this one is appropriate and more acceptable in PHP

<?php

   $array = array('a','b','c','d','e','f','g');
       $y = 0;
       foreach($array as $x){
         echo $x.'['.$y.'] <br/>';
            $y++;
        }

To capture the array from the loop above, you will need to reassign the array something like this..

$thisArray[] = $x;
}//end of foreach loop

return $thisArray; // or do whatever you need to do with it.

Edited by veedeoo: info added.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.