【问题标题】:PHP Strict searchingPHP 严格搜索
【发布时间】:2018-12-06 11:34:36
【问题描述】:

我在搜索时遇到问题,我正在处理一个项目,以便用户可以根据邮政编码查找交货日期。荷兰和比利时都有邮政编码,荷兰的邮政编码只是数字(例如 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 %}

搜索,无严格匹配

编辑:我通过删除搜索前后的 % 解决了严格匹配的问题。

【问题讨论】:

  • 为什么不搜索:your_field = pincode,而不是 your_field LIKE % pincode %?
  • @Shan 因为它不会显示任何记录,但我通过删除 % 解决了它。
  • 正如@Shan 所建议的那样?请你们中的一个写下它作为答案然后接受它。
  • @Veve 我已经用答案编辑了帖子,但我还有另一个问题。
  • 这不是办法。编辑您的代码以反映您的修改,然后改写您的问题。

标签: php symfony matching strict


【解决方案1】:

关于严格匹配的问题:

通过删除搜索前后的 % 来修复它:

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);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多