Search code examples
phpsymfonymatchingstrict

PHP Strict searching


I am having issues with searching, I am working on a project so that users can find delivery dates based on zipcodes. There are zipcodes from both Holland and Belgium, the Dutch zipcodes are just numbers (ex. 1000) and the Belgium ones are with a B in front of it so (ex. B1000). When I search for 1000, it shows both 1000(dutch zipcode) and B1000(belgium zipcode). I tried to use '=', 'MATCH', '==', 'STRICT' and '===' instead of 'LIKE'. I hope you guys can help me with this issue. Thanks in advance.

Controller

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

}

?>

Twig

{% 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 %}

Search, no strict matching

Search

EDIT: I resolved the issue with strict matching by removing the % in front and after search.


Solution

  • Issue about strict matching:

    Fixed it by removing the % in front and after in search:

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