Sub-categories Tutorial

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

By Jason - November 1, 2010

Hi Zick,

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

<?php if($selectedCat): ?>
<?php foreach($categoryRecords as $category): ?>
<?php if ($selectedCat==$category['parentNum']):?>
<?php echo $category['_listItemStart']; ?>
<?php $selectedSubCat=$category['num']; ?>
<a href="?subCat=<?php echo $category['num'];?>&category=<?php echo $selectedCat;?>"><?php echo $category['name'] ?> </a>
<?php echo $category['_listItemEnd']; ?>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>

<?php if(@$_REQUEST['subCat']): ?>
<?php foreach($categoryRecords as $category): ?>
<?php if ($_REQUEST['subCat']==$category['parentNum']):?>
<?php echo $category['_listItemStart']; ?>
<?php $selectedSubCat=$category['num']; ?>
<?php echo $category['name'] ?>
<?php echo $category['_listItemEnd']; ?>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>


This uses 3 loops. The first displays the top level categories, the second will display a second level if a first level is selected. Finally, the third loop will display a third level if a second level has been selected.

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 Mikey - November 1, 2010 - edited: November 1, 2010

Hey Jason,
This works nice. I changed the Third Level code <?php echo $category['name'] ?> to include a link to the associated records as seen in bold. Do you see any thing wrong with this that I should fix?

Thanks Zick

Hi Zick,

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

<?php if($selectedCat): ?>
<?php foreach($categoryRecords as $category): ?>
<?php if ($selectedCat==$category['parentNum']):?>
<?php echo $category['_listItemStart']; ?>
<?php $selectedSubCat=$category['num']; ?>
<a href="?subCat=<?php echo $category['num'];?>&category=<?php echo $selectedCat;?>"><?php echo $category['name'] ?> </a>
<?php echo $category['_listItemEnd']; ?>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>

<?php if(@$_REQUEST['subCat']): ?>
<?php foreach($categoryRecords as $category): ?>
<?php if ($_REQUEST['subCat']==$category['parentNum']):?>
<?php echo $category['_listItemStart']; ?>
<?php $selectedSubCat=$category['num']; ?>
<a href="?category=<?php echo $category['num']?>"><?php echo $category['name']; ?></a>
<?php echo $category['_listItemEnd']; ?>
<?php endif ?>
<?php endforeach ?>
<?php endif ?>


This uses 3 loops. The first displays the top level categories, the second will display a second level if a first level is selected. Finally, the third loop will display a third level if a second level has been selected.

Hope this helps

Re: [chris] Sub-categories Tutorial

By mohabsi - January 10, 2011 - edited: January 10, 2011

hi chris

i try the code that you are posted in first of this thread it works good

but when using paging in articles (next , prev)
the url will be:

artecals.php?category=1&page=2

the selected category is changed depend on end of url
how can I solve this issue

Re: [mohabsi] Sub-categories Tutorial

By Jason - January 10, 2011

Hi,

The best way to get around this is to not use "whereRecordNumberInUrl()". That way the page number won't be used instead of the category number.

For example, you can select your articles like this:

list($articleRecords,$articleMetaData)=getRecords(array(
'tableName' => 'articles',
'allowSearch' => false,
'where' => "category='".mysql_esacpe(@$_REQUEST['category'])."'",
));


Hope this helps get you started. Let me know if you run into any problems
---------------------------------------------------
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 mohabsi - January 11, 2011

Dear Jason

i get this error

Fatal error: Call to undefined function mysql_esacpe() in C:\xampp\htdocs\cms\articles.php on line 7

Re: [mohabsi] Sub-categories Tutorial

By Jason - January 11, 2011

Hi,

Ooops, typo.

Change that to mysql_escape.

That should take care of it.
---------------------------------------------------
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 mohabsi - January 12, 2011

i try this but nothing effected by this line of code [:/]

when I click next or prev articles in pagination button
the category is changed depends on the number of page

article.php?category=1&page=2
article.php?category=1&page=3
article.php?category=1&page=4

the category in url is correct
but in conten selected category is changed

Re: [Jason] Sub-categories Tutorial

By mohabsi - January 13, 2011

hi Jason

the file is attached
Attachments:

articles.php 4K

Re: [mohabsi] Sub-categories Tutorial

By Jason - January 14, 2011

Hi,

You try the following code to temporarily remove the page number from the query string. We then do our category select and then put the page number back:

$tmpQueryString=$_SERVER['QUERY_STRING'];
$_SERVER['QUERY_STRING']=@$_REQUEST['category'];

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

$_SERVER['QUERY_STRING']=$tmpQueryString;
list($articlesRecords, $articlesMetaData) = getRecords(array(
'tableName' => 'articles',
'perPage' => '6',
));


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/