0

I'm trying to get a search bar to go through my database and grab all the numbers that include these 2 random numbers a user will input. The 2 consecutive numbers can be anywhere in the string and then it should grab the whole string. One issue I'm having is that the form is in the layout (maybe it doesn't matter, but i would still like to know how i can put the layout in a controller and include it in other methods)so it doesn't have it's own method and the form itself i'm trying to route to another view with the search results appearing. As of right now this is the error I'm getting: Missing argument 1 for App\Http\Controllers\SearchController::SearchEngine()

In my layout:

{!! Form::open(array('method' => 'GET', 'url' => 'search_engine')) !!}
    <div class="form-group">
        {!! Form::label('SEARCH:') !!}
        {!! Form::text('search', null, ['class' => 'form-control',
          'placeholder' =>'(NSN or PN)', 'size' => '20']) !!}
        {!! Form::submit('Submit', ['class' => 'btn btn-primary form-control']) !!}
    </div>
{!! Form::close() !!}

In my Routes: `Route::get('search_engine', 'SearchController@SearchEngine');

In my controller:

 public function SearchEngine($i)
    {
        $search = input::get('search');
        $terms = explode(" ", $search);
        $query = fsgdata::where('fsg_number')->get();

        foreach($terms as $each) {
            $i++;
            if($i == 1) {
                $query .= "fsg_number LIKE '%$each%' ";
                echo $each;
            }
            else {
                $query .= "0R fsg_number LIKE '%$each%' ";
            }
        }

    // connect
        mysql_connect("localhost", "USERNAME", "PASSWORD");
        mysql_select_db("DB_NAME");

        $query = mysql_query($query);
        $numrows = mysql_num_rows($query);
        if ($numrows > 0) {

            while ($row = mysql_fetch_assoc($query)) {
                $fsg_number = $row['fsg_number'];

                echo "<h2>$fsg_number</h2>";
            }
        }
        else {
        }

    // disconnect
        mysql_close();

        return View('Partials.search_form');
    }

And my view is just another search bar etc.

Unfortunately, maybe I'm a dumb dumb, but laravels docs doesn't go into much detail about any of their features and instead choose to do a one and done example picture style. I'm new to laravel period so to me to task of filling in the dots can be very daunting.

Vantheman6
  • 1,719
  • 3
  • 13
  • 13
  • 1
    you have defined the function as `public function SearchEngine($i)` where the `$i` argument is never passed. What should `$i` be? – manix Apr 07 '15 at 23:39
  • haha wow. thanks. Honestly i found this code, because i have never made a search bar before and the whole thing is over my head. After i fixed that though I'm getting a weird error `mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead` . Is it really telling me i can't use a database that my boss asked of me? – Vantheman6 Apr 08 '15 at 13:37

1 Answers1

1

I know I'm rather late but why wouldn't you use Laravel's native query builder? It will clean up your queries, automatically sanitizes them.

It also looks like you're trying to retrieve the data from the form wrong as well.

An example method for getting data from a post form should look something like this:

public function retrieveFromPost(Request $request) {
    if ($request->has('param')) {
        $param = $request->input('param');
            $data = DB::select("SELECT *
            FROM table_name
            WHERE column_name LIKE :param" ['param' => $param]);
            return view('view', ['data' => $data]);
    } else { 
        return; 
    }

This will verify that the request has a valid input under the "param" parameter sent from the post form. It will select data using the SQL 'LIKE' statement, and return a view with all the retrieved data passed into the view. The else statement will return and end the script if the data from the post request is invalid.

Sloth
  • 53
  • 7