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 rez - March 26, 2022 - edited: March 26, 2022

						
<?php $tagsByNum = array_groupBy($tagsRecords, 'num'); // group all tags by num to check for project matches later
  //showme( $tagsByNum ); ?>
<?php foreach ($imageRecords as $record): // already filtered in header to contain only this project's record ?>
  <?php	$assignedTags = listValues_unpack($record['tags']); 	// create arrays of tags that were assigned with checkboxes for this record. Each array contains all tag record info
    //showme($assignedTags);?>

    <?php foreach ($assignedTags as $key => $value): //loop through each tag array for current image?>
  	<?php if (array_key_exists($value, $tagsByNum)) : ?>
  	    <?php echo $tagsByNum [$value] ['title']; ?>
 	 <?php endif ?>
     <?php endforeach ?>
    <?php endforeach ?>

Through many var_dump and showme's, I have it working. I actually have the filters working now as well. I still get confused looking back.

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?

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? 

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?