filtering record by type

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

By petrogus - November 19, 2018

Hi Everyone,

I would to filter 'myrecords' base on articleTypesRecords between tabs. I have two types of records and i want to filter at each tab - type.

At the moment 'myrecords' load at both tabs, could you help me please ?!

<?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) { $articleType = mysql_select("rec_type", "num = '".$articleItem['rec_type']."'"); $articleType = @$articleType[0]; ?>
				<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>
PetroGus

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