filtering record by type

2 posts by 2 authors in: Forums > CMS Builder
Last Post: November 26, 2018   (RSS)

By Dave - November 26, 2018

Hi petrogus, 

How about this? 

<?php
  list($articleRecords, $articleMetaData) = getRecords(array(
    'tableName'   => 'myrecords',  
    'loadUploads' => true,
    'allowSearch' => false,
  ));  
  
  list($articleTypesRecords, $articleTypesMetaData) = getRecords(array(
    'tableName'   => 'rec_type',
    'loadUploads' => true,
    'allowSearch' => false,
  ));  

?>

<div class="tabs">
	<?php foreach($articleTypesRecords as $articleType) { ?>
        <div class="tab-item <?php if ($articleType['num'] == 1) { echo "tab-active"; } ?>">
          	<a class="tab-label <?php if ($articleType['num'] == 1) { echo "active-btn"; } ?>" data-url="<?php echo $articleType['num']; ?>"><?php echo htmlencode($articleType['title_'.$_SESSION['lang']]); ?></a>
			<div class="tab-content">
				<?php
          foreach($articleRecords as $articleItem) {
            if ($articleItem['rec_type'] != $articleType['num']) { continue; } // skip articles not in this tab
        ?>
          <article>
            <h3><a href="sample.php"><?php echo $articleItem['title']; ?></a></h3>
            <a href="blog.php?article=<?php echo $articleItem['num']; ?>">Read more</a>  
          </article>
				<?php } ?>
			</div>  
		</div>
	<?php } ?>	
</div>

Your code is looping over $articleTypesRecords to display each tab, then I updated the inner foreach that loops over $articleRecords so it only shows the article if $articleItem['rec_type'] equals the $articleType['num'] for the current tab.

Let me know if that works for you.

Dave Edis - Senior Developer

interactivetools.com