1

I have a page that contains a dropdown whose contents(filters) are a result of a DB query and a table also populated with the results of a DB query. What i would like to do is to select one of the options of the dropdown and once selected, this option should be passed back on to the sql-query of the table as a variable so that the table can be displayed with these new filters.

Model1(Table):

  class TopPage {
       public static function webmasters(){
             $top_pages = DB::select(DB::raw('SELECT * 
                                     FROM my.top_pages
                 WHERE filter IS **Variable**
                                     LIMIT 10'));
             return $top_pages;

       }
  }

Model2(Dropdown):

  Class Dropdown {
        public static function getFilters() {
             $filter = DB::table('my.top_pages')->select(DB::raw('DISTINCT (filter) AS filt'))->lists('filt');
             return $filter;
        }
   }

Controller:

  class WebController extends BaseController {
        public function getSql_Test() {
                    $topPages = TopPage::webmasters();
                $filters = Dropdown::getFilters();
                        return View::make('tools.show',['TopPage'=>$topPages],
                      ['Dropdown'=>$filters]);

View:

  {{ Form::open() }}
    <p></p>
    <table class='liste' style='margin-left: 0px;' cellpadding='5'>
    {{ Form::select('filt', $Dropdown, 2) }}

    <p></p> 
    <tr>
           <td style='background-color: #426bb3; color: white; font-weight: bold; width:16%;'>Date</td>
        <td align='left' style='background-color: #426bb3; color: white; font-weight: bold; width:12%;'>Page</td>
        <td align='left' style='background-color: #426bb3; color: white; font-weight: bold; width:12%;'>Categorie</td>
    </tr>

        @foreach($TopPage as $topPages)
    <tr>
        <td> {{$topPages->date}} </td>              
        <td> {{$topPages->page}} </td>        
        <td> {{$topPages->categorie}} </td> 
    </tr>
    @endforeach 

        </table><br>  

   {{ Form::close() }}  

Any help would be greatly appreciated.

1 Answer 1

1

Are you forgetting to include submit in view so it can call the filter?

{{ Form::submit('Search') }} 

In your form action you need to reference the function

Form::open(array('action' => 'Controller@method'))

Laravel Documentation

Laravel Documentation for HTML Forms

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for the input. I have included Submit. I am stuck as to how i can get the selected option from the dropdown back into the variable in my sql-query. Do you have any heads up on how to go about this?
You need to make a function that has a parameter. Then in your form action pass in the argument for the function which will be your dropdown value.
However understand that you can use query strings to accomplish the same thing. for example www.questions.com/questions?=unanswered

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.