PHP严格搜索
我在搜索方面遇到了问题,我正在做一个项目,这样用户就可以根据zipcodes找到交付日期。荷兰和比利时都有Zipcode,荷兰Zipcode只是数字(例如1000),比利时Zipcode前面有一个B(例如B1000)。当我搜索1000时,它同时显示1000(荷兰zipcode)和B1000(比利时zipcode)。我试着用“=”、“匹配”、“严格”和“=”,而不是“喜欢”。我希望你们能帮我解决这个问题。提前谢谢 控制器PHP严格搜索,php,symfony,matching,strict,Php,Symfony,Matching,Strict,我在搜索方面遇到了问题,我正在做一个项目,这样用户就可以根据zipcodes找到交付日期。荷兰和比利时都有Zipcode,荷兰Zipcode只是数字(例如1000),比利时Zipcode前面有一个B(例如B1000)。当我搜索1000时,它同时显示1000(荷兰zipcode)和B1000(比利时zipcode)。我试着用“=”、“匹配”、“严格”和“=”,而不是“喜欢”。我希望你们能帮我解决这个问题。提前谢谢 控制器 <?php namespace AppBundle\Controll
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Nlbelevering;
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 %}
{%extends'layout/default.html.twig%}
{%block title%}NL/BE-杠杆30-33{%endblock%}
{%block content%}
佐克·福尔穆利埃先生{
填充:20px;
边缘顶部:20px;
}
佐肯
{%q==0%}
{%else%}
{%endif%}
邮政编码
第30周
第31周
第32周
第33周
{nlbelevering%中的邮政编码为%}
{{postcode.postcode}
{{postcode.week1}}
{{postcode.week2}}
{{postcode.week3}}
{{postcode.week4}}
{%endfor%}
邮政编码
第34周
第35周
第36周
第37周
{nlbelevering%中的邮政编码为%}
{{postcode.postcode}
{{postcode.week5}}
{{postcode.week6}}
{{postcode.week7}}
{{postcode.week8}}
{%endfor%}
{%endblock%}
搜索,无严格匹配
{% 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);
}
为什么不搜索:您的_字段=pincode,而不是像%pincode%这样的_字段?@Shan,因为它不会显示任何记录,但我通过删除%.解决了这个问题,正如@Shan建议的那样?请你们其中一位写下来作为答案,然后接受它。@Veve我已经用答案编辑了这篇文章,但我还有另一个问题。这不是解决问题的方法。编辑您的代码以反映您的修改,然后改写您的问题。