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条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路