I'm trying to have a user put any amount of numbers in a search bar and have those numbers match the to strings in my database that includes those numbers and pull the whole string. I'm having a tough time getting it to work right now. I'm trying to get it just work with 4 numbers and I figure that would be easier an feat, while also giving me a good concept of how to tackle the rest. As of right now I'm not getting any errors, but it isn't displaying any information when I put in 4 numbers that i know should match an existing string. The database I'm using is mysql. This is what I have in my controller:
public function executeSearch()
{
$search = Input::get('search');
session_start();
$_SESSION['search'] = $search;
$nsn_list_all = nsn::all();
$query = \App
sn::where('nsn_number', $search)->get();
return View('Search_views.searchresults',compact('search', 'nsn_list_all', 'query'));
}
This is my view:
@extends('layout.master')
@section('content')
<div class="main">
<br/>
<h2>Search Results for {{$search}}:</h2>
<br/>
<table id="contact_table">
<thead>
<tr>
<th>National Stock Number</th>
<th>Part Number</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
@foreach($query as $search_result)
@if($search_result->nsn_number == $search &&
$search_result->nsn_number[1] == $search[1] &&
$search_result->nsn_number[2] == $search[2] &&
$search_result->nsn_number[3] == $search[3])
<td>{{$search_result->nsn_number}}</td>
<td>{{$search_result->pn_name}}</td>
<td></td>
@endif
@endforeach
</tr>
</tbody>
</table>
</div>
@endsection
On a side note without the if
statement the the foreach
statement works for finding the exact match though.
I figured it out. This will locate all the matching numbers in a string.
In the controller:
if ($search)
{
$nsn_query = DB::table('nsns')->where('nsn_number', 'LIKE', "%$search%")->get();
}
The View:
@foreach($nsn_query as $search_result)
<td>{{$search_result->nsn_number}}</td>
<td>{{$search_result->pn_name}}</td>
<td></td>
@endforeach
FYI Multiple results will run over and won't line break, about to work on that part, but I figured if anyone needed help figuring out the query part.