0

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.

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.

I say this because I have a fairly complicated arrangement I'm attempting to make.

A sample:

<?php
  // db connection info goes here
  
  // pgsql query info goes here
  $i = 0;
  $result = pg_fetch_all($getData);
?>
<!-- Starting the container accordion -->
<div class="panel-group" id="main-accordion">
  <?php
    foreach($result as $row):
      $title1 = $row['title1'];
      $title2 = $row['title2'];
  ?>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#main-accordion" href="#<?=$row['id']?>">
          (<?=$row['category']?>) <?=$title1?> - <?=$title2?>
        </a>
      </h4>
    </div>
    <div id="<?=$row['id']?>" class="panel-collapse collapse">
      <!-- The body of the accordion, contents go here. -->
      <div class="panel-body">
        <?php 
          // another pgsql query here
          
          $newresult = pg_fetch_all($newgetData);
        ?>
        <!-- In the accordion body, a new group of accordions. This is doable if hardcoded -->
        <div class="panel-group" id="sub-accordion-<?=$i?>">
          <?php
            // I think this is where the issues start??
            foreach($newresult as $newrow):
              $subtitle = $newrow['subtitle'];
          ?>
          <div class="panel panel-default">
            <div class="panel-heading">
              <h4 class="panel-title">
                <a data toggle="collapse" data-parent="#sub-accordion" href="#<?=$newrow['subid']?>">
                  <?=$subtitle?>
                </a>
              </h4>
            </div>
            <div id="<?=$newrow['subid']?>" class="panel-collapse panel">
              <div class="panel-body">
              <!-- contents go here -->
              </div>
            </div>
          </div>
          <?php endforeach; ?>
        </div>
      </div>
    </div>
    <?php
      $i++; 
      endforeach; ?>
  </div>
</div>

So, now my questions for the web dev people with HTML PHP and Bootstrap exp.:

  1. 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?
  2. What might be triggering the HTML 500?

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.

EDIT 1: 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.

EDIT 2: Might have solved my own questions:
1. Turns out I used the wrong method when retrieving the ajax request: used PHP's $_POST['var'] instead of $_GET['var']
2. There was one syntax error on one of my shorthand PHP tags.

Now things are showing up! But, the formatting is still wrong. I can deal with that. Thank you all for all your help!

psosuna
  • 113
  • 1
  • 5
  • side note - you have `id="sub-accordion"` in a loop, which means you will have `count($result)` of the same `id`. – Sean Dec 15 '17 at 22:13
  • @Sean thanks for pointing this out. my actual code has iterators that +1 every time the loop runs, and that also affects the id def on all references. I should probably add this in into this example to avoid confusion. – psosuna Dec 15 '17 at 22:16
  • if you are getting a 500 error, have you checked your logs for an error message? have you tried turining on [error_reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display/21429652#21429652)? – Sean Dec 15 '17 at 22:20
  • @Sean error_reporting is on. Part of the difficulty of getting an output is that this page that populates these accordions is a result to an ajax xhr request. It is returning this 500 error, and I can't see the PHP error_reporting in the browser because it doesn't return it and because running this page directly with no data returns a blank page – psosuna Dec 15 '17 at 22:22
  • 2
    if you get err500 I'd suggest to first loop both your queries and build asoc array of that, dump it to see if all is cool and then build accordian spagety code.. – Kresimir Pendic Dec 15 '17 at 22:23
  • Without an error message, it is difficult for us to guess what is happening with partial code. If this is coming from an ajax call, the error message might be in the browser console, as the ajax may not be to know what to due with data returned that it is not expecting – Sean Dec 15 '17 at 22:31
  • The ajax is expecting an HTML response. I've tested the ajax call before and it does work on simpler code. However, I'm having issues getting any information past an HTML 500... I would be glad to post the ajax as well if necessary. – psosuna Dec 15 '17 at 22:38
  • Some additional context: The page that calls to populate has a dropdown menu. The selection on it triggers the ajax call which loads a PHP file with similar content to this (literally same code with var names changed). The HTML that results deposits onto a div on the same page as the dropdown... but this particular snippet of code does not load. – psosuna Dec 15 '17 at 22:41
  • @KresimirPendic I'll be trying that now. I'm thinking something is out of scope (maybe my divs are not well organized) but I wanted to run it by some people other than me to see if someone had a better idea than me – psosuna Dec 15 '17 at 22:43

1 Answers1

0

In your PHP.ini short tags might be turned off. In that case you either turned on or if you have no access to PHP.ini, then you should not use short echo tags `, try changing it to

Gopakumar Gopalan
  • 1,137
  • 14
  • 22
  • I have access to php.ini. The actual code of this is in a localhost sandbox on my machine. Short tags are on. – psosuna Dec 15 '17 at 22:25