php - Ajax 搜索过滤器

标签 php jquery mysql ajax symfony

我有一个“内容”表格,您可以为您的内容分配类别。

class Content
{

    private $name;

    private $categories;

    ...
    ...
    ...

    public function __construct()
    {
        $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function addCategories(\Publicartel\AppBundle\Entity\Category $categories)
    {
        $this->categories[] = $categories;

        return $this;
    }

    public function removeCategories(\Publicartel\AppBundle\Entity\Category $categories)
    {
        $this->categories->removeElement($categories);
    }

    public function setCategories(\Publicartel\AppBundle\Entity\Category $categories = null)
    {
        $this->categories = $categories;

        return $this;
    }

    public function getCategories()
    {
        return $this->categories;
    }

}

然后我执行查询来查找所有类别并将它们显示在页面上。

$categories = $em->getRepository('PublicartelAppBundle:Category')->getAllCategories();

public function getAllCategories()
    {

    $em = $this->getEntityManager();

    $dql = 'SELECT c FROM Publicartel\AppBundle\Entity\Category c';

    $query = $this->getEntityManager()
        ->createQuery($dql)
        ->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);

    return $query->execute();
}

显示模板 Twig 中的所有类别。

<div class="form-group">
            <label for="publicartel_appbundle_category_name">Buscar por categoría:</label>
            <select id='selectCategory' class="form-control select2">
                {% for categories in categories %}
                    <option>
                        {{ categories.name }}
                    </option>
                {% endfor %}
            </select>
        </div>

我分配一个句柄来选择显示类别,以在 ajax 中传递来自选择的值。

我有一个函数可以检测所选选项的变化。

保存选择值以传递给 Controller ​​以执行查询:

$('#selectCategory').change(function() {
                var optionSelect = $(this).val();
                console.log(optionSelect);
                $.ajax({
                    url: '{{path('playlist_new') }}', 
                    data: '&category='+optionSelect,
                    type: 'POST',
                    success: function(catContent) {

                    {% for contentCategory in catContent %}
                        var nameContent =  '{{ contentCategory.name }}';
                        console.log(nameContent);
                    {% endfor %}

                    $('playlist_content_name').html(nameContent);
                },
                error: function(e){
                    console.log(e.responseText);
                }
            });
        });

$category = $request->query->get('category');

    $catContent = $em->getRepository('PublicartelAppBundle:Content')->findByCategory($category);

return $this->render('PublicartelAppBundle:Playlist:new.html.twig', array(
            'catContent' => $catContent, 
            'categories' => $categories,
            'entity'     => $entity,
            'form'       => $form->createView(),
        ));

显示所选类别内容的查询:

public function findByCategory($category)
    {
        $em = $this->getEntityManager();

    $dql = 'SELECT c FROM Publicartel\AppBundle\Entity\Content c';

    if (!is_null($category)) {
        $dql .= " WHERE c.categories.name LIKE :category";
    }

    $query = $this->getEntityManager()
        ->createQuery($dql)
        ->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);

    if (!is_null($category)) {
        $query->setParameter('category', '%'.$category.'%');
    }

    return $query->getResult();
}

但是控制台总是显示相同的结果,但在我的选择中选择不同的选项。

就是选择你选择的选项,总是显示相同的结果。

最佳答案

我不知道类别名称是什么样的,但这里有一个可能的错误:

$dql .= " WHERE c.categories.name LIKE :category";

{% for contentCategory in catContent %}
    var nameContent =  '{{ contentCategory.name }}';
    console.log(nameContent);
{% endfor %}

如果类别以相同的字母开头,则将导致始终显示相同的类别。

尝试将 $dql 变量的 LIKE 部分更改为 =

关于php - Ajax 搜索过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31091326/

相关文章:

javascript - PHP 使用 move_uploaded_file 上传图像两次(不应该的时候)

php - 如何根据数组中指定的字母表以最少的时间损失对大型数组进行排序?

php - 直接查看 php 文件时出现 Wordpress 404 错误

javascript - jQuery - onClick 在新页面中打开随机链接

php - MySQL 中的合并查询

使用 SUM 的 MYSQL 查询

javascript - $ sign in jquery document ready 的含义

jQuery CSS AddClass - RemoveClass 在 Internet Explorer 中移动 Div

ASP.NET MVC : Ajax actionlink or Jquery solution?

php - 使用 codeigniter 更新查询中的 CONCAT