Laravel PHP:按字母顺序在下拉列表中显示数组元素,而不使用sort()

Laravel PHP:按字母顺序在下拉列表中显示数组元素,而不使用sort()

问题描述:

I have a Laravel PHP Photo Gallery application that allows the user to create an album and then insert photos into an album of their choice from a drop down list. This requires the user to create the album first since the drop down list is dynamic based on which albums actually exist. The drop down list is currently functioning properly but it does not display albums (array elements) alphabetically.

I've tried using the 'sort()' method but the problem is that the array element ids then become switched/re-sorted when doing so. I do not want the array ids to be re-sorted since this will put photos into the wrong albums.

So I want to know if there is a way to display the albums alphabetically in a drop down list while not re-sorting their array element ids.

Controller:

public function create()
{
    $stoneArray = $this->stone->all()->toArray();

    if (empty($stoneArray)) {
        $dropdown[0] = 'There are no stones';
    }

    foreach ($stoneArray as $stone) {
        $dropdown[$stone['stone_id']] = $stone['stone_name'];
        // sort($dropdown); /* This re-sorted the array element ids */

    }

    $data = array('type' => 'stone_photo', 'dropdown' => $dropdown);
    $this->layout->content = \View::make('forms.stones.new-photo', $data);
}

View:

<div class="form-group">
    {{ Form::label('stone_id', 'Stone: ') }}
    {{ Form::select('stone_id', $dropdown, array('class' => 'form-control')) }}
</div>

Any help is greatly appreciated! Thank you!

我有一个Laravel PHP照片库应用程序,允许用户创建一个专辑,然后将照片插入到一个专辑中 他们从下拉列表中选择。 这要求用户首先创建专辑,因为下拉列表是动态的,基于哪些专辑实际存在。 下拉列表当前正常运行,但不按字母顺序显示专辑(数组元素)。

p>

我尝试过使用'sort()' strong>方法,但问题是数组元素ids会变成切换/重新 这样做时分类。 我不希望对数组ID进行重新排序,因为这会将照片放入错误的相册中。

p>

所以我想知道是否有办法在下拉列表中按字母顺序显示专辑,而不是重新排序他们的数组元素ID。 p >

控制器: strong> p>

  public function create()
 {
 $ stoneArray = $ this-&gt; stone  - &gt; all() - &gt; toArray(); 
 
 if(empty($ stoneArray)){
 $ dropdown [0] ='没有宝石'; 
} 
 
 foreach(  $ stoneArray as $ stone){
 $ dropdown [$ stone ['stone_id']] = $ stone ['stone_name']; 
 // sort($ dropdown);  / *这重新排序了数组元素ids * / 
 
} 
 
 $ data = array('type'=&gt;'stone_photo','dropdown'=&gt; $ dropdown); 
 $ this  - &gt; layout-&gt; content = \ View :: make('forms.stones.new-photo',$ data); 
} 
  code>  pre> 
 
 

查看: strong> p>

 &lt; div class =“form-group”&gt; 
 {{Form :: label('stone_id','Stone:  '}}} 
 {{Form :: select('stone_id',$ dropdown,array('class'=&gt;'form-control'))}} 
&lt; / div&gt; 
  code>  
 
 

非常感谢任何帮助! 谢谢! p> div>

EDIT

I'm making some assumptions here about your file structure, but it should give you the idea.

In: app\Acmeepositories\Eloquent\EloquentStoneRepository.php or whatever you called it, there's a function:

public function all()
{
  return Stone::all();
}

(I think that's what it looks like, but I'm not positive). Try changing that to:

public function all()
{
  return Stone::orderBy('column_name', 'asc')->get();
}

If you use all() somewhere else and don't want it sorted, consider adding another function:

public function all_sorted()
{
  return Stone::orderBy('column_name', 'asc')->get();
}

Hope that helps!

Edit: Changed ->all() to ->get() so eloquent can properly sort it.