WordPress.org

Ready to get started?Download WordPress

Forums

How to convert this PHP function to display dropdown list instead of checkboxes? (3 posts)

  1. wprelax
    Member
    Posted 1 year ago #

    How to modify this function to show categories in a dropdown menu instead of list with checkboxes, with categories and subcategories?

    Function

    function get_question_categories($queston_cat = array())
    {
    	$return_str = '';
    	if(get_option('ptthemes_question_cat_selection_flag'))
    	{
    
    		$cat_exclude = get_inc_categories("cat_exclude_");
    		$cat_exclude_arr = explode(',',$cat_exclude);
    		$cat_arr = array();
    		for($i=0;$i<count($cat_exclude_arr);$i++)
    		{
    			if($cat_exclude_arr[$i])
    			{
    				$cat_arr[] = $cat_exclude_arr[$i];
    			}
    		}
    		if($cat_arr)
    		{
    			for($j=0;$j<count($cat_arr);$j++)
    			{
    				$selected='';
    				if($queston_cat)
    				{
    					if(in_array($cat_arr[$j],$queston_cat))
    					{
    						$selected = 'checked';
    					}
    				}else
    				if($_SESSION['question_info']['queston_cat'] && in_array($cat_arr[$j],$_SESSION['question_info']['queston_cat']))
    				{
    					$selected = 'checked';
    				}
    				$return_str .= '<li><input type="checkbox" value="'.$cat_arr[$j].'" name="queston_cat[]" '.$selected.'>'.get_cat_name( $cat_arr[$j] ).'</li>';
    			}
    		}
    	}
    	return $return_str;
    }

    And this code is calling categories list

    <div class="ask_row">
                <label> <?php _e('Select Category');?> : </label>
                <?php
                if(get_question_categories())
                {
                ?>
                <ul class="question_cat">
                <?php echo 	get_question_categories($queston_cat);?>
                </ul>
                <?php }?>
    </div>
  2. potentweb
    Member
    Posted 1 year ago #

    if($cat_arr)
    		{
    			for($j=0;$j<count($cat_arr);$j++)
    			{
    				$selected='';
    				if($queston_cat)
    				{
    					if(in_array($cat_arr[$j],$queston_cat))
    					{
    						$selected = 'checked';
    					}
    				}else
    				if($_SESSION['question_info']['queston_cat'] && in_array($cat_arr[$j],$_SESSION['question_info']['queston_cat']))
    				{
    					$selected = 'checked';
    				}
    				$return_str .= '<li><input type="checkbox" value="'.$cat_arr[$j].'" name="queston_cat[]" '.$selected.'>'.get_cat_name( $cat_arr[$j] ).'</li>';
    			}
    		}
    	}
    	return $return_str;
    }

    You need to modify this section

  3. wprelax
    Member
    Posted 1 year ago #

    Thank you, potentweb, I tried to modify this function and I failed to do that on my own, that's why I asked if anyone can solve this for me :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags