【问题标题】:PHP Number Increment undefinedPHP数字增量未定义
【发布时间】:2021-09-25 07:26:10
【问题描述】:

我有下面的代码,它在countingNumbers 变量上返回未定义。我需要实现类名编号,这样我就可以在 JavaScript 中循环并给它们单独的“阅读更多”标签。

<?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

【问题讨论】:

  • 您确定要在刀片文件中添加&lt;?php 标签吗?这不是违背了 Laravel 的全部目的吗?
  • 变量$countingNumbers在函数custom_echo()中未定义
  • Laravel Blade 有 The Loop Variable 你可以使用
  • @Shutt 你必须学习很多关于 Laravel 的知识,因为你将纯 PHP 与 Blade 混合在一起,你正在做的事情是行不通的,多阅读文档并寻找更多教程,这样您就可以更好地学习,并且不会像这样轻易停止。

标签: javascript php mysql laravel


【解决方案1】:
  1. 正如@N69S 所指出的,$countingNumbers 不在custom_echo() 的范围内。为此,您可以通过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. 如果您的数组有数字、增量索引,您可以这样做(如果没有,您可以使用循环变量,正如@brombeer 指出的那样):
<?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. 您还可以像 @AlbertoSinigaglia 指出的那样摆脱剩余的 PHP 代码块。

你的看法:

@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

【讨论】:

  • use 构造用于未命名函数的匿名函数
  • 感谢您的提示。无论如何,这两种选择都应该有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-05
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 2011-09-10
相关资源
最近更新 更多