Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

duckz1209's avatar

Radio Button loop

can someone help me on how to do loop with radio button.. this is my code and the result is 5 radio button displayed per row but only one is being chosen on all rows.. here is the code

@foreach($books as $book)

                                    <tr>
                                        <td align="justify"><h3 class="semi-bold">{{$book->name}}: {{$book->description}}</h3></td>

                                        <td width="20%"></td>
                                       
                     @for($i=1; $i <= 5; $i++)
                                            
                                        <td width="4%"><input type="radio" name="$i" value="$i" /><label>{{ $i }}</label></td>

                                                @endfor
                                    </tr>
                                    @endforeach
0 likes
3 replies
Tray2's avatar

Not really sure what it is you are trying to accomplish but when it comes to radio buttons only one can be selected in each group.

duckz1209's avatar

ive got it.. :)

@foreach($books as $book )

                                    <tr>

                                        <td align="justify"><h3 class="semi-bold">{{$book ->name}}: {{$book ->description}}</h3></td>

                                        <td width="20%"></td>
                                        

                                        <td width="4%">{{ Form::radio($book ->id, 1, false, ['class' => 'icheck']) }}1</td>
                                        <td width="4%">{{ Form::radio($book ->id, 2, false, ['class' => 'icheck']) }}2</td>
                                        <td width="4%">{{ Form::radio($book ->id, 3, false, ['class' => 'icheck']) }}3</td>
                                        <td width="4%">{{ Form::radio($book ->id, 4, false, ['class' => 'icheck']) }}4</td>
                                        <td width="4%">{{ Form::radio($book ->id, 5, false, ['class' => 'icheck']) }}5</td>
                                    </tr>

                                    @endforeach
bobbybouwmann's avatar
Level 88

So I think the name of the element should be string in the first place. Right now you have the id of the item in there while I would expect something like this

Form::radio('book[' . $book->id . ']', 1, false, ['check' => 'icheck'])

The rest of the code looks pretty good to me!

1 like

Please or to participate in this conversation.