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

laravel_beginner's avatar

php while loop combined with html

I have issue. I have some code and with php while loop some section of html code. After that section I am putting php tag with ending }. But the while loop continues to the html code below } as well. The first thing that I thought is that it doesn't match the while loop { specifically and for some reason it ends if, but that is not the case. Break would be possible to use only if I could use variable, but break is with non-integer operant no longer suppported.

<?php
    if($empty==false){
        $i = 1;
        $total_price = 0;
        while ($i <= $length )
            $all_from_row = Product::where('id', $values[$key+($i-1)])->get();
            $i++;
            $total_price += $all_from_row[0]["price"];

            ?>
<section class="h-100 h-custom" style="background-color: #eee;">
    <div class="container py-5 h-100">
        <div class="row d-flex justify-content-center align-items-center h-100">
            <div class="col">
                <div class="card">
                    <div class="card-body p-4">

                        <div class="row">

                            <div class="col-lg-7">
                                <h5 class="mb-3"><a href="{{ route('check') }}" class="text-body"><i
                                            class="fas fa-long-arrow-alt-left me-2"></i>Continue shopping</a></h5>
                                <hr>

                                <div class="d-flex justify-content-between align-items-center mb-4">
                                    <div>
                                        <p class="mb-1">Shopping cart</p>
                                        <p class="mb-0">You have {{$length}} items in your cart</p>
                                    </div>
                                    <div>
                                        <p class="mb-0"><span class="text-muted">Sort by:</span> <a href="#!"
                                                                                                    class="text-body">price <i class="fas fa-angle-down mt-1"></i></a></p>
                                    </div>
                                </div>

                                <div class="card mb-3">
                                    <div class="card-body">
                                        <div class="d-flex justify-content-between">
                                            <div class="d-flex flex-row align-items-center">
                                                <div>
                                                    <img
                                                        src="{{ asset("images/" . $all_from_row[0]["image"]) }}"
                                                        class="img-fluid rounded-3" alt="Shopping item" style="width: 65px;">
                                                </div>
                                                <div class="ms-3">
                                                    <h5>{{ $all_from_row[0]["name"] }}
                                                        {{ $all_from_row[0]["model"] }}</h5>
                                                    <p class="small mb-0"></p>
                                                </div>
                                            </div>
                                            <div class="d-flex flex-row align-items-center">
                                                <div style="width: 50px;">
                                                    <h5 class="fw-normal mb-0">1</h5>
                                                </div>
                                                <div style="width: 80px;">
                                                    <h5 class="mb-0">{{ $all_from_row[0]["price"] }} Kč</h5>
                                                </div>
                                                <a href="/add_cart/{{$all_from_row[0]["id"]}}" style="color: #cecece;"><i class="fas fa-trash-alt"></i></a>
                                            </div>
                                        </div>
                                    </div>
                                </div>


                                    <?php
                                }

                                ?>
                            </div>
                            <div class="col-lg-5">
							.
							.
							.

Any idea where is the issue?

0 likes
5 replies
Snapey's avatar

putting a database query inside a loop isn't a good idea. You should prepare the data in the controller and oass it to the view

1 like
Shivamyadav's avatar

@Snapey That a great answer by @snapey sir , you have to placed your code at a right place ,as in future update you can find it simply in the controller all your database query and change it , looks good for other developer to understand it..

laravel_beginner's avatar

@Snapey I did that

<?php
    if($empty==false){
        while ($i <= $length ) {
            $total_price += $all_from_row[$i]["price"];
            $i++;
            ?>

But still I don't see any other option than to use the while loop and that lead to the same issue I had at the beginning.

jlrdw's avatar

@laravel_beginner I suggest taking the Laravel from scratch free training it will answer many of these questions.

Please or to participate in this conversation.