我试图让用户在搜索栏中输入任意数量的数字,并使这些数字与我的数据库中包含这些数字的to字符串相匹配,然后提取整个字符串。我现在很难让它工作。我试图让它只工作与4个数字,我认为这将是一个更容易的壮举,同时也给了我一个很好的概念如何处理其余的。到目前为止,我没有收到任何错误,但当我输入4个我知道应该与现有字符串匹配的数字时,它没有显示任何信息。我使用的数据库是mysql。这是我的控制器中的内容:
public function executeSearch()
{
$search = Input::get('search');
session_start();
$_SESSION['search'] = $search;
$nsn_list_all = nsn::all();
$query = \App\nsn::where('nsn_number', $search)->get();
return View('Search_views.searchresults',compact('search', 'nsn_list_all', 'query'));
}
这是我的观点:
@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
另外,在没有if
语句的情况下,foreach
语句用于查找精确匹配的内容。
发布于 2015-04-22 05:09:37
我想通了。这将定位字符串中所有匹配的数字。
在控制器中:
if ($search)
{
$nsn_query = DB::table('nsns')->where('nsn_number', 'LIKE', "%$search%")->get();
}
视图:
@foreach($nsn_query as $search_result)
<td>{{$search_result->nsn_number}}</td>
<td>{{$search_result->pn_name}}</td>
<td></td>
@endforeach
仅供参考,多个结果将运行,不会换行,即将在这一部分工作,但我想如果有人需要帮助找出查询部分。
https://stackoverflow.com/questions/29779705
复制相似问题