jeudi 17 mars 2016

PHP/ Laravel: How to Give An If Statement a Dynamic Condition ?

Within the my Quotation module, Products can be grouped under a variety of different categories. Categories can also be created and deleted. The code below works fine but only because I have hardcoded the categories. You will see that if category_id = 1, then show products belonging to "cars". If it is "2", then show "motorcycles".

This is completely inflexible. I want to be able to dynamically create a new category, and have that new category presented on my form. So what can I do, if anything, to allow the dynamic creation or deletion of a category ?

I am using Laravel, however, a PHP answer is just as good.

The code is a Laravel View but I think it is pretty obvious what is going on:

        @foreach($quotation as $quote)
            @if($quote->category_id == 1)
                <tr>
                <td>{!! Form::select('product_id[0]',$products['cars'], $quote->name, ['class'=>'product_id']) !!}</td>
                <td>{!!Form::text('quantity',$quote->quantity) !!}</td>
                <td id="price">{{$quote->price}}</td>
                <td id="cost">{{$quote->cost}}</td>
                </tr>
            @endif


            @if($quote->category_id == 4)
                <tr>
                <td>{!! Form::select('product_id[0]',$products['motorcycle'], $quote->name, ['class'=>'product_id']) !!}</td>
                <td>{!!Form::text('quantity',$quote->quantity) !!}</td>
                <td id="price">{{$quote->price}}</td>
                <td id="cost">{{$quote->cost}}</td>
                </tr>
            @endif
        @endforeach



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire