PHP 严格搜索
PHP Strict searching
我在搜索时遇到问题,我正在做一个项目,以便用户可以根据邮政编码找到交货日期。荷兰和比利时都有邮政编码,荷兰的邮政编码只是数字(例如 1000),而比利时的邮政编码前面有一个 B(例如 B1000)。当我搜索 1000 时,它同时显示 1000(荷兰邮政编码)和 B1000(比利时邮政编码)。我尝试使用“=”、'MATCH'、“==”、'STRICT' 和“===”来代替 'LIKE'。我希望你们能帮我解决这个问题。提前致谢。
控制器
<?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 %}
搜索,不严格匹配
编辑:我通过删除搜索前后的 % 解决了严格匹配的问题。
关于严格匹配的问题:
通过删除搜索中前后的 % 修复了此问题:
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);
}
我在搜索时遇到问题,我正在做一个项目,以便用户可以根据邮政编码找到交货日期。荷兰和比利时都有邮政编码,荷兰的邮政编码只是数字(例如 1000),而比利时的邮政编码前面有一个 B(例如 B1000)。当我搜索 1000 时,它同时显示 1000(荷兰邮政编码)和 B1000(比利时邮政编码)。我尝试使用“=”、'MATCH'、“==”、'STRICT' 和“===”来代替 'LIKE'。我希望你们能帮我解决这个问题。提前致谢。
控制器
<?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 %}
搜索,不严格匹配
编辑:我通过删除搜索前后的 % 解决了严格匹配的问题。
关于严格匹配的问题:
通过删除搜索中前后的 % 修复了此问题:
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);
}