-1

I have the code below and it's returning undefined on the countingNumbers variable. I need to implement the class name numbered so I can loop over in JavaScript and give them separate "Read More" tags.

<?php $countingNumbers = 0 ?>

            <?php 
            
            function custom_echo($x, $length){

                if(strlen($x)<=$length)
                    {
                        echo $x;
                    }
                    else
                    {   
                        $y=substr($x,0,$length) . '...';
                        echo $y;
                        echo '<div class="service-about__link' . $countingNumbers . '">
                                READ MORE
                              </div>';
                    }
                } 

            ?>

            @foreach($services as $service)
            <?php $countingNumbers++ ?>
            
            <div class="service-about movement{{$countingNumbers}} row row-margin">
                <div class="col-lg-6">
                    <h3 class="service-about__title">
                        {!!$service->title!!}
                    </h3>
                    
                    <div class="service-about__content-small">
                        <?php 
                        custom_echo($service->content, 200); ?>
                    </div>
                    <div class="service-about__content-large">
                        {!!$service->content!!}
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="service-about__image">
                        <img src="{{url('')}}/img/services/{{$service->image}}">
                    </div>
                </div>
            @endforeach
Shutt
  • 31
  • 2
  • 2
    are you sure you want to add ` – Alberto Sinigaglia Jul 16 '21 at 13:31
  • 2
    the variable `$countingNumbers` is undefined inside the function `custom_echo()` – N69S Jul 16 '21 at 13:35
  • 1
    Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – N69S Jul 16 '21 at 13:37
  • 1
    Laravel Blade has [The Loop Variable](https://laravel.com/docs/8.x/blade#the-loop-variable) you could use – brombeer Jul 16 '21 at 13:48
  • @Shutt you have to learn a lot about Laravel, as you are mixing up pure PHP with Blade, you are doing stuff that is not going to work, read the documentation a little more and look for more tutorials so you can learn better and don't get stopped really easy like this. – matiaslauriti Jul 16 '21 at 16:00

1 Answers1

-1
  1. As @N69S pointed out, $countingNumbers is not in custom_echo()'s scope. To make that happen, you can pass it in via use():
<?php $countingNumbers = 0 ?>

            <?php 
            
            function custom_echo($x, $length) use($countingNumbers) {

                if(strlen($x)<=$length)
                    {
                        echo $x;
                    }
                    else
                    {   
                        $y=substr($x,0,$length) . '...';
                        echo $y;
                        echo '<div class="service-about__link' . $countingNumbers . '">
                                READ MORE
                              </div>';
                    }
                } 

            ?>

            @foreach($services as $service)
            <?php $countingNumbers++ ?>
            
            <div class="service-about movement{{$countingNumbers}} row row-margin">
                <div class="col-lg-6">
                    <h3 class="service-about__title">
                        {!!$service->title!!}
                    </h3>
                    
                    <div class="service-about__content-small">
                        <?php 
                        custom_echo($service->content, 200); ?>
                    </div>
                    <div class="service-about__content-large">
                        {!!$service->content!!}
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="service-about__image">
                        <img src="{{url('')}}/img/services/{{$service->image}}">
                    </div>
                </div>
            @endforeach

  1. If your array has numeric, incremental indexes, you can do this (if not, you can use the loop variable as @brombeer pointed out):
<?php 

function custom_echo($index, $x, $length){

    if(strlen($x)<=$length)
        {
            echo $x;
        }
        else
        {   
            $y=substr($x,0,$length) . '...';
            echo $y;
            echo '<div class="service-about__link' . $index . '">
                    READ MORE
                    </div>';
        }
    } 

?>

@foreach($services as $index => $service)

<div class="service-about movement{{$index}} row row-margin">
    <div class="col-lg-6">
        <h3 class="service-about__title">
            {!!$service->title!!}
        </h3>
        
        <div class="service-about__content-small">
            <?php 
            custom_echo($index, $service->content, 200); ?>
        </div>
        <div class="service-about__content-large">
            {!!$service->content!!}
        </div>
    </div>
    <div class="col-md-6">
        <div class="service-about__image">
            <img src="{{url('')}}/img/services/{{$service->image}}">
        </div>
    </div>
@endforeach

  1. You could also get rid of the remaining PHP code block as @AlbertoSinigaglia pointed out.

your view:

@foreach($services as $index => $service)

<div class="service-about movement{{$index}} row row-margin">
    <div class="col-lg-6">
        <h3 class="service-about__title">
            {!!$service->title!!}
        </h3>
        
        <div class="service-about__content-small">
            @include('custom_echo', [
                'index' => $index,
                'x' => $service->content,
                'length' => 200
            ])
        </div>
        <div class="service-about__content-large">
            {!!$service->content!!}
        </div>
    </div>
    <div class="col-md-6">
        <div class="service-about__image">
            <img src="{{url('')}}/img/services/{{$service->image}}">
        </div>
    </div>
@endforeach

custom_echo.blade.php

@if (strlen($x)<=$length)
    {{ $x }}
@else
    {{ substr($x,0,$length) }}&hellip;
    <div class="service-about__link{{ $index }}">
                READ MORE
    </div>
@endif
shaedrich
  • 5,457
  • 3
  • 26
  • 42