1

I have the output from my controller as shown below:

D:\web\www2\application\src\CommonBundle\Controller\ClassesController.php:22:
 array (size=2)
  'Grade 8' => 
    array (size=1)
      0 => 
        array (size=2)
         0 => 
           object(CommonBundle\Entity\GradeLevel)[363]
             ...
         1 => 
           object(CommonBundle\Entity\GradeLevel)[367]
             ...
    'Grade 9' => 
      array (size=1)
       0 => 
        array (size=1)
          0 => 
           object(CommonBundle\Entity\GradeLevel)[372]
             ...

This data is coming from a One-T-_Many relation and what I want to do is have a grouping on my html table. Below is how I create my array of object:

$grades = $this->getDoctrine()->getRepository('CommonBundle:Grade')->findAll();

    $classes = array();
    foreach($grades as $grade){
       $classes[$grade->getName()][] = $this->getDoctrine()->getRepository('CommonBundle:GradeLevel')->findBy(array(
          'grade' => $grade->getId()
        ));
     }

    return $this->render('CommonBundle:Classes:index.html.twig', array(
       'classes' => $classes
      )); 

If this was a plain PHP code I would simple do something like this:

<?php

   foreach($classes as $key => $values){
      ...
      <td colspan="4"><?php echo $key //Being the category name as indexed from the controller ?></td>
      //And continue with the contents of the classes
      foreach($values as $class){
         <tr>
          <td><?php echo $class->getName(); ?>
           ....
        }
    }

I am finding it hard getting any solutiuon on the web to suite my requirement. Can anybody point me to a right direction as the TWIG documentation seems to be abstract on this as well.

EDIT:

Below is the current table structure that I have to make it easy for any helper to have an idea of what I am trying to achieve.

<table class="table">
<thead>
  <tr>
    <th>#</th>
    <th>Class</th>
    <th>Created on</th>
    <th>Modified at</th>
    <th>Actions</th>
  </tr>
</thead>
<tbody>
  {% for values in classes %}
    <tr>
      <!--- Category name should go here e.g "Grade 8" then next loop should list all Grades from that category -->
      <td colspan="5" align="right">{{ values }}</td>
    </tr>
    {% for item in values %}
      <tr>
        <td>&nbsp;</td>
        <td>{{ item.name }}</td>
        <td>{{ item.created|date('Y-m-d H:i:s') }}</td>
        <td>{{ item.modified|date('Y-m-d H:i:s') }}</td>
        <td><a class="btn btn-sm btn-primary" href="#" role="button">Summary</a></td>
       </tr>
    {% endfor %}
  {% endfor %}
</tbody>

EDIT 2

enter image description here

2 Answers 2

1

You need do this same in twig what you would do in php - loop inside loop.

Remember that you have 3 levels in your array.

{% for values in classes %}
    {% for item in values[0] %}
        <li>{{ item.name }}</li>
    {% endfor %}
{% endfor %}

access first (and only element) under Grade * key with values[0].

It's not best thing from performance perspective, but if you can't change your data structure then you can use it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Pawel. I have added my HTML so that you can actually see what I am trying to do. I hope that helps because I still cannot access the key string of my array objects.
Whilst at your update what would be your best advice in terms of performance to achieve something as shown on the image in my update above? Thanks in advance.
0

This is my final table code in the view that renders correctly and as desired.

<table class="table">
<thead>
  <tr>
    <th>#</th>
    <th>Class</th>
    <th>Created on</th>
    <th>Modified at</th>
    <th>Actions</th>
  </tr>
</thead>
<tbody>
  {% for group, values in classes %}
    <tr>
      <td colspan="5" align="left"><strong>{{ group }}</strong></td>
    </tr>{% for item in values %}
      <tr>
        <td>&nbsp;</td>
        <td>{{ item.name }}</td>
        <td>{{ item.created|date('Y-m-d H:i:s') }}</td>
        <td>{{ item.modified|date('Y-m-d H:i:s') }}</td>
        <td><a class="btn btn-sm btn-primary" href="#" role="button">Summary</a></td>
       </tr>
    {% endfor %}
  {% endfor %}
</tbody>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.