johnW
johnW

Reputation: 329

Show the content of array properly in the view

I have a PaymentController that gets an array and returns that array "selectedTypes" to the registration.blade.php:

class PaymentController extends Controller
{
    public function storeQuantity(Request $request){
        $selectedTypes = $request->type;
        return view('posts.registration')->with('selectedTypes', $selectedTtypes);
    }
}

Now in the registration.blade.php file I want to show the ticket type name and the selected quantity but its not working properly. The code is:

<div class="card_body">
    <ul class="list-group list-group-flush">
        <li>
            <span>Ticket Type</span>
            <span>Quantity</span>
        </li>
        <?php var_dump($selectedTypes); ?>

        @foreach($selectedTypes as $selectedType)
            <li class="list-group-item list-group-registration d-flex align-items-center justify-content-between">
                <span class="font-size-sm">{{$selectedType[0]}}</span>
                <span class="font-size-sm">{{$selectedType[1]}}</span>
            </li>
        @endforeach
    </ul>
</div>

The issue is that its showing always "2". For example if the user selected 2 for the ticket type "center bench" and two for ticket type "lateral bench" its appearing:

 <div class="card_body">
    <ul class="list-group list-group-flush">
        <li>
            <span>Ticket Type</span>
            <span>Quantity</span>
        </li>
            <li>
                <span class="font-size-sm">2</span>
                <span class="font-size-sm">2</span>
            </li>
            <li>
                <span class="font-size-sm">2</span>
                <span class="font-size-sm">2</span>
            </li>
    </ul>
</div>

Instead of:

 <div class="card_body">
    <ul class="list-group list-group-flush">
        <li>
            <span>Ticket Type</span>
            <span>Quantity</span>
        </li>
            <li>
                <span class="font-size-sm">Center Bench</span>
                <span class="font-size-sm">2</span>
            </li>
             <li>
                <span class="font-size-sm">Lateral Bench</span>
                <span class="font-size-sm">2</span>
            </li>
    </ul>
</div>

The "<?php var_dump($selectedTypes); ?>" shows:

array(3) { ["a"]=> string(1) "2" ["center bench"]=> string(1) "2" ["lateral bench"]=> NULL }

Upvotes: 1

Views: 39

Answers (1)

Supun Praneeth
Supun Praneeth

Reputation: 3160

You are accessing array is incorrect. $selectedType is no an array. so when you access like this $selectedType[0] it get first character of the string, that mean its 2.

Try this:

<div class="card_body">
    <ul class="list-group list-group-flush">
        <li>
            <span>Ticket Type</span>
            <span>Quantity</span>
        </li>
        <?php var_dump($selectedTypes); ?>

        @foreach($selectedTypes as $k=>$selectedType)
            <li class="list-group-item list-group-registration d-flex align-items-center justify-content-between">
                <span class="font-size-sm">{{$k}}</span>
                <span class="font-size-sm">{{$selectedType}}</span>
            </li>
        @endforeach
    </ul>
</div>

Upvotes: 1

Related Questions