Categorize Filter - option type B

How to add Category Filters (option type 'B') in Table Views

When using category filters in table we use the option type "B", B stands for Blog, as most of the times Blogs have categories and articles need to be filtered by categories, but it is used a lot for services with categories, pages with categories and also in products with categories.

When creating the section make sure to add the option type B

$Blog->setOption("VIEW ARTICLES,VER ARTICULOS,VOIR ARTICLES","aside","4","VPSLB",true);

The option type B will make the $options['categorize'] variable = true and will let us use the following variables in our table view

//declared in header when option type B exists
//$options['categorize'] variable = true 

$cattable = "CATEGORIES"; 
$showAllCat = true / false; 
$catdefault = "0";  
$catfield= "CAT_NAME";
$catid = "CATID";
$qcatid = "q.CATID";
$qcatwhere = "where ....";
$qlike = true/false;
$bpos = true/false;
$filterBy = //string or array;
$filtertype = 'default filter by'

 

  • $cattable Table from the category is coming from
  • $showAllCat Will add the option view all
  • $catdefault: default value to select, if $catdefault is not declared or null, $showAllCat becomes the default if true, if false the first result in the categories becomes the default.
     
$showAllCat = true; 
$catdefault = "5";

  • $catfield column name where the name to be displayed on the dropdown of the category is found.
  • $catid name of the column of the category ID, usually a foreign key to the main query.
  • $qcatid is the name with which the column is named on the original query "$query" at the bottom of the file.  For instance if the table name is PRODUCTS as p then $qcatid will be p.CATID (%%WHERE%% must exist)
     
$dbtable = 'PRODUCTS';
$query = "SELECT * FROM $dbtable p  %%WHERE%%";

In this case if a category is selected the final $query will become:

 SELECT * FROM $dbtable p where p.CATID = 52

  • $qcatwhere where clause to be added to the query when selecting the categories to display on the drop down, must start with "where". 
  • $qlike on change he ID value of the category is added to the URL and becomes the variable "$thisCATID", if q_like is true, the query becomes where CATID LIKE '%$thisCATID%' instead of where CATID= '%$thisCATID%' 
  • $bpos true or false, it is used if the categories have POS and they need to be respected in the drop down
  • $filterBy If filter by is a string it will simply change the text where it says "Filter by categories, for "Filter by $filterBy". 

    If it is an array, it will become a drop down option, and on change will replace the page submitting the variable $filtertype with the value of the option selected
     
$filterBy[0] = "Brands";
$filterBy[1] = "Categories";

$filtertype (string) Default filterby when filter by is an array, this is the variable that is sent on the URL when filter type changes, here we can determine the type of variables to declare depending on the $filtertype

 

if(!isset($filtertype)){
     //DEFAULT
     $filtertype = 'brands'; //lower case
} 

switch($filtertype){
case 'categories': //lower case
    $cattable = "PRODUCTS_CATEGORIES";
    $showAllCat = true;
    $catfield= "title_en";
    $catid = "PCID";
    $qcatid = "PRODUCTS.PCID";
    $qcatwhere = "order by title_en";
    $bpos = false;
    //Do not show position for this filter
    $showpos = false;
break;
case:'brands': //lower case
default:
    $cattable = "PRODUCTS_BRANDS";
    $showAllCat = true;
    $catfield= "title_en";
    $catid = "PBID";
    $qcatid = "PRODUCTS.PBID";
    $qcatwhere = "order by title_en"; 
    $bpos = false;
break;
}

 

NON foreign key categories

This happens often when we need to categorize our table but not coming from a foreign key. but an internal value, for example, categorize by "status" 1 or 0

 

$cattable = "KAS_ACCESS"; 
$showAllCat = true;
$catdefault = "1";
$catid = "status";
$catfield= "status";
$qcatid = "status";
$catfield = "IF(status=1, 'All Enabled', 'All Disabled')";
$qcatwhere = "group by status";
$filterBy = "Status";
$bpos = false;
Get a Quote

Get a Quote