Displaying array contents / multi lists

3 posts by 2 authors in: Forums > CMS Builder
Last Post: March 28, 2022   (RSS)

By rez - March 25, 2022 - edited: March 26, 2022

I have a multi editor named images. Each record in the editor is an upload of an image. There is also a multi select list (checkboxes) using value of num and label of title coming from the tags editor

I have a multi editor named tags. Each record of the editor is a title of a tag name

I am displaying a grid of images for a portfolio.

I will create buttons above this grid / gallery using the tag names. These will filter the images (ads, photography, print, merch)

Typical portfolio gallery, right?

I'm trying to wrap my brain around this array stuff to be able to display the tags for now. Once that works, I can turn them into css  classes for filtering out photos.\

I  THINK I'm almost there? I can't get the tag names to display which I thought would be the easy part if I understand the rest of this right.

  list($case_studiesRecords, $case_studiesMetaData) = getRecords(array(
    'tableName'   => 'case_studies',
    'where'       => whereRecordNumberInUrl(0),
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1',
  ));
  $case_studiesRecord = @$case_studiesRecords[0]; // get first record


  if ($case_studiesRecord):
    list($imageRecords, $imageMetaData) = getRecords(array(
      'tableName'   => 'image',
      'where' => mysql_escapef('the_client = ?', $case_studiesRecord['the_client']), 
      'loadUploads' => true,
      'allowSearch' => false,
    ));
  endif;

  list($tagsRecords, $tagsMetaData) = getRecords(array(
    'tableName'   => 'tags',
    'loadUploads' => true,
    'allowSearch' => false,
  ));
$tagsByNum = array_groupBy($tagsRecords, 'num');
//showme( $tagsByNum );



<?php foreach ($imageRecords as $record):?>
  <?php	$assignedTags = listValues_unpack($record['tags']);?>
  <?php foreach ($assignedTags as $key => $value) : ?>
    <?php if (array_key_exists($value, $tagsByNum) !== false): ?>
      <?php echo htmlencode ($assignedTags [$key]['title']) ?> //illegal string offset 'title'
<?php echo htmlencode ($assignedTags [$value]['title']) ?> // Notice: Trying to access array offset on value of type null 
<?php echo htmlencode ($assignedTags [$value]) ?> // Undefined offset: 11 , 13, etc. These tag num's seem right.
    <?php endif ?>
  <?php endforeach ?>
<?php endforeach ?>




By daniel - March 28, 2022

Hi rez,

Congrats on getting it working yourself! I can answer your specific questions:

1. I don't understand the $key => $value though. What does this say in English? When I look at what is inside these variables, they are the same. Can you explain using this line and why? This foreach is creating ANOTHER array?

foreach($array as $key => $value) is a way to, for lack of a better way to put it, access both the key and value from an array. The other common syntax - e.g. foreach($records as $record) - is essentially the same but only accessing the value portion. You'll see them variably throughout the code depending on which data is needed. Here's some documentation on arrays and foreach loops that will be a lot more thorough than I can be:

In your specific case, the values are the same because this is how the list was set up; for a CMSB listValues array the $key refers to the "saved" value, and $value refers to the "display" label. They may be the same, but they may also be different, depending on the list setup.

2. The if statement is checking if the $value exists in tagsByNum? Why? All of the tags are in tagsByNum, so we know it's in there. Therefore, I eliminated this if statement and it still works fine. I must have misunderstood the post where I got this? When would I use array_search or array_key_exists? 

If the list field's options are changed after records have been created, it can be possible for a record to have a value assigned that no longer "exists" in the $tagsByNum array - which is what this is checking for.

3. I see in other posts where you are wrapping this foreach in isset(). I don't seem to be getting offset errors. When would I use isset()? I am thinking I can skip it because i require the tags by admins. Therefore, there must not be any empty variables in the array. Is that why I don't need it here or when do you use it?

It's entirely dependent on your code, but typically these types of checks are added to safeguard against edge cases and future code changes. Related to the previous answer, it's good practice to assume that any data or structures not explicitly defined in the current file could change at any point. If the check isn't causing any issues, I'd recommend leaving it in place.

Hope that helps!

Daniel
Technical Lead
interactivetools.com