PHP严格搜索
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP严格搜索相关的知识,希望对你有一定的参考价值。
我遇到了搜索问题,我正在开发一个项目,以便用户可以根据zipcodes查找交付日期。荷兰和比利时都有拉链码,荷兰的拉链码只是数字(例如1000),而比利时的拉链码在它前面有一个B(例如B1000)。当我搜索1000时,它显示1000(荷兰邮政编码)和B1000(比利时邮政编码)。我尝试使用'=','MATCH','==','STRICT'和'==='而不是'LIKE'。我希望你们能帮我解决这个问题。提前致谢。
调节器
<?php
namespace AppBundleController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use AppBundleEntityNlbelevering;
class PostcodezoekerController extends Controller
{
/**
* @Route ("/nlbe/levering", name="nlbelevering")
*/
public function nlbeleveringHomepage(Request $request){
$nlbelevering = $this->getDoctrine()->getRepository("AppBundle:Nlbelevering")->findAll();
$search = $request->get('q');
$em = $this->getDoctrine()->getManager();
if ($search) {
$nlbelevering = $em->createQuery('Select a FROM AppBundle:Nlbelevering a WHERE a.postcode LIKE :query')
->setParameter('query', '%'.$search.'%');
} else{
$nlbelevering = $em->createQuery('Select a FROM AppBundle:Nlbelevering a WHERE a.postcode LIKE :query')
->setParameter('query', '%'.$search.'%');
}
//Verwijzing naar formulier
return $this->render('nlbe/levering/Nlbelevering.html.twig', [
'nlbelevering' => $nlbelevering->getResult(),
'q' => $search
]);
}
}
?>
枝条
{% extends 'layout/default.html.twig' %}
{% block title %}NL/BE - Levering 30-33{% endblock %}
{% block content %}
<style>
.zoekformulier{
padding: 20px;
margin-top: 20px;
}
</style>
<div class= "zoekformulier">
<form>
<input name="q" value="{{ q }}" placeholder="Zoeken" />
<button type="submit">Zoeken</button>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
{% if q == 0 %}
<div class="col-md-12" style="display:none">
{% else %}
<div class="col-md-12">
{% endif %}
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Postcode</th>
<th scope="col">Week 30</th>
<th scope="col">Week 31</th>
<th scope="col">Week 32</th>
<th scope="col">Week 33</th>
</tr>
</thead>
<tbody>
{% for postcode in nlbelevering %}
<tr>
<th scope="row">{{ postcode.postcode }}</th>
<td>{{ postcode.week1 }}</td>
<td>{{ postcode.week2 }}</td>
<td>{{ postcode.week3 }}</td>
<td>{{ postcode.week4 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Postcode</th>
<th scope="col">Week 34</th>
<th scope="col">Week 35</th>
<th scope="col">Week 36</th>
<th scope="col">Week 37</th>
</tr>
</thead>
<tbody>
{% for postcode in nlbelevering %}
<tr>
<th scope="row">{{ postcode.postcode }}</th>
<td>{{ postcode.week5 }}</td>
<td>{{ postcode.week6 }}</td>
<td>{{ postcode.week7 }}</td>
<td>{{ postcode.week8 }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
<hr>
</div>
{% endblock %}
搜索,没有严格的匹配
编辑:我通过删除前面和搜索后的%来解决严格匹配的问题。
答案
关于严格匹配的问题:
通过删除搜索前后的%来修复它:
if ($search) {
$nlbelevering = $em->createQuery('Select a FROM AppBundle:Nlbelevering a WHERE a.postcode LIKE :query')
->setParameter('query', $search);
} else{
$nlbelevering = $em->createQuery('Select a FROM AppBundle:Nlbelevering a WHERE a.postcode LIKE :query')
->setParameter('query', $search);
}
以上是关于PHP严格搜索的主要内容,如果未能解决你的问题,请参考以下文章