简体   繁体   English

Laravel自定义表单选择宏

[英]Laravel Custom Form Select Macro

Please help me with this custom select macro that I've got from this website . 请帮我使用我从此网站获得的自定义选择宏。 This code is working fine when you add the start value less that the end value. 当您添加的起始值小于最终值时,此代码可以正常工作。

Form::macro('selectRangeWithDefault', function($name, $start, $end, $selected = null, $default = null, $attributes = [])
{
  if ($default === null) {
    return Form::selectRange($name, $start, $end, $selected, $attributes);
  }
  $items = [];
  if (!in_array($default, $items)) {
    $items[''] = $default;
  }

  if($start > $end) {
    $interval = -1;
    $startValue = $end;
    $endValue = $start;
  }  else {
    $interval = 1;
    $startValue = $start;
    $endValue = $end;
  }

  for ($i=$startValue; $i<$endValue; $i+=$interval) {
    $items[$i . ""] = $i;
  }

  $items[$endValue] = $endValue;

  return Form::select($name, $items, isset($selected) ? $selected : $default, $attributes);
});

When adding greater than value from start value and less in the end value it breaks. 当从起始值开始增加一个值,而在结束值之间增加一个值时,它将中断。 Please help me fix this code guys 请帮我解决此代码的家伙

If your startvalue is bigger than your endvalue, you set 'start=end', and 'end=start'. 如果您的起始值大于最终值,则设置“ start = end”和“ end = start”。 If you want (need) to loop from the start to the endvalue, you need to increase every time with '1'. 如果要(需要)从起始值循环到最终值,则每次都需要增加“ 1”。 However, if you set the interval to '-1', and you begin from your startvalue (which is now smaller than the endvalue), your loop will never stop. 但是,如果将间隔设置为“ -1”,并且从起始值(现在小于结束值)开始,则循环将永远不会停止。

change 更改

if($start > $end) {  
$interval = -1;
$startValue = $end; 
$endValue = $start;

into 进入

if($start > $end) {
$interval = 1;
$startValue = $end;
$endValue = $start;

to make it work. 使它工作。

edit: explanation 编辑:解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM