Get articles from different tables (subcategories)

4 posts by 2 authors in: Forums > CMS Builder
Last Post: September 18, 2013   (RSS)

By Jesus - September 18, 2013

Hello,

Here's another issue I need to get solved... will try my best to explain this properly.

I've a products category with 3 articles on it. Each article represents one product category.

Each product category has their own table (subcategories).

I want my detail page on products to display the article list for each particular subcategory.

Something like:

/products/category-one/

/products/category-two/

/products/category-three/

Where on category-one file I'll display the article list from sub-category-one, on category-two file I'll display the article list from sub-category-two and on category-three file I'll display the article list from sub-category-three. 

The thing here its that I need an IF statement on my detail page on products so based on which category I'm it will display the proper subcategory list.

I hope I explain this properly and it is clear. Thanks in advance for your help!

Jesus

By Jesus - September 18, 2013

I found a way to do this, I don't know if that's the best possible way to do it, but it works for me :)

On my products table, I create a list field with 4 options (select). Those options are SUB1, SUB2, SUB3, and SUB4 (you can name it in any way, just be sure to use the same name on the IF Statement below.

Then on my detail page I use this code:

                        <h2>Products</h2>
                        <ul class='left_navigation'>
                         <?PHP if ($productsRecord['category'] == "SUB1"): ?>
                         <?php foreach ($subcategory1Records as $record): ?>
                             <li class="page_item page-item-54"><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a><div></div></li>
                         <?php endforeach ?>
                         <?PHP endif ?>

                         <?PHP if ($productsRecord['category'] == "SUB2"): ?>
                         <?php foreach ($subcategory2Records as $record): ?>
                             <li class="page_item page-item-54"><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a><div></div></li>
                         <?php endforeach ?>
                         <?PHP endif ?>

                         <?PHP if ($productsRecord['category'] == "SUB3"): ?>
                         <?php foreach ($subcategory3Records as $record): ?>
                             <li class="page_item page-item-54"><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a><div></div></li>
                         <?php endforeach ?>
                         <?PHP endif ?>

                         <?PHP if ($productsRecord['category'] == "SUB4"): ?>
                         <?php foreach ($subcategory4Records as $record): ?>
                             <li class="page_item page-item-54"><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a><div></div></li>
                         <?php endforeach ?>
                         <?PHP endif ?>
                        
                        </ul>

This worked fine for what I'm looking for. If anyone knows a better way to do this, it will be great if he can share it! :)

Jesus

By Jason - September 18, 2013

Hi Jesus,

That solution would definitely work.  The only issue I can see is that on any given page, you would load at least 3 record sets that would never be used.  To help keep the code clean, what you can do is use an if statement to figure out which table you should be getting records from and only load a single record set.

NOTE:  In this example I'm assuming that your tables are called sub1, sub2, etc

if ($productsRecord['category'] == "SUB1") {
    $tableName = 'sub1';
  }
  elseif ($productsRecord['category'] == "SUB2") {
    $tableName = 'sub2';
  }
  elseif ($productsRecord['category'] == "SUB3") {
    $tableName = 'sub3';
  }
  else {
    $tableName = 'sub4';
  }
  
  list($subcategoryRecords, $subMetaData) = getRecords(array(
    'tableName'    =>  $tableName,
    'allowSearch'  =>  false,
  ));

This way, in your code you can always just output $subcategoryRecords without having to worry about which record set you need.

Hope this helps

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/