multi select display / checkboxes

6 posts by 2 authors in: Forums > CMS Builder
Last Post: February 7, 2020   (RSS)

By rez - February 4, 2020 - edited: February 4, 2020

I have a detail page casestudies.php?1  that shows a client. In the client editor / table is a multi list of checkboxes for services performed for that client coming from a services editor / table.

  list($clientRecords, $clientMetaData) = getRecords(array(
    'tableName'   => 'client',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));

  list($servicesRecords, $servicesMetaData) = getRecords(array(
    'tableName'   => 'services',		
    'loadUploads' => true,
    'allowSearch' => false,
  ));

so on casestudies.php?1 I can show all of the checked services (from the code generator):

Services (labels): <?php echo join(', ', $clientRecord['services:labels']); ?><br>

That lists them all in one place separated by commas. How do I loop through this so I can show them in a <UL> for more control of the display?

By daniel - February 4, 2020

Hi rez,

I think something like this should work:

<ul>
<?php foreach ($clientRecord['services:labels'] as $label): ?>
  <li><?php echo $label; ?></li>
<?php endforeach; ?>
</ul>

Let me know if that does the trick or if you have any other questions.

Thanks!

Daniel
Technical Lead
interactivetools.com

By rez - February 7, 2020 - edited: February 7, 2020

This seems to work and it's simple. I found this "continue" line of code here and threw the "true" in there since it's a text box. ? Is this efficient / recommended? Output looks right to me.

  // load record from 'client'
  list($clientRecords, $clientMetaData) = getRecords(array(
    'tableName'   => 'client',
    'loadUploads' => true,
    'allowSearch' => false,
  ));

  // load records from 'services'
  list($servicesRecords, $servicesMetaData) = getRecords(array(
    'tableName'   => 'services',
    'loadUploads' => true,
    'allowSearch' => false,
  ));


						<?php foreach ($clientRecords as $record): //for every client ?> 
							<h5><?php echo htmlencode($record['title']) // show their title?></h5>
								<ul>
									<?php foreach ($record['services:labels'] as $label): // and loop through assigned service names?>
										<?php if ($record['services:values'] != true) { continue; } // skip services that aren't selected ?>
  										<li><?php echo $label; // show name of service not skipped?></li>
									<?php endforeach; ?>
								</ul>
						<?php endforeach; ?>

By daniel - February 7, 2020 - edited: February 7, 2020

Hi Rez,

That looks pretty good!

I think you can remove the line with the "continue" however, since $record['services:labels'] should already only contain services selected by that client record. So, you can try removing that line to see if you still get the same result. 

Cheers,

Daniel
Technical Lead
interactivetools.com

By rez - February 7, 2020

*facepalm*. I understand. Yes, that works well. Actually, this all means I don't even need the services list at the top. Thanks.