【问题标题】:Count content of columns in table symfony2/twig/doctrine计算表 symfony2/twig/doctrine 中列的内容
【发布时间】:2016-01-18 19:45:38
【问题描述】:

所以我在我的 symfony2 项目中使用 crud 创建了这个表。该表显示了不同的产品,每种产品都有自己的价格。我想在表格底部输出所有产品的总价值。

实体“waarde”的index.html.twig

{% extends '::base.html.twig' %}

{% block body -%}
<h1>Waarde voorraad</h1>

<table class="records_list">
    <thead>
        <tr>
            <th>Product</th>
            <th>Type</th>
            <th>Fabriek</th>
            <th>Aantal</th>
            <th>Prijs</th>
            <th>Inkoop Waarde</th>
            <th>Verkoop Waarde</th>
            <th>Locatie</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
    {% for entity in entities %}
        <tr>
             <td>{{ entity.getProduct().getNaam() }}</td>
             <td>{{ entity.getProduct().getType() }}</td>
             <td>{{ entity.getProduct().getFabriek() }}</td>
             <td>{{ entity.aantal }}</td>
             <td>{{ entity.getProduct().getInkoopprijs() }}</td>
             <td>{{ entity.getProduct().getInkoopprijs() * entity.aantal }}</td>
             <td>{{ entity.getProduct().getVerkoopprijs() * entity.aantal }}</td>
             <td>{{ entity.getLocatie().getLocatienaam() }}</td>
            <td>
            <ul>
                <li>
                    <a href="{{ path('waarde_show', { 'id': entity.id }) }}">show</a>
                </li>
                <li>
                    <a href="{{ path('waarde_edit', { 'id': entity.id }) }}">edit</a>
                </li>
            </ul>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>

    <ul>
    <li>
        <a href="{{ path('waarde_new') }}">
            Create a new entry
        </a>
    </li>
</ul>
{% endblock %}

我在实体“Product”和“Waarde”(=value) 之间有关联。

<td>{{ entity.getProduct().getInkoopprijs() }}</td> 

上面的代码返回产品的价格。我已经做了一个简单的代码,通过这个代码计算单个产品的价值(“aantal”=数量/产品数量)

 <td>{{ entity.getProduct().getInkoopprijs() * entity.aantal }}</td>

【问题讨论】:

    标签: php symfony doctrine twig


    【解决方案1】:
    {% set sum = 0 %}
    
    {% for entity in entities %}
    
        {% set sum = sum + (entity.getProduct().getInkoopprijs() * entity.aantal) %}
    
    {% endfor %}
    
    Total price: {{ sum }}
    

    【讨论】:

    • 谢谢!不得不删除“endforeach”中的每个,但我看到你已经改变了它
    猜你喜欢
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多