Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
Sub-categories Tutorial

 

First page Previous page 1 2 3 4 5 Next page Last page  View All


Chris
Staff


Jan 22, 2010, 4:58 PM

Post #1 of 125 (40216 views)
Shortcut
Sub-categories Tutorial Can't Post

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:


Code
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)


Code
<?php header('Content-type: text/html; charset=utf-8'); ?> 
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
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 style="color: red; font-weight: bold">Rename this file to have a .php extension!</li><x */ ?>
<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!
Chris


(This post was edited by chris on Jan 26, 2010, 3:59 PM)


gkornbluth
Veteran

Jan 23, 2010, 12:41 PM

Post #2 of 125 (40192 views)
Shortcut
Re: [chris] Sub-categories Tutorial [In reply to] Can't Post

Chris,

Thanks for this much needed tutorial

Very clear and concise.

Best,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!
http://www.thecmsbcookbook.com


flamerz
User

Jan 25, 2010, 8:03 PM

Post #3 of 125 (40162 views)
Shortcut
Re: [chris] Sub-categories Tutorial [In reply to] Can't Post

this works great.

could be possible to have some identation inside the article pulldown (inside cms builder)?

for example, if we have fruits:apples and fruits:oranges.. the pulldown could show:

Code
fruits 
apples
oranges


instead of

fruits
apples
oranges

Note: i updated my entry form with the <?php echo $ESCAPED_FILTER_VALUE ?> trick.

nice way to populate a list from another.


(This post was edited by flamerz on Jan 25, 2010, 10:58 PM)


Chris
Staff


Jan 26, 2010, 2:19 PM

Post #4 of 125 (40144 views)
Shortcut
Re: [flamerz] Sub-categories Tutorial [In reply to] Can't Post

Hi flamerz,

Not quite, but you can achieve something similar by choosing:


Code
Use this field for option labels: breadcrumb


This will give you a pulldown with these options:


Code
fruits  
fruits : apples
fruits : oranges


I hope this helps! Please let me know if you have any questions.
Chris


(This post was edited by chris on Jan 27, 2010, 4:03 PM)


paulmac
User

Jan 27, 2010, 3:36 PM

Post #5 of 125 (40111 views)
Shortcut
Re: [chris] Sub-categories Tutorial [In reply to] Can't Post

Hi Chris

I can get this working to a point, but you may be able to give me some pointers or assistance with the following:

I want to set the menus up as following:

PRODUCTS
(when a user selects this link, they get taken to a new page with an overview of all their products and a list/menu of all the Product Categories:

This new page will display the following menu:

PRODUCT1
PRODUCT2
PRODUCT3
PRODUCT4
PRODUCT5

When the user selects PRODUCT2 from these options, they will be taken to a new page with an overview of PRODUCT2 and a menu like below:

PRODUCT1
PRODUCT2
product2_1
product2_2
product2_3
PRODUCT3
PRODUCT4
PRODUCT5


When the user selects product2_1 from these options, they will be taken to a new page with an overview of product_2 and a menu the same as the previous one.

Hope this all makes sense, I would appreciate any help with it.

Thanks.


Chris
Staff


Jan 27, 2010, 4:02 PM

Post #6 of 125 (40110 views)
Shortcut
Re: [paulmac] Sub-categories Tutorial [In reply to] Can't Post

Hi paulmac,

Does this help? Try adding this line (in red):


Code
  list($categoriesRecords, $selectedCategory) = getCategories(array( 
'tableName' => 'categories',
'categoryFormat' => 'onelevel',
));


I hope this helps! Please let me know if you have any questions.
Chris


(This post was edited by chris on Jan 27, 2010, 4:04 PM)


paulmac
User

Jan 27, 2010, 4:54 PM

Post #7 of 125 (40103 views)
Shortcut
Re: [chris] Sub-categories Tutorial [In reply to] Can't Post

Hi Chris

Thanks for getting back so quickly. I think I'm edging closer to the solution, but a combination of my poor php skills and not quite understanding how the Viewer URLS work is holding me back I think.

Whe I complete the tutorial and create the page articleList.php, I have a page displaying the following:

Categories
•(All Articles)
•Dogs
•Cats

Articles - List Page Viewer
Chihuahuas Article
Cat Article 1
Dog Article 1

If I click on Dogs the following appears on the page, replacing what was previously on articlesList.php:

Categories
•(All Articles)
•Dogs
&#9702;Chihuahuas
•Cats

Articles - List Page Viewer
Dog Article 1

What I want to happen is when I click on Dogs, the user gets taken to a new page with an overview about Dogs and a menu like below:

•Dogs
&#9702;Chihuahuas
•Cats

I feel like I'm dragging this out, but as I say I'm quite inexpereinced with both php and CMSB.

Thanks again for your help.


Chris
Staff


Jan 28, 2010, 12:03 PM

Post #8 of 125 (40082 views)
Shortcut
Re: [paulmac] Sub-categories Tutorial [In reply to] Can't Post

Hi paulmac,

For simplicity, the tutorial is only concerned with a single viewer. If you need to specialize the behaviour of your pages depending on whether or not a category is selected, (e.g. removing the "(All Articles)" link,) you can either use separate viewers or check $selectedCategory (note that if there is no selected category, the variable will be an empty array, see below for an example.)

Displaying an overview of your category can be accomplished with the Category section's "content" wysiwyg field:


Code
  <?php if (!empty($selectedCategory)): ?> 
Selected Category: <?php echo $selectedCategory['name'] ?><br />
<?php echo $selectedCategory['content'] ?><br />
<?php else ?>
No Category Selected.<br />
<?php endif ?>


I hope this helps! Please let me know if you have any questions.
Chris


(This post was edited by chris on Jan 28, 2010, 12:04 PM)


Dan Maitland
User

Mar 30, 2010, 5:29 AM

Post #9 of 125 (38243 views)
Shortcut
Re: [chris] Sub-categories Tutorial [In reply to] Can't Post

Are you able to bring in any images that are related to the article as well or just text? If you can bring in imagesthen what is the difference between Categories, subcategories and the relatedrecordslookup plugin?


Chris
Staff


Mar 30, 2010, 4:20 PM

Post #10 of 125 (38215 views)
Shortcut
Re: [Dan Maitland] Sub-categories Tutorial [In reply to] Can't Post

Hi Dan,

Categories and Sub-categories are the same thing, really. They can have uploaded images, yes, and getCategories will load them.

The relatedrecordslookup plugin is something completely different.
Chris


svsanchez
User

May 31, 2010, 1:30 PM

Post #11 of 125 (35615 views)
Shortcut
Re: [chris] Sub-categories Tutorial [In reply to] Can't Post

Hello, thank you for this excellent post, I would like to know how to do the following:

My customer needs his site to show the MAIN category on a column, then subcategories from the selected MAIN category on a second column, and lastly products from she selected subcategory on the third column, like this:

MAIN CATEGORY 1 | Subcat 1 from selected main Category | List of products
MAIN CATEGORY 2 | Subcat 2 from selected main Category | from selected subcat
MAIN CATEGORY 3 | Subcat 3 from selected main Category |
| Subcategory 4 from selected main Category |
| Subcategory 5 from selected main Category |

I know how to show only the MAIN CATEGORY with the 'categoryFormat' => 'onelevel', tag, but how do I only show the Subcategories from a selected Category?


(This post was edited by svsanchez on May 31, 2010, 1:35 PM)


Jason
Staff / Moderator


Jun 1, 2010, 2:09 PM

Post #12 of 125 (35545 views)
Shortcut
Re: [svsanchez] Sub-categories Tutorial [In reply to] Can't Post

Hi,

If you could attach the .php file you're currently working with, I can take a closer look at this for you.

Thanks.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

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


svsanchez
User

Jun 1, 2010, 8:59 PM

Post #13 of 125 (35531 views)
Shortcut
Re: [Jason] Sub-categories Tutorial [In reply to] Can't Post

Hello Jason, thank you for your reply. For the moment I have only done what is explained on your tutorial (which I have pasted below) but what I need to achieve is in the following URL (in yellow my comments): www.casahermes.com/new/cms.htm

For the code I have as of now, as I said it's only the one from your example with the difference that I used "twolevel" instead of "onelevel" to see if it would show me only the subcategories but of course this didn't work :(

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';




$dirsToCheck = array('/home/casahem/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records

list($categoriesRecords, $selectedCategory) = getCategories(array( 'tableName' => 'categories', 'categoryFormat' => 'twolevel', ));
list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',

));

?>


<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): ?>

<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 -->


Jason
Staff / Moderator


Jun 2, 2010, 11:56 AM

Post #14 of 125 (34931 views)
Shortcut
Re: [svsanchez] Sub-categories Tutorial [In reply to] Can't Post

Hi,

What we need to do is output our categories in two different loops: one for the top level, one for the second level.

For the first level, you can use a loop like this:


Code
<?php $selectedCat=""; ?> 
<?php foreach($categoryRecords as $category):?>
<?php if(!$category['_hasParent']):?>
<?php echo $category['_listItemStart']; ?>
<?php if($category['_isSelected']){echo "<b>"; $selectedCat=$category['num'];} ?>
<a href="?category=<?php echo $category['num']?>"><?php echo $category['name']; ?></a>
<?php if($category['_isSelected']){echo "</b>";} ?>
<?php echo $category['_listItemEnd']; ?>
<?php endif ?>
<?php endforeach ?>


This will only output categories that do not have a parent (top level).

The second column would be outputted like this:


Code
<?php if($selectedCat): ?> 
<?php foreach($categoryRecords as $category): ?>
<?php if ($selectedCat==$category['parentNum']):?>
<?php echo $category['_listItemStart']; ?>
<?php echo $category['name']; ?>
<?php echo $category['_listItemEnd']; ?>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>


This will only output if a top level category has been selected. And then will only output categories whose parent was selected.

You may need to change some names to match what you have in the database. You will also need to add an <a> tag around the second column items.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

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


svsanchez
User

Jun 2, 2010, 2:45 PM

Post #15 of 125 (34919 views)
Shortcut
Re: [Jason] Sub-categories Tutorial [In reply to] Can't Post

Hello Jason, thank you for your help. I tried the code you sent me but I am getting errors, I am sure this is because I have to change the names but don't know where and what to put. Here's the URL of the page I inserted your code in so you can see the errors I am getting. I inserted exactly the code you sent me, the first code on the first column and the second code on the second column:

http://www.casahermes.com/techos.php


Jason
Staff / Moderator


Jun 2, 2010, 2:51 PM

Post #16 of 125 (34915 views)
Shortcut
Re: [svsanchez] Sub-categories Tutorial [In reply to] Can't Post

Hi,

The easiest change would be to change where you`re selecting your category records.

Try changing this:

Code
list($categoriesRecords, $selectedCategory) = getCategories(array( 'tableName' => 'categories', 'categoryFormat' => 'twolevel', ));

to this:

Code
list($categoryRecords, $selectedCategory) = getCategories(array( 'tableName' => 'categories', 'categoryFormat' => 'twolevel', ));


That should take care of the error for you.
Let me know if you run into any other problems.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

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


svsanchez
User

Jun 3, 2010, 1:32 PM

Post #17 of 125 (34862 views)
Shortcut
Re: [Jason] Sub-categories Tutorial [In reply to] Can't Post

Hello Chris, you're the man! This is excellent!

Can you tell me how to remove the bulleted list (I would like to use a little image to the left of each menu item instead of bullets!).


Jason
Staff / Moderator


Jun 3, 2010, 1:40 PM

Post #18 of 125 (34860 views)
Shortcut
Re: [svsanchez] Sub-categories Tutorial [In reply to] Can't Post

Hi,

Glad to hear that's working out for you.

There are two ways to get an image there.

1) you can use CSS to set the background image of the list tag
example:

Code
li 
{
padding-left: 10px;
background-image: url(images/arrow.gif);
background-repeat: no-repeat;
background-position: 0 .5em;
}


Or, you can remove the code that looks like this:

Code
<?php echo $category['_listItemEnd']; ?>


and replace:


Code
<?php echo $category['_listItemStart']; ?>

with some kind of <img> tag.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

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


svsanchez
User

Jun 3, 2010, 2:01 PM

Post #19 of 125 (34859 views)
Shortcut
Re: [Jason] Sub-categories Tutorial [In reply to] Can't Post

Sorry Jason, I don't know why I wrote "chris" on my last post... what you just suggested worked as a marvel, many thanks for your excellent support!!!! It is very de-stressing to know we can count on you guys!


svsanchez
User

Jun 8, 2010, 11:23 AM

Post #20 of 125 (33669 views)
Shortcut
Re: [svsanchez] Sub-categories Tutorial [In reply to] Can't Post

Hello, I am encountering a new problem: when I select a sub menu, it will show me the products inside but the sub menu disappears. If you can go to www.casahermes.com/techos.php you will see your code in action in the first and second columns. If you click on the first column menu on "techos" you get the second column menu with 3 options:

- Fibrocemento
- Metálicos
- Plasticos

The Fibrocemento option has a few test products inside, please click on it and you will see them displayed on the 3rd column, but the sub menu disappears!


Chris
Staff


Jun 8, 2010, 4:21 PM

Post #21 of 125 (33655 views)
Shortcut
Re: [svsanchez] Sub-categories Tutorial [In reply to] Can't Post

Hi svsanchez,

I'm getting a 500 server error at www.casahermes.com/techos.php
Chris


svsanchez
User

Jun 8, 2010, 7:04 PM

Post #22 of 125 (33647 views)
Shortcut
Re: [chris] Sub-categories Tutorial [In reply to] Can't Post

Hello Chris, sorry for that, there was a spike on the load of the server but it is working now!


Jason
Staff / Moderator


Jun 9, 2010, 9:36 AM

Post #23 of 125 (33595 views)
Shortcut
Re: [svsanchez] Sub-categories Tutorial [In reply to] Can't Post

Hi,

I took a look at this, and I think I found our error. We were only displaying a second column when a top level category was selected. As soon as you select a category from the second column, there is no longer a top level category that is selected. We can get around this by changing the variable ['isSelected'] to ['isSelectedBranch']. So our code to display our top level will look like this: (NOTE: I've highlighted the changes in red)


Code
<?php $selectedCat=""; ?>  
<?php foreach($categoryRecords as $category):?>
<?php if(!$category['_hasParent']):?>
<?php echo $category['_listItemStart']; ?>
<?php if($category['_isSelectedBranch']){echo "<b>"; $selectedCat=$category['num'];} ?>
<a href="?category=<?php echo $category['num']?>"><?php echo $category['name']; ?></a>
<?php if($category['_isSelectedBranch']){echo "</b>";} ?>
<?php echo $category['_listItemEnd']; ?>
<?php endif ?>
<?php endforeach ?>


That should be all that's required to fix this. Give it a try and let me know.

Thanks.
---------------------------------------------------
Jason Sauchuk - Programmer 
interactivetools.com

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


svsanchez
User

Jun 9, 2010, 11:32 AM

Post #24 of 125 (33590 views)
Shortcut
Re: [Jason] Sub-categories Tutorial [In reply to] Can't Post

Chris, as always thank you very much for your excellent support, this did the trick!


svsanchez
User

Jul 19, 2010, 1:56 PM

Post #25 of 125 (28995 views)
Shortcut
Re: [svsanchez] Sub-categories Tutorial [In reply to] Can't Post

Hello Chris, how can I put in BOLD the selected SUB category in the code you sent me above?

First page Previous page 1 2 3 4 5 Next page Last page  View All