Sub-categories Tutorial

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

By Jason - August 23, 2010

Hi,

Try this when outputting your categories:

<?php if ($categoryRecord['_isSelected']): ?><b><?php endif ?>
<?php $where = "category =".intval($categoryRecord['num']); ?>
<a href="?recipe_categories=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?> (<?php echo mysql_select_count_from('recipes',$where); ?>)</a>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif ?>


This code assumes that you're counting the number of records in the table recipes and that the field in recipes is called category.

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

Okay... on recipes.php, I replaced this:

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



with this:
<?php if ($categoryRecord['_isSelected']): ?><b><?php endif ?>
<?php $where = "category =".intval($categoryRecord['num']); ?>
<a href="?recipe_categories=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?> (<?php echo mysql_select_count_from('recipes',$where); ?>)</a>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif ?>


and got this:

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

By Jason - August 23, 2010

Hi,

What is the name of the category field in the recipe table? You'll have to use this field in the $where clause.

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

FANTASTIC! IT WORKS! I should have caught that one. thank you!

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

Okay.. one thing at a time :) okay... two easy ones next (I hope)

On the right column, Browse Recipes by Title, when I click an article link (recipe) I get the error "Record not found!".

I'd also like to see these in alphabetical order. I've changed the sort order before but for whatever reason I can't figure it out this time. I sent to Section Editors > Recipes > Sorting and tried to change "dragSortOrder DESC" to "title" but it won't save. I know it's just something really obvious that I'm missing.

thanks! Tina

Re: [Jason] Sub-categories Tutorial

By csdesign - August 23, 2010

I Added an example of what I'd like to happen in the right column if possible - at below the existing list. http://www.aubriericheson.com/recipes.php

By Chris - August 23, 2010

Hi Tina,

On the right column, Browse Recipes by Title, when I click an article link (recipe) I get the error "Record not found!".


Please attach the full PHP source code for your recipeDetail.php page. I expect there's a whereRecordNumberInUrl(1) in a getRecords() call where there shouldn't be (there should only be one for your recipe record getRecords().)

I'd also like to see these in alphabetical order. I've changed the sort order before but for whatever reason I can't figure it out this time. I sent to Section Editors > Recipes > Sorting and tried to change "dragSortOrder DESC" to "title" but it won't save. I know it's just something really obvious that I'm missing.


Do you have an 'orderBy' option in your getRecords() call in recipes.php? An orderBy there will override the option in Section Editors > Sorting. Also, are you sure you clicked Save Details after making that change? Sometimes I forget!

I Added an example of what I'd like to happen in the right column...


Splitting your results up into 5 pages (A-E | F-J | K-O | P-S | T-Z) isn't a very scalable solution. If you have categories with very few records, your users may get confused (especially if there's a category with no records beginning with A-E!) If you have categories with many, many records, your lists could still get quite long!

I'd recommend going with good old fashioned pagination (i.e. [<<prev] page 1 of 6 [next>>].)

Please let me know if you need help setting up pagination... or if you're absolutely certain that you want to split your results up into 5 pages (A-E | F-J | K-O | P-S | T-Z.)
All the best,
Chris

By Chris - August 23, 2010

Hi Tina,

Here's the full PHP source for recipeDetail.php page:


I'm guessing there's a whereRecordNumberInUrl(1) in a getRecords() call in one of the INCLUDE files. Specifically, because you're getting the "Record not found!" message before anything is displaying, it must be in include_header.php. When getting a record from a Single Record section, you don't need a 'where' line at all.

... and clicked "Save Details" and it won't take


That's troubling. Can we take a look? Please fill out a [url http://www.interactivetools.com/support/]2nd Level Support Request[/url] with your CMS Builder login details. Also, please add my name and a link to this thread in the comments section.

good old fashioned pagination!


First off, add the following red line to your $recipeRecords getRecords() call (you can change the number 10 to whatever you like):

list($recipeRecords, $recipeMetaData) = getRecords(array(
'tableName' => 'recipe',
...
'perPage' => 10,
));


Next, paste this code somewhere on your page (after STEP 2 would make sense):

<!-- STEP3: Display Page Links (Paste anywhere below "Load Record List") -->
<?php if ($recipeMetaData['prevPage']): ?>
<a href="<?php echo $recipeMetaData['prevPageLink'] ?>">&lt;&lt; prev</a>
<?php else: ?>
&lt;&lt; prev
<?php endif ?>

- page <?php echo $recipeMetaData['page'] ?> of <?php echo $recipeMetaData['totalPages'] ?> -

<?php if ($recipeMetaData['nextPage']): ?>
<a href="<?php echo $recipeMetaData['nextPageLink'] ?>">next &gt;&gt;</a>
<?php else: ?>
next &gt;&gt;
<?php endif ?>
<!-- /STEP3: Display Page Links -->


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 23, 2010

pagination worked perfect. Thanks!! I just sent in the request. Thank you very much for your help!

Re: [Jason] Sub-categories Tutorial

By Mikey - October 30, 2010

Jason,
Any suggestions on how to make this top level and second level loop work with an additional third level?

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:

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

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