php 如何在laravel刀片上的for循环中设置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38045911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:28:56  来源:igfitidea点击:

How to set value in for-loop on laravel blade

phplaraveldateselect

提问by Fan

<select id="year" name="year" class="form-control ">
    {{ $last= date('Y')-120 }}
    {{ $now = date('Y') }}

      @for ($i ={{ $now }}; $i <= {{ $last }}; $i--)
         <option value="{{ $i }}">{{ $i }}</option>
      @endfor               
</select>

And I got the error message Parse error: syntax error, unexpected '<'
It look like the variable can`t read.
How to set the value in the for-loop?

我收到错误消息Parse error: syntax error, unexpected '<'
它看起来像变量无法读取。
如何在for循环中设置值?

回答by Minhaj Mimo

Basically {{ $last= date('Y')-120 }}in this part you are showing the value but you need to assign the value. So assign like this :

基本上{{ $last= date('Y')-120 }}在这部分中,您正在显示值,但您需要分配值。所以像这样分配:

<?php $last= date('Y')-120; ?>

Same thing goes for the for loop too.Just compare the value. Do not put it in blade syntax.

同样的事情也适用于 for 循环。只需比较值。不要把它放在刀片语法中。

<select id="year" name="year" class="form-control ">
    {{ $last= date('Y')-120 }}
    {{ $now = date('Y') }}

    @for ($i = $now; $i >= $last; $i--)
        <option value="{{ $i }}">{{ $i }}</option>
    @endfor
</select>

回答by Osama Sayed

Change your view to:

将您的视图更改为:

<select id="year" name="year" class="form-control ">
    <?php $last= date('Y')-120; ?>
    <?php $now = date('Y'); ?>

    @for ($i = $now; $i <= $last; $i--)
        <option value="{{ $i }}">{{ $i }}</option>
    @endfor
</select>

回答by Jahid Mahmud

You can write

你可以写

<select id="year" name="year" class="form-control ">
    {{ $last= date('Y')-120 }}
    {{ $now = date('Y') }}

    @for ($i = $now ; $i <=  $last ; $i--)
    <option value="{{ $i }}">{{ $i }}</option>
    @endfor               
</select>

It will resolve your error.

它将解决您的错误。

回答by Akhilendra Singh

using "@for ($i = $now; $i <= $last; $i--)" didn't work for me so I had to use incrementing count.

使用 "@for ($i = $now; $i <= $last; $i--)" 对我不起作用,所以我不得不使用递增计数。

<div class="form-group">
  <label for="task" class="col-sm-1 control-label">Text</label>
     @for ($i = 0; $i < $count; $i++)
       <div class="col-sm-12">
           <input type="text" name="text[{{ $i }}]" id="text[{{ $i }}]" class="form-control">
      </div>
     @endfor
</div>

回答by Xerif

The best way to do this is to use range()and foreach()

最好的方法是使用range()foreach()

Example:

例子:

@foreach(range(date('Y')-5, date('Y')) as $y)
    {{$y}}
@endforeach

Result

结果

2014 2015 2016 2017 2018 2019 

Documentation:

文档:

回答by mizan3008

Hope this will help.

希望这会有所帮助。

<select id="year" name="year" class="form-control">
    {{-- */$last= date('Y')-120;/* --}}
    {{-- */$now = date('Y');/* --}}
    @for ($i = $now; $i <= $last; $i--)
    <option value="{{ $i }}">{{ $i }}</option>
    @endfor               
</select>

It will resolve you error, but would be nice if you pass your $last and $now variable from controller.

它会解决你的错误,但如果你从控制器传递 $last 和 $now 变量会很好。

Thanks

谢谢