php - Javascript How to achieve conditional/ nested filters in drop down

44

I have a category dropdown1 (with id:#blogcat), which filters the blog posts of wordpress which is a default functionality.

I have another custom type drop down (with id:#blogarticletype).

Requirement is:

Consider blogs:

Blog 4 which belongs to categoryCategory 1 and typeabc

Blog 5 which belongs to categoryCategory 1 and typexyz

This is Working for category dd alone

If i select Category 1 from 1st drop down(ie category dropdown), it filters me out Blog4 and Blog5 - http://example.com/index.php/blog/category1

This is working for article type dd alone: http://example.com/index.php/blog/?article_type=xyz

Problem here. http://example.com/index.php/blog/category1/?article_type=xyz

If i again selectxyz, from 2nd drop down (ie type dropdown), it should give me Blog5 only -

How to achieve conditional filters in drop down in Javascript

Both filters works separately ie)

If i select value from 1st dd, it gives correct filters and same in 2nd dd

Problem is when i try do a child or 2nd level filter

Below is the code

1st dropdown

jQuery('#blogcat').on('change', function() {
    var baseurl ='<?php echo Mage::getBlockSingleton('wordpress/homepage')->getBlogHomepageUrl(); ?>'   
    var baseurl_events ='<?php echo Mage::getBaseUrl(); ?>events'   
    var category_slug = jQuery(this).val();
    if(category_slug == 'events'){
        var categories_redirect = baseurl_events;
        jQuery( location ).attr("href", baseurl_events);
    }else{
        var categories_redirect = baseurl + category_slug;  
        jQuery( location ).attr("href", categories_redirect);
    }
});

2nd drop down

jQuery('#blogarticletype').on('change', function() {
    var baseurl ='<?php echo Mage::getBlockSingleton('wordpress/homepage')->getBlogHomepageUrl(); ?>'
    var baseurl_events ='<?php echo Mage::getBaseUrl(); ?>events'
    var baseurl_glossary ='<?php echo Mage::getBaseUrl(); ?>glossary'
    var baseurl_catalogs ='<?php echo Mage::getBaseUrl(); ?>catalogs'

    var category_slug = jQuery('#blogcat').val();
    var article_slug = jQuery(this).val();
    if(article_slug == 'events'){
        var categories_redirect = baseurl_events;
        jQuery( location ).attr("href", baseurl_events);
    }
    else if(article_slug == 'glossary'){
        var categories_redirect = baseurl_glossary;
        jQuery( location ).attr("href", baseurl_glossary);
    }
    else if(article_slug == 'catalogs'){
        var categories_redirect = baseurl_catalogs;
        jQuery( location ).attr("href", baseurl_catalogs);
    }
    else if(article_slug == null || article_slug == ''){
        var categories_redirect = baseurl;
        jQuery( location ).attr("href", baseurl);
    }   
    else{           
        var redirect_url = baseurl+category_slug+'?article_type='+article_slug;
        alert(redirect_url);
        jQuery( location ).attr("href", redirect_url);
    }

});

Code on how my custom drop down filter works - magento code UPDATED below.

Fishpig\Wordpress\Block\Post\List.php

protected function _getPostCollection()
{
    //RAMD-641: Custom query to get the Article Filter types
    $artical_type = Mage::app()->getRequest()->getParam('article_type');
    if (is_null($this->_postCollection) && $this->getWrapperBlock()) {
        $this->_postCollection = $this->getWrapperBlock()->getPostCollection();

        if ($this->_postCollection) {
            if ($this->getPostType()) {
                $this->_postCollection->addPostTypeFilter($this->getPostType());
                if(!empty($artical_type)) {
                    $posttypevalue = Mage::helper('wordpress')->getPostMetaData();
                    if(!empty($posttypevalue)) {
                        $this->_postCollection = $this->_postCollection->addFieldToFilter('ID',array('in' => $posttypevalue));
                    }
                }
            }

            if ($this->getPagerBlock()) {
                $this->getPagerBlock()->setCollection($this->_postCollection);
            }
        }
    }

    return $this->_postCollection;
}

Helper/Data.php

public function getPostMetaData()
{
    $artical_type = Mage::app()->getRequest()->getParam('article_type');
    try{
        if(!empty($artical_type)) {
            $resource = Mage::getSingleton('core/resource');
            $readConnection = $resource->getConnection('new_db');
            $currentUrl = Mage::helper('core/url')->getCurrentUrl();
            $url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
            $query = 'SELECT b.post_id FROM wp_posts AS a, wp_postmeta AS b WHERE a.ID=b.post_id AND b.meta_key="type" AND a.post_status="publish"';
            $query .= " AND b.meta_value LIKE '%".$artical_type."%' ORDER BY a.post_date DESC";
            //var_dump($query);
            $post_ids = $readConnection->fetchAll($query);
            if(!empty($post_ids)) {
                $_post_id_array = array();
                foreach($post_ids as $post) {
                    $_post_id_array[] = $post['post_id'];
                }
                $_post_id_array = array_unique($_post_id_array);
                return $_post_id_array;
            }
        }
        return false;
    } catch (Exception $e) {
        return false;
    }
}

People are also looking for solutions to the problem: php - What is difference between reference, pointer and identifier?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.