多个if-else if语句树枝

时间:2017-07-06 12:01:35

标签: php symfony twig

所以我有一个应用程序,我想显示几个用户的计划。在显示计划之前,用户可以在表单中选择一天。 对于工作日周一= 1和周五= 5.我将其存储在我的数据库中,用户有一个计划日。 所以在我的控制器中我喜欢这样来检索共享相同计划的所有用户

$planningRepo = $this->getDoctrine()->getManager()->getRepository('OCPediBundle:Planning');
        $user = $this->getUser();
        $planningID = $user->getPlanning()->getId();
        $planning = $planningRepo->find($planningID);
        $userRepo = $this->getDoctrine()->getManager()->getRepository('OCUserBundle:User');
        $users = $userRepo->findByPlanning($planningID);
        var_dump($users);
        if (null === $planning) {
            throw new NotFoundHttpException("Le planning d'id ". $id. " n'existe pas.");
        }
        return $this->render('::planning.html.twig', array('planning' => $planning,
                                                           'users' => $users));
    }

在我的视图中:

<table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Lundi</th>
                        <th>Mardi</th>
                        <th>Mercredi</th>
                        <th>Jeudi</th>
                        <th>Vendredi</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <th scope="row">Responsable</th>
                        {% for user in users %}
                        {{dump(user.planningday)}}
                        {% if user.planningday == 1 %}
                        <td>{{user.name}} {{user.lastname}}</td>
                        {% elseif user.planningday == 2 %}
                        <td>{{user.name}} {{user.lastname}}</td>
                        {% elseif user.planningday == 3 %}
                        <td>{{user.name}} {{user.lastname}}</td>
                        {% elseif user.planningday == 4 %}
                        <td>{{user.name}} {{user.lastname}}</td>
                        {% else %}
                        <td>{{user.name}} {{user.lastname}}</td>
                        {% endif %}
                        {% endfor %}
                    </tr>
                    <tr>
                        <th scope="row">Description</th>
                        {% for user in users %}
                        {% if user.planningday == 1%}
                        <td>{{user.planningcontent}}</td>
                       {% elseif user.planningday == 2 %}
                        <td>{{user.planningcontent}}}</td>
                        {% elseif user.planningday == 3 %}
                        <td>{{user.planningcontent}}}</td>
                        {% elseif user.planningday == 4 %}
                        <td>{{user.planningcontent}}}</td>
                        {% else %}
                        <td>{{user.planningcontent}}}</td>

                        {% endif %}
                        {% endfor %}
                    </tr>
                </tbody>
            </table>
</div>

但是我的问题是,我的if语句不起作用。示例i让用户在星期二选择第2天,并在星期一td显示姓名,姓氏和内容。 有人可以帮帮我吗?感谢

1 个答案:

答案 0 :(得分:2)

问题是您只在if。

中打印一列

您可以分割它并始终打印列

试试这个:

{% for user in users %}
      {{dump(user.planningday)}}
      <td>
      {% if user.planningday == 1 %}
          {{user.name}} {{user.lastname}}
      {% endif %}
      </td>
      <td>
      {% if user.planningday == 2 %}
          {{user.name}} {{user.lastname}}
      {% endif %}
      </td>
      <td>
      {% if user.planningday == 3 %}
          {{user.name}} {{user.lastname}}
      {% endif %}
      </td>
      <td>
      {% if user.planningday == 4 %}
          {{user.name}} {{user.lastname}}
      {% endif %}
      </td>
      <td>
      {% if user.planningday == 5 %}
          {{user.name}} {{user.lastname}}
      {% endif %}
      </td>
{% endfor %}

您可以对描述行使用相同的策略

相关问题