Monomoni
Monomoni

Reputation: 435

PHP If Statement Argument in Laravel

I have a calculation to make in which the variable $totalPercent might be a 0. For that case, I added some if statements to the script to only run since the initial returned with an error on division by zero on some occasions.

This was the newly revised calculation, which now works when $totalPercent is a 0, but when it has a value, and the supposed script is called, it returned me an error of Undefined variable: designPercent.

Here is the new script with the added if statement that was meant to do calculation only if $totalPercent is more than 0.

@if ($totalnumerical > '0')
    $totalPercent= '100' / $totalnumerical;
    $stylePercent= ($styletempt *  $totalPercent).'%';
    $designPercent= ($designtempt * $totalPercent).'%';
    $managePercent= ($managetempt * $totalPercent).'%';
    <div class="graphSection">
        <div class="skillGraph">
            <span style="width:{{$designPercent}}" class="bar-1"></span>
            <span style="width:{{$stylePercent}}" class="bar-2"></span>
            <span style="width:{{$managePercent}}" class="bar-3"></span>
        </div><br>
        <div class="skillGraph graph_text">
            @if(($designtempt * $totalPercent) > 15)
                <span style="width:{{$designPercent}}" >Design</span>
            @else
                <span class="hovertext" style="width:{{$designPercent}}; margin-top:-57px" >Design</span>
                <span style="width:{{$designPercent}}" ></span>
            @endif
            @if(($styletempt * $totalPercent) > 15)
                <span style="width:{{$stylePercent}}" >Interior Styling</span>
            @else
                <span class="hovertext" style="width:{{$stylePercent}}; margin-top:-67px;" >Interior Styling</span>
                <span style="width:{{$stylePercent}}" ></span>
            @endif
            <span style="width:{{$managePercent}}" >Project Management</span>
        </div>
    </div>
@endif

Upvotes: 0

Views: 98

Answers (1)

Jerodev
Jerodev

Reputation: 33186

Everything in a blade view is interpreted as text, not code. If you want to add php code in your view, you need to put this code in @php blocks.

@if ($totalnumerical > '0')
    @php
        $totalPercent= '100' / $totalnumerical;
        $stylePercent= ($styletempt *  $totalPercent).'%';
        $designPercent= ($designtempt * $totalPercent).'%';
        $managePercent= ($managetempt * $totalPercent).'%';
    @endphp
    <div class="graphSection">
        <div class="skillGraph">
            <span style="width:{{$designPercent}}" class="bar-1"></span>
            <span style="width:{{$stylePercent}}" class="bar-2"></span>
            <span style="width:{{$managePercent}}" class="bar-3"></span>
        </div><br>
        <div class="skillGraph graph_text">
            @if(($designtempt * $totalPercent) > 15)
                <span style="width:{{$designPercent}}" >Design</span>
            @else
                <span class="hovertext" style="width:{{$designPercent}}; margin-top:-57px" >Design</span>
                <span style="width:{{$designPercent}}" ></span>
            @endif
            @if(($styletempt * $totalPercent) > 15)
                <span style="width:{{$stylePercent}}" >Interior Styling</span>
            @else
                <span class="hovertext" style="width:{{$stylePercent}}; margin-top:-67px;" >Interior Styling</span>
                <span style="width:{{$stylePercent}}" ></span>
            @endif
            <span style="width:{{$managePercent}}" >Project Management</span>
        </div>
    </div>
@endif

Upvotes: 1

Related Questions