douba4824 2017-08-06 09:32
浏览 96
已采纳

创建数组php和输出选项值

I try to build a function which should create an array. This array I want to put out in my view. This is the function, Im not sure how to build the array.

 public function getSpielplan(){
        //$newdata =  array (

        $spieltagSpiel = Spielplan::where('Spieltag', '=', 1)->get();
            foreach($spieltagSpiel as $spieltagSpielOutput){

                $heimName = Verein::where('V_ID', '=', $spieltagSpielOutput->Heimmannschaft)->get();
                    foreach($heimName as $heimNameOutput){

                        $gastName = Verein::where('V_ID', '=', $heimNameOutput->Gastmannschaft)->get();
                            foreach($gastName as $gastNameOutput){

                                //array ($spieltagSpielOutput->Spielplan_ID, $heimNameOutput->Name, $gastNameOutput->Name)
                            }
                    }
            }
        //);
        //return view('spielplan')->with('alleSpiele', $newdata);
    }

In my view based on blade from laravel, this will be my output

div class="col-xs-6">
                label for="">Spielauswahl/label>
                select class="form-control input-sm" name="spiele" id="spiele">

                @foreach($alleSpiele as $alleSpieleOutput)
    
                    option value="{!! HERE MUST BE SPIELPLAN_ID [array0?]!!}">{{HERE MUST BE NAME [array1?] }}/option>
    
                @endforeach
                /select>
            /div>

In the value must be Spielplan_ID, I think must be the first column of the array [0]? And in the option array I need Name array [1]. What I have to change that this will work?

  • 写回答

2条回答 默认 最新

  • dongshi6528 2017-08-06 11:04
    关注

    I guess u need Spielplan_ID as option value, and option name should be combined with both heimName and gastName right? I consider that V_ID is the primary key of Verein model. If I am right, then follow these codes.

    public function getSpielplan(){
    
      $spieltagSpiel = Spielplan::where('Spieltag', '=', 1)->get();
    
      foreach($spieltagSpiel as $spieltagSpielOutput){
    
       $heimName=Verein:: where('V_ID','=',$spieltagSpielOutput->Heimmannschaft)->first();
    
       $gastName = Verein:: where('V_ID', '=', $heimNameOutput->Gastmannschaft)->first();
    
       $resultData[$spieltagSpielOutput->Spielplan_ID] =  $heimName->Name. $gastName->Name;
    
      }
    
       return view('spielplan')->with('alleSpiele', $resultData);
    
    }
    

    blade view should like this,

     @foreach( $alleSpiele as $alleSpieleKey => $alleSpieleName )
       <option value="{{ $alleSpieleKey }}">
       {{ $alleSpieleName }}
       </option>
     @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统