title: Select result in grouped row using fetch and while in PHP
body: <p>In the code below, I send a request, the I put the result In a table using PDO fetch. But when we have a "Data Element" with 2 elements, it creates 2 rows in the table, 1 for each "element". Example:</p>
  
  <pre><code>****************************************
  **Data Element Name** | data 1
  ****************************************
  **Elements**          | element 1
  ****************************************
  
  ****************************************
  **Data Element Name** | data 1
  ****************************************
  **Elements**          | element 2
  ****************************************
  and what I want is:
  ****************************************
  **Data Element Name** | data 1
  ****************************************
  **Elements**          |element 1
                        |element 2
  ****************************************
  </code></pre>
  
  <p>Thank you!</p>
  
  <pre><code>&lt;?php
  if ($select=="Data Element Name"){
   $result="SELECT    
        D1.Data_Element_Name dataElementName,
        D2.Data_Element_Name element
       FROM 
        data_element_name D1
        LEFT JOIN packagedelement ON packagedelement.id=COALESCE(D1.base_Attribute, D1.base_Class)
        LEFT JOIN ownedattribute ON ownedattribute.FK_packagedElement=packagedelement.PrimaryKey
        LEFT JOIN data_element_name D2 ON D2.base_Attribute=ownedattribute.id
            WHERE 
        D1.Data_Element_Name LIKE '%$search%' ";
  
       try {
      $req =$pdo-&gt;query($result);
  } catch(PDOException $ex) {
      echo "An Error occured!"; //user friendly message
      some_logging_function($ex-&gt;getMessage());
  }
      echo 'Data Elements including the keyword: " '.$search.' " ';
      echo '&lt;table id="table1" style="margin-left:10px; width:700px;"&gt;';
      //Loop to stock results on an array
      $i=0;
      while($data = $req-&gt;fetch())
      { 
      $array[$i]=$data['dataElementName'];
      $element[$i]=$data['element'];
      echo '&lt;tr&gt;
          &lt;td&gt;
                  &lt;a href="" onclick="var source = ($(\'#arrow'.$i.'\').attr(\'src\') === \'img/arrowInactive.jpg\') ? \'img/arrowActive.jpg\' : \'img/arrowInactive.jpg\'; $(\'#arrow'.$i.'\').attr(\'src\', source); $(\'#hiddenDiv'.$i.'\').toggle(); return false;"&gt;
            &lt;img id="arrow'.$i.'" src="img/arrowInactive.jpg" alt="arrowInactive" /&gt;' .json_encode($array[$i]). 
          '&lt;/a&gt;
          &lt;/td&gt;
                 &lt;/tr&gt;
            &lt;tr id="hiddenDiv'.$i.'" style="padding-left:20px;display:none"&gt;
          &lt;td&gt;
            &lt;table&gt;    
              &lt;tbody&gt;
                &lt;tr&gt;
                  &lt;td class="LeftCell"&gt;Elements&lt;/td&gt;
                  &lt;td id="elements"&gt;'.json_encode($element[$i]).'&lt;/td&gt;
                &lt;/tr&gt;
              &lt;/tbody&gt;
            &lt;/table&gt;
          &lt;/td&gt;
            &lt;/tr&gt;';
      $i=$i+1;
  }
  echo '&lt;/table&gt;';
  }
  ?&gt;
  </code></pre>