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 rez - February 5, 2020 - edited: February 5, 2020

Oh. I used categories and the snippets i have from a long time ago are more complicated. Just getting back into CMSB again. 

So all my pages with categories were lists like:

'where' => "category LIKE '%\t{$selectedCategory['num']}\t%'",

That type of thing, looping out results, and some array tricks I have saved has always given me pretty much everything I needed for things like a restaurant menu. I may make some new posts of code I have been using and you cant tell me if it's still efficient or anything has changed? 

Continuing what you showed me here,  without categories, it's odd that when I search <?php echo $label; ?>, there are no results to see what others are doing. What if I changed my list code to show the whole client table on one page and each showed their services? Is that getting into multiple arrays? Once I see it, I can usually adjust for what I need. 

Client 1

Service 1

Service 2

Client 2 

Service 1

Service 2

Service 3 

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.