0
votes

I'm working on a reporting page using php Laravel framework. I select the options from the comboboxes, these values are sent to the controller to replace them in some queries used to display the reports.

When I click the submit button, the page refreshes and the name shown is the last element of the dropdown.

My Controller:

$negocioDive = DB::table('ma_leads_reporte')
                      ->selectRaw('NEGOCIO, year(FEC_CREACION) as AÑO')
                      ->groupBy('NEGOCIO', 'AÑO')
                      ->get()->toArray();

        $selectFecha = DB::table('param_fecha')
                      ->selectRaw('mesNum, mesNom')
                      ->groupBy('mesNum', 'mesNom')
                      ->get()->toArray();

        $año = $request ->input('año');
        $mes = $request -> input('mes');
        $negocio = $request -> input('negocio');

        //Solicitud de Cotización versus mes anterior
        $mesAnt = DB::table('ma_leads_reporte')
                    ->selectRaw('count(*) as C, day(FEC_CREACION) AS DIA, monthname(FEC_CREACION) AS MES')
                    ->whereMonth('date_identified', '=', $mes-1)
                    ->whereYear('date_identified', '=', $año)
                    ->groupBy('MES', 'DIA')
                    ->get()->toArray();
        $vscont = array_column($mesAnt, 'C');
        $antMes = array_column($mesAnt, 'MES');
        $dia = array_column($mesAnt, 'DIA');

My View:

<div class="container">
<div class="row justify-content-center">
<div class="content mt-3">
<div class="animated fadeIn">
<div class="col-lg-8">
   <div class="card">
      <div class="card-header">
         <form action="{{route('reportes')}}" method="GET" id="fecha">
            {{Form::label('', 'Año')}}
            <select id="año" name="año">
               @foreach($negocioDive as 
               $negocioD)
               <option value="{{$negocioD->AÑO}}" 
                  selected="selected">{{$negocioD->AÑO}}</option>
               @endforeach
            </select>
            {{Form::label('', 'Mes')}}
            <select id="mes" name="mes">
               @foreach($selectFecha as $fecha)
               <option value="{{$fecha->mesNum}}" name="{{$fecha->mesNom}}">{{$fecha->mesNom}}</option>
               @endforeach
            </select>
            {{Form::label('', 'Negocio')}}
            <select id="negocio" name="negocio" >
               @foreach($negocioDive as $negocioD)
               <option value="{{$negocioD->NEGOCIO}}" selected="selected">{{$negocioD->NEGOCIO}}</option>
               @endforeach
            </select>
            <button type="submit"class="btn btn-default" id="filter">Filtrar
            <i class="fa fa-filter"></i>
            </button>
         </form>
         <?php if(isset($_GET['año']) && isset($_GET['mes']) && isset($_GET['negocio'])){
            echo "<h4> Año: $año |   Mes: {$fecha->mesNom}   |  Negocio: $negocio </h4>";
            }?><!-- 
            <p id="mesJS"></p> -->
      </div>
   </div>
</div>

After receiving the request variables, it should show the correct values instead of the last index of the dropdown. What can I do? dropdown options:

enter image description here

After submit:

enter image description here

1

1 Answers

0
votes

The reason it is showing the last index of the dropdown is that you are looping on your various items and creating your options within each select box with 'selected' overwritten every time all the way to the last one in the loop.

To fix this, you'll need to add some type of conditional on the option box to tell it if the loop item is the correct selected one. If the new model has the key within this loop, then, and only then, make it selected. Something like this (You'll need to make this code your own):

 @foreach($negocioDive as $id=>name)
      <option value="{{$id}}" {{$theThingYouAreSelecting->id === $id?"selected":''}}>$name</option>
 @endforeach

NOTE - I'm assuming you are sending a key->value array to the foreach as this is what the option expects - the key would be the id and the value would be the name for the item.

Once you get this working, I strongly recommend looking at LaravelCollective HTML - it binds the model to the form and automates a huge chunk of the conditionals.