title: PHP and HTML: Nested foreach loops with HTML interspersed
body: <p>I'm trying to put together a layout of items with an Accordion layout (from Bootstrap). In order to fill the accordions, I reach into a pgsql db to gather the data, and I'm able to retrieve this data.</p>
  
  <p>What I'm having issues with is getting the data to show up at all. Right now I'm getting an HTML 500. It might be a layout issue or it might be a PHP interpretation issue (maybe out of depth? or something not visible to PHP), but I'm having issues determining which is the culprit.</p>
  
  <p>I say this because I have a fairly complicated arrangement I'm attempting to make. </p>
  
  <p>A sample:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
    // db connection info goes here
    
    // pgsql query info goes here
    $i = 0;
    $result = pg_fetch_all($getData);
  ?&gt;
  &lt;!-- Starting the container accordion --&gt;
  &lt;div class="panel-group" id="main-accordion"&gt;
    &lt;?php
      foreach($result as $row):
        $title1 = $row['title1'];
        $title2 = $row['title2'];
    ?&gt;
    &lt;div class="panel panel-default"&gt;
      &lt;div class="panel-heading"&gt;
        &lt;h4 class="panel-title"&gt;
          &lt;a data-toggle="collapse" data-parent="#main-accordion" href="#&lt;?=$row['id']?&gt;"&gt;
            (&lt;?=$row['category']?&gt;) &lt;?=$title1?&gt; - &lt;?=$title2?&gt;
          &lt;/a&gt;
        &lt;/h4&gt;
      &lt;/div&gt;
      &lt;div id="&lt;?=$row['id']?&gt;" class="panel-collapse collapse"&gt;
        &lt;!-- The body of the accordion, contents go here. --&gt;
        &lt;div class="panel-body"&gt;
          &lt;?php 
            // another pgsql query here
            
            $newresult = pg_fetch_all($newgetData);
          ?&gt;
          &lt;!-- In the accordion body, a new group of accordions. This is doable if hardcoded --&gt;
          &lt;div class="panel-group" id="sub-accordion-&lt;?=$i?&gt;"&gt;
            &lt;?php
              // I think this is where the issues start??
              foreach($newresult as $newrow):
                $subtitle = $newrow['subtitle'];
            ?&gt;
            &lt;div class="panel panel-default"&gt;
              &lt;div class="panel-heading"&gt;
                &lt;h4 class="panel-title"&gt;
                  &lt;a data toggle="collapse" data-parent="#sub-accordion" href="#&lt;?=$newrow['subid']?&gt;"&gt;
                    &lt;?=$subtitle?&gt;
                  &lt;/a&gt;
                &lt;/h4&gt;
              &lt;/div&gt;
              &lt;div id="&lt;?=$newrow['subid']?&gt;" class="panel-collapse panel"&gt;
                &lt;div class="panel-body"&gt;
                &lt;!-- contents go here --&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;
            &lt;?php endforeach; ?&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;?php
        $i++; 
        endforeach; ?&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>So, now my questions for the web dev people with HTML PHP and Bootstrap exp.:</p>
  
  <ol>
  <li><s>Is it possible to nest foreach loops in this fashion without falling back to echo statements to print out the HTML (because ew)? Will it actually create (repeat) the HTML for the accordion objects this way or do I need to modify something here?</s></li>
  <li><s>What might be triggering the HTML 500?</s></li>
  </ol>
  
  <p>I realize this is a tough question to answer without live working code to mess with. If anyone knows a good resource to quickly sandbox a full stack for demo purposes I would be glad to know of it so I can put up some "working" code.</p>
  
  <p><strong>EDIT 1:</strong> User Sean pointed out that the sub-accordion is in a loop. Added in an iterator that modifies this id as the goal is to have multiple sub-accordions, not with the same id.</p>
  
  <p><strong>EDIT 2:</strong> Might have solved my own questions:<br>
   1. Turns out I used the wrong method when retrieving the ajax request: used PHP's $_POST['var'] instead of $_GET['var']<br>
   2. There was one syntax error on one of my shorthand PHP tags.</p>
  
  <p>Now things are showing up! But, the formatting is still wrong. I can deal with that. Thank you all for all your help!</p>