2017-07-06 67 views
2

所以我有一個應用程序,我想爲幾個用戶顯示一個規劃。在顯示計劃之前,用戶可以在表單中選擇一天。 星期一星期一= 1和星期五= 5。我將它存儲在我的數據庫中,用戶有一個計劃日。 所以在我的控制器,我喜歡這個檢索所有共享同一規劃多if-else if語句樹枝

$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語句不工作。例如,我有一個用戶選擇星期二的第2天,並且姓名,姓氏和內容顯示在星期一td中。 有人可以幫助我嗎?謝謝

回答

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

您可以使用說明行相同的策略

+0

是啊,這工作。謝啦!非常感謝您的幫助。 – Simon

+0

很高興爲您服務!接受幫助其他人解決同樣問題的答案 –

+0

@Simon通過使用簡單的'for'-loop內部樹枝縮短代碼。觀看[這裏](https://twigfiddle.com/lqd8zd) – DarkBee