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

Re: [chris] Sub-categories Tutorial

By csdesign - August 23, 2010

thanks Chris,

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

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

<a href="<?php echo $recipesMetaData['_listPage']; ?>">&lt;&lt; Back to list page</a> | <a href="mailto:?subject=<?php echo thisPageUrl() ?>">Email this recipe to a friend!</a> | <a href="javascript:window.print()">Click to Print This Page</a>

</p>
<?php include "include_footer_sec.php"; ?>






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!

Quote
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 deleted the dragSortOrder field in Section Editors > Recipe and tried to change the ListPage Fields (deleting "dragSortOrder") and deleting "dragSortOrder" from the Sorting page too... and clicked "Save Details" and it won't take.

I did a search for "orderBy" in recipes.php and it didn't find anything. heres' the code under "browse recipes by title"

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





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


I love this idea... see, I knew you would have a better solution than mine. Thank you and yes, I do need help with that.

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.