Bootstrap Collapse

5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 17, 2016   (RSS)

By Damon - August 17, 2016

Hi,

It looks like the reason everything is open is because of "in".

<div id="'collapse'+'<?php echo htmlencode($record['num']) ?>'" class="panel-collapse collapse in">

This sets it as open so should only apply to the first record.

Here is some code using a $counter. If the $counter equals one, then I set a variable to open the Collapse menu and if it isn't one then I unset it.

Also, since I'm using a $counter anyway, I just use that in the IDs.

<?php $counter = 1; ?>
<?php foreach ($sample_multi_recordRecords as $record): ?>
 <?php if($counter ==1) { $defaultOpen = "in"; } ?>
 <div class="panel panel-default">
 <div class="panel-heading">
 <h4 class="panel-title">
 <a data-toggle="collapse" data-parent="#accordion" href="#collapse<?php echo $counter; ?>"><?php echo htmlencode($record['title']) ?></a>
 </h4>
 </div>
 <div id="collapse<?php echo $counter; ?>" class="panel-collapse collapse <?php echo @$defaultOpen; ?>">
 <div class="panel-body"><?php echo $record['content']; ?></div>
 </div>
 </div>
 <?php $defaultOpen = ""; ?>
 <?php $counter++; ?>
<?php endforeach; ?>

Hopefully this helps!

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By northernpenguin - August 17, 2016

Damon:  That worked great, except that the default has all the accordions open.  Is there anyway to set them such that the first one is open and the others closed ?

--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

By Damon - August 17, 2016

Hi,

My code works for me. Only the first accordian is open, the others closed by default.

Are you resetting the $defaultOpen variable at the end of the foreach loop?

<?php $defaultOpen = ""; ?>

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By northernpenguin - August 17, 2016

I must have inadvertently deleted  <?php $counter++; ?>

It works now.

Thanks Damon! 

--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke