Sub-categories Tutorial

134 posts by 17 authors in: Forums > CMS Builder
Last Post: August 7, 2012   (RSS)

By Chris - July 29, 2010

Hi svsanchez,

The problem here is that getCategories() is picking the selected category based on getLastNumberInUrl().

You'll want to supply the article's category to getCategories(), like this:

list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$articlesRecord = @$articlesRecords[0]; // get first record

list($categoryRecords, $selectedCategory) = getCategories(array(
'tableName' => 'categories',
'selectedCategoryNum' => @$articlesRecord['category'],
'categoryFormat' => 'twolevel',
));


Does that help? Please let me know if you have any questions.
All the best,
Chris

By Jason - August 3, 2010

Hi,

I'm not sure what could be causing this. If you want us to look into this issue further, we can work on it through out consulting services.

Go to www.interactivetools.com/hire-us/ and fill out the form there.

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/

Re: [chris] Sub-categories Tutorial

By csdesign - August 6, 2010

Hello!
Great instructions... I'm sure this is User Error... ;)
I have it up - but the articles are not showing when clicked on so I'm obviously missing something.

Here's my page: http://www.aubriericheson.com/recipes.php

I stripped everything back out. Here's the stripped own version:
http://www.aubriericheson.com/recipes2.php

I appear to have 3 problems:

1) when I click on a category - it does not bring up the articles (recipes) in that category (recipe_categories). Right now I have one 1 in breakfast, dinner and snacks.

2) when I click on a recipe by title - I get a view urls error. I'm still a little new to this so a push in the right direction on that would help.

3) I would like the number of recipes within each category to appear after the name. For instance:
• Breakfast (10)
• Lunch (3)

Thanks so much for your help!!! Tina


And here's the code to the stripped own version:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/a/u/b/aubrier/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($recipe_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'recipe_categories',
));

list($recipesRecords, $recipesMetaData) = getRecords(array(
'tableName' => 'recipes',
));

?>

<h1>Browse Recipes by Category</h1>
<ul>
<li><a href="?">(View All)</a></li>
<?php foreach ($recipe_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>Browse Recipes by Title</h1>
<?php foreach ($recipesRecords as $record): ?>

<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br/>

<?php endforeach ?>

<?php if (!$recipesRecords): ?>
No records were found!<br/><br/>
<?php endif ?>

<!-- /STEP2: Display Records -->

Re: [csdesign] Sub-categories Tutorial

By Chris - August 6, 2010

Hi csdesign,

1. You named your article's category field "recipe_categories" instead of "category". (Lucky guess!) You can either change the name of that field OR change this:

<a href="?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a>

...to this:

<a href="?recipe_categories=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a>

2. You'll need to create a Detail Page for your recipes. You can use the Code Generator to create one for you. After you have decided on a URL for it, go into the Section Editor, click the Viewer Urls tab, and enter the URL for your Detail Page.

3. First, add this code at the bottom of STEP 1 (but before the ?>):

$rows = mysql_query_fetch_all_array("SELECT c.num, COUNT(*) FROM {$TABLE_PREFIX}articles a JOIN {$TABLE_PREFIX}categories c ON a.category = c.num GROUP BY c.num");
$articleCountByCategoryNum = array_combine(array_pluck($rows, 0), array_pluck($rows, 1));


Note that you'll need to change the field name in red to match yours (recipe_categories.)

Next, add this (in red) to your display code:

<a href="?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?> (<?php echo $articleCountByCategoryNum[$categoryRecord['category']] ?>)</a>

Once again, you'll need to change both instances of "category" to "recipe_categories".

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

Re: [chris] Sub-categories Tutorial

By csdesign - August 6, 2010

oh... I see! it's calling the table name there. Thanks so much!!

Re: [csdesign] Sub-categories Tutorial

By csdesign - August 21, 2010

I'm getting close, but obviously, I'm still missing something. The Recipes page does come up with categories and all the right recipes list to the right of those categories, however, there are 2 errors with each category:

http://www.aubriericheson.com/recipes.php

ERROR:
Notice: Undefined index: recipe_category in /home/content/a/u/b/aubrier/html/recipes.php on line 56 Notice: Undefined variable: articleCountByCategoryNum in /home/content/a/u/b/aubrier/html/recipes.php on line 56 )

__________________________________________________

ON THE RECIPES.PHP PAGE, HERE'S STEP 1 AND STEP 2 CODE:



// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/a/u/b/aubrier/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($recipe_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'recipe_categories',
));

list($recipesRecords, $recipesMetaData) = getRecords(array(
'tableName' => 'recipes',
));

?>

STEP 2: (LINE 56 starts with <a href="?recipe_categories=..... )

<!--BEGIN PAGE CODE HERE-->

<table width="830" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top"> <h1>Browse Recipes by Category</h1>
<ul>
<li><a href="?">(View All)</a></li>
<?php foreach ($recipe_categoriesRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>

<?php if ($categoryRecord['_isSelected']): ?><b><?php endif ?>
<a href="?recipe_categories=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?> (<?php echo $articleCountByCategoryNum[$categoryRecord['recipe_categories']] ?>)</a>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach ?> s
</ul></td>
<td align="left" valign="top">&nbsp;</td>
<td align="left" valign="top">
<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
<h1>Browse Recipes by Title</h1>
<?php foreach ($recipesRecords as $record): ?>

<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br/>

<?php endforeach ?>

<?php if (!$recipesRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
<!-- /STEP2: Display Records -->

__________________________________________________

I added this to the recipeDetail.php page (not ending ?> tag for placement)

$rows = mysql_query_fetch_all_array("SELECT c.num, COUNT(*) FROM {$TABLE_PREFIX}articles a JOIN {$TABLE_PREFIX}categories c ON a.recipe_categories = c.num GROUP BY c.num");
$articleCountByCategoryNum = array_combine(array_pluck($rows, 0), array_pluck($rows, 1));
?>

I'm getting close, but obviously, I'm still missing something. The Recipes page does come up with categories and all the right recipes list to the right of those categories, however, there are 2 errors with each category:

http://www.aubriericheson.com/recipes.php

ERROR:
Notice: Undefined index: recipe_category in /home/content/a/u/b/aubrier/html/recipes.php on line 56 Notice: Undefined variable: articleCountByCategoryNum in /home/content/a/u/b/aubrier/html/recipes.php on line 56 )

__________________________________________________

ON THE RECIPES.PHP PAGE, HERE'S STEP 1 AND STEP 2 CODE:



// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/a/u/b/aubrier/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($recipe_categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'recipe_categories',
));

list($recipesRecords, $recipesMetaData) = getRecords(array(
'tableName' => 'recipes',
));

?>

STEP 2: (LINE 56 starts with <a href="?recipe_categories=..... )

<!--BEGIN PAGE CODE HERE-->

<table width="830" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top"> <h1>Browse Recipes by Category</h1>
<ul>
<li><a href="?">(View All)</a></li>
<?php foreach ($recipe_categoriesRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>

<?php if ($categoryRecord['_isSelected']): ?><b><?php endif ?>
<a href="?recipe_categories=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?> (<?php echo $articleCountByCategoryNum[$categoryRecord['recipe_categories']] ?>)</a>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach ?> s
</ul></td>
<td align="left" valign="top">&nbsp;</td>
<td align="left" valign="top">
<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
<h1>Browse Recipes by Title</h1>
<?php foreach ($recipesRecords as $record): ?>

<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br/>

<?php endforeach ?>

<?php if (!$recipesRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
<!-- /STEP2: Display Records -->

__________________________________________________


In the Admin > Section Editors > Recipes

General: Table name (cms_recipes)
Viewer URLs:
• list page url: recipes.php
• detail page url: recipeDetail.php

I did not add anything view urls to the Recipe Categories Editor.

__________________________________________________

http://www.aubriericheson.com/recipes.php

So on the main recipes page, it looks like it's trying to display the number of articles so when that works, that's perfect and each set of recipes is coming up - even with the errors.

when I click on a recipe in the right column, it's trying... I get the url with the title afterwards but get this error on a blank page:

MySQL Error: Table 'aubriedb.cms_articles' doesn't exist

example:
http://www.aubriericheson.com/recipeDetail.php?Cinnamon-Quinoa-Hot-Cereal-36

__________________________________________________

Okay, and I'm only throwing this easy on in there because I know it will take you a fraction of the time to tell me as it will for me to find it.

right now the ListPageFields for recipes.php is:

dragSortOrder, recipe_categories, title

If I want it to list first by recipe_categories, then drag order, then title (alphabetically, what's the correct working for A-Z DESC... or is that it?

thanks so much!! She has her content in - we're ready to launch on Sunday... hopefully. Thanks! Tina

Re: [csdesign] Sub-categories Tutorial

By Jason - August 23, 2010

Hi,

The first error being cause (recipe_category) looks like it's being caused because there is no field in the recipe_categories table called recipe_category.

The second error is caused because the variable $articleCountByCategoryNum is never actually created.

If you could give me some more information on exactly how you want this to work and the names of all of the fields in both recipe_categories and recipes, I can take a closer look at this issue.

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/

Re: [Jason] Sub-categories Tutorial

By csdesign - August 23, 2010

Sorry... missed this part. The complete list of categories:

View All
Breakfast
Lunch
Dinner
Protein Shakes
Snacks
Dessert

Re: [Jason] Sub-categories Tutorial

By csdesign - August 23, 2010

all fields in recipe.php

<h1>Browse Recipes by Category</h1>
<ul>
<li><a href="?">(View All)</a></li>
<?php foreach ($recipe_categoriesRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>

<?php if ($categoryRecord['_isSelected']): ?><b><?php endif ?>
<a href="?recipe_categories=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?> (<?php echo $articleCountByCategoryNum[$categoryRecord['recipe_categories']] ?>)</a>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif ?>

<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach ?></ul></td>
<td align="left" valign="top">&nbsp;</td>
<td align="left" valign="top">
<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
<h1>Browse Recipes by Title</h1>
<?php foreach ($recipesRecords as $record): ?>

<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br/>

<?php endforeach ?>

<?php if (!$recipesRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
<!-- /STEP2: Display Records -->


=============================================

FROM RECIPEDETAIL.PHP PAGE:

// load records
list($recipesRecords, $recipesMetaData) = getRecords(array(
'tableName' => 'recipe_categories',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$recipesRecord = @$recipesRecords[0]; // get first record

// show error message if no matching record is found
if (!$recipesRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}
$rows = mysql_query_fetch_all_array("SELECT c.num, COUNT(*) FROM {$TABLE_PREFIX}articles a JOIN {$TABLE_PREFIX}categories c ON a.recipe_categories = c.num GROUP BY c.num");
$articleCountByCategoryNum = array_combine(array_pluck($rows, 0), array_pluck($rows, 1));
?>
<?php include "include_header.php"; ?>

<!-- STEP2: Display Records (Paste this where you want your records to be listed) -->
<h1>Recipes - Detail Page Viewer</h1>
<p>Record Number: <?php echo $recipesRecord['num'] ?><br/>
Title: <?php echo $recipesRecord['title'] ?><br/>
Content: <?php echo $recipesRecord['content'] ?><br/>
Recipe Categories: <?php echo $recipesRecord['recipe_categories'] ?><br/>
_link : <a href="<?php echo $recipesRecord['_link'] ?>"><?php echo $recipesRecord['_link'] ?></a><br/>


<!-- STEP 2a: Display Uploads for field 'recipe_photo' (Paste this anywhere inside STEP2 to display uploads) -->
<!-- Upload Fields: num, createdTime, tableName, fieldName, recordNum, preSaveTempId, filePath, filename, extension, thumbFilePath, isImage, hasThumbnail, urlPath, width, height, thumbUrlPath, thumbWidth, thumbHeight, info1, info2, info3, info4, info5 -->
<?php foreach ($recipesRecord['recipe_photo'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>

<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>

<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>

<?php endif ?>
<?php endforeach ?>
<!-- STEP2a: /Display Uploads -->
<?php if (!$recipesRecord): ?>
No record found!</p>
<p><br/>
<br/>
<?php endif ?>
<!-- /STEP2: Display Records -->