Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何用细枝按类别划分项目_Php_Symfony_Foreach_Twig - Fatal编程技术网

Php 如何用细枝按类别划分项目

Php 如何用细枝按类别划分项目,php,symfony,foreach,twig,Php,Symfony,Foreach,Twig,我试图用javascript和twig按类别划分我的项目,所以从数据库中取出所有产品后,我不知道如何用{%for%}分隔产品。这是我的小树枝代码: <div class="col-sm-4" itemscope itemtype="http://schema.org/Product"> <div class="panel panel-default"> <div class="panel-heading"> &l

我试图用javascript和twig按类别划分我的项目,所以从数据库中取出所有产品后,我不知道如何用{%for%}分隔产品。这是我的小树枝代码:

<div class="col-sm-4" itemscope itemtype="http://schema.org/Product">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h5 class="panel-title truncate">Categoria:{{ producto.idCategoria }}</h5>
        </div>
        <div class="panel-body">
            Nombre:{{ producto.producto }}
            <img src="{{ asset('bundles/savainventario/images/'~producto.filePersistencePath ) }}"
                 alt="404 file not found" class="img-thumbnail"/>
        </div>
        <div class="panel-footer">

            <div class="container-fluid">

                {#Precio#}
                <span itemprop="price">
                 Precio:{{ producto.precio }}.Bsf
                </span>
                {#Form#}
                <form class="form-inline" role="form" method="get"
                      action={{ path('sava_inventario_addcart', {'id': producto.idProducto }) }}>
                    <div class="form-group">
                        <input class="btn btn-default" type="submit" value="Agregar">
                    </div>

                    {#Ver mas#}
                    <!-- Button trigger modal -->
                </form>
                <button class="btn btn-primary btn-sm" data-toggle="modal"
                        data-target="#myModal{{ producto.idProducto }}">
                    Ver mas...
                </button>
                <!-- Modal -->
                <div class="modal fade" id="myModal{{ producto.idProducto }}" tabindex="-1" role="dialog"
                     aria-labelledby="myModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal"><span
                                            aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                                </button>
                                <h4 class="modal-title" id="myModalLabel">{{ producto.producto }}</h4>
                            </div>
                            <div class="modal-body">
                                <!-- Datos productos -->
                                <table class="table table-striped">
                                    <tr>
                                        <td>Nombre:</td>
                                        <td>{{ producto.producto }}</td>
                                    </tr>
                                    <tr>
                                        <td>Image:</td>
                                        <td>
                                            <img src="{{ asset('bundles/savainventario/images/'~producto.filePersistencePath ) }}"
                                                 alt="404 file not found"/></td>
                                    </tr>
                                    <tr>
                                        <td>Descripcion</td>
                                        <td>{{ producto.descripcionProducto }}</td>
                                    </tr>
                                    <tr>
                                        <td>Precio:</td>
                                        <td>{{ producto.precio }}</td>
                                    </tr>
                                    <tr>
                                        <td>Cantidad:</td>
                                        <td>{{ producto.cantidad }}</td>
                                    </tr>
                                    <tr>
                                        <td>Categoria:</td>
                                        <td>{{ producto.idCategoria.categoria }}</td>
                                    </tr>
                                    <tr>
                                        <td>Modelo:</td>
                                        <td>{{ producto.idModelo.modelo }}</td>
                                    </tr>
                                    <tr>
                                        <td>Video:</td>
                                        <td>
                                            <iframe width="433" height="315"
                                                    src="//www.youtube.com/embed/tQShyqnRx3s?list=PLw4rBoBPv1Vbq16M4SFkJPZj08FMaaR-8"
                                                    frameborder="0" allowfullscreen></iframe>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>


        </div> {#footer end#}
    </div>
</div>

{% endif %}

{% endfor %}

</div>
</div>

类别:{{producto.idCategoria}
名称:{{producto.producto}}
{#Precio}
Precio:{{producto.Precio}}.Bsf
{形式}
{vermas}
圣诞节。。。
&时代;关
{{producto.producto}}
名义:
{{producto.producto}}
图片:
描述
{{producto.descripionproducto}}
Precio:
{{producto.precio}}
康蒂达:
{{producto.cantidad}
类别:
{{producto.idCategoria.categoria}
模型:
{{producto.idModelo.modelo}
视频:
关
{#页脚结束}
{%endif%}
{%endfor%}

我的问题是如何按类别划分我的项目,在一个类别中我将其打印在一个div容器中,在另一个div容器中我添加来自另一个类别的产品,依此类推。

如果您的实体设置正确,这应该不难:

//Contoller

/**
 * @Template()
 */
public function showProductsByCategory()
{
    $categories = $this->getDoctrine()->getManager()
        ->getRepository("NamespacedBundle:Category")->findAll();
    return array(
        'categories' => $categories
    );
}
假设您的类别了解产品

//Category Entity
/**
 * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
 */
 protected $products;
您的产品与该类别相关

//Product Entity
/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
 * @ORM\JoinColumn(name="category", referencedColumnName="category_id")
 */
 protected $category;
那么你的树枝可能是基于:

//Sample Twig
{% for category in categories %}
    <div class="container">
        <h1>{{category.name}}</h1>
        <ul>
        {% for product in category.products %}
            <li>{{product.name}}</li>
        {% endfor %}
    </div>
{% endfor %}
//示例细枝
{categories%%中的类别为%s}
{{category.name}
    {category.products%中的产品为%s}
  • {{product.name}
  • {%endfor%} {%endfor%}
查找控制中断算法。这里有一个C语言的例子-你不能在细枝上打断循环。你为什么要打断它?因为我使用的是elasticsearch而不是doctrine,我不能使用这个方法,但仍然给了我一个解决方法的想法。谢谢你的回答。