Let's say you want to organize a bunch of articles into categories and sub-categories. In the end, you want a list of all your categories displayed on your site, and when users click on these categories, they'll see a list of all the articles belonging to that category. Of course, everything will also need to dynamically update as you change your categories and articles from within CMS Builder.
Firstly, we'll need two sections.
In Admin > Section Editor, create a Multi Record section called "Articles".
Create another section, selecting "Category Menu" as the menu type under <Advanced Menus>; call this menu Categories.
Next, we'll add a "category" field to the Articles section to associate articles with categories.
Click "modify" next to your Articles section, then click Add Field.
Type a Field Label of "Category" and select a Field Type of "list".
Under Field Options, change List Options from "Use options listed below" to "Get options from database (advanced)".
Under that, three new fields will appear. Set Section Tablename to "categories", "Use this field for option values" to "num", and "Use this field for option labels" to "name".
Your field should now be configured like this:
Field Label: Category
Field Name: category
Field Type: list
Field Options
Display As: pulldown
List Options: Get options from database (advanced)
Section Tablename: categories
Use this field for option values: num
Use this field for option labels: name
Now Save your field, and let's add it to the ListPage Fields before leaving the Section Editor.
Change the ListPage Fields to: "dragSortOrder, category, title", then click Save Details.
On the left side of the screen, you should see your new sections: Articles and Categories. Let's add some content now so we'll know we have everything working as we write our viewers.
Click Categories, then Create.
Ignore the Content field for now, just set the Name field to "Dogs", then save it.
Create a second category called "Cats". Now create a third category called "Chihuahuas", but set its Parent Category to "Dogs".
When you save, you'll be able to see that you've created a subcategory.
Now let's add some articles.
Click Articles, then Create.
Enter "Dog Article 1" as the Title and "Dogs" as the Category.
Create two more articles -- one for each of your categories.
It's time to get our fingers dirty.
Click Admin > Code Generator.
Select Section "Articles" and Viewer Type "List page", then click Show Code.
Copy and paste the resulting code into a text editor (notepad will suffice) and save it as articlesList.php.
Let's make a few changes: (new code in red, removed code struck-out)
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
require_once "/my/path/to/viewer_functions.php";
list($categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'categories',
));
list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<style type="text/css">
body { font-family: arial; }
.instructions { border: 3px solid #000; background-color: #EEE; padding: 10px; text-align: left; margin: 25px}
</style>
</head>
<body>
<!-- INSTRUCTIONS -->
<div class="instructions">
<b>Sample List Viewer - Instructions:</b>
<ol>
<?php ?>
<li><b>Remove any fields you don't want displayed.</b> (Most list pages only have title and link fields.)</li>
<li>Rearrange remaining fields to suit your needs.</li>
<li>Copy and paste code into previously designed page (or add design to this page).</li>
</ol>
</div>
<!-- /INSTRUCTIONS -->
<h1>Categories</h1>
<ul>
<li><a href="?">(All Articles)</a></li>
<?php foreach ($categoriesRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?><b><?php endif ?>
<a href="?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach ?>
</ul>
<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
<h1>Articles - List Page Viewer</h1>
<?php foreach ($articlesRecords as $record): ?>
Record Number: <?php echo $record['num'] ?><br/>
Title: <?php echo $record['title'] ?><br/>
Content: <?php echo $record['content'] ?><br/>
Category: <?php echo $record['category'] ?><br/>
_link : <a href="<?php echo $record['_link'] ?>"><?php echo $record['_link'] ?></a><br/>
<hr/>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br/>
<?php endforeach ?>
<?php if (!$articlesRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
<!-- /STEP2: Display Records -->
</body>
</html>
Now you should be able to click between the different categories and see the correct articles show up. The magic is in getRecords() -- it automatically filters results based on the query string. I hope this helps you get started with categories! Please let me know if you have any questions!
All the best,
Chris