Recent Topics

Associated Categories Plugin

Started by on Dec 12, 2010 – Contents updated: Dec 12, 2010

Dec 12, 2010 15:14    

Problem:
When you enter a post which is related to certain categories, you have to select them each time.
Ex: When you enter a post about "Metallica" you have to select "Metallica" as main cat and also "Music" & "Metal" & "More" sub categories

Solution:
Storing main categories & its associated categories as a plugin option in two seperate places, one being select, one being multiple array in database and retrieve& check them via jQuery.

Categories:

   - Background  ----------------->level1=main category
   - Fun
      - Music  ---------------------->level2=sub category
      - Movie
   - Welcome
      - Hello
         - Hi  ---------------------->level3=sub category


Assuming the category hierarchy is like above

What i need to spit them like below

  <select id="main_cats">
    <option value="2">Background</option>
    <option value="5">Welcome</option>    
    <option value="8">Fun</option>
  </select>

  <select id="sub_cats" multiple="multiple">
    <option selected="selected">Music</option>
    <option value="11">Movie</option>
    <option value="14">Hello</option>
    <option value="23">Hi</option>
  </select>

To have a better understanding of what i am trying to do, see screen shot below or simply clik here for demo: http://tilqi.com/assets/asc_cats.html

[url=http://tilqi.com/assets/asc_cats.html]http://tilqi.com/assets/asc_cats.jpg[/url]

Assume that i have selected "Background" main category to associate with "Hello" , "Hi" categories

var subcatarray2= jQuery("#sel_extracat_14,#sel_extracat_23,#sel_extracat_355");
jQuery( "#" + this).bind (
"click",
function(){
$(subcatarray2).attr('checked','checked');

Jan 07, 2011 20:49

So all you want is display a dropdown of main cats and a multiple-select list of sub-cats, is that correct?

Jan 07, 2011 22:01

sam2kb wrote:

So all you want is display a dropdown of main cats and a multiple-select list of sub-cats, is that correct?

basically all i need is a "dropdown" and a "multiple select" for all categories.. store and retrieve them in array in DB in order to use in the above jquery code.

For example, when i post something about "Albert Einstein" as a "main category" (radio), i have to select "Scientist" and "German" categories as "extra category" too. To get rid of this selection each time, i want to associate them once and for all.

Feb 22, 2011 13:25

Here's the meat of this plugin. You still need to add some js

global $DB;

$blog = 1; // set a working blog here

$main_cats = $DB->get_results('SELECT cat_ID, cat_name FROM T_categories
								WHERE cat_blog_ID = '.$DB->quote($blog).'
								AND cat_parent_ID IS NULL');

$extra_cats = $DB->get_results('SELECT cat_ID, cat_name, cat_parent_ID FROM T_categories
								WHERE cat_blog_ID = '.$DB->quote($blog).'
								AND cat_parent_ID IS NOT NULL');

$Form = & new Form( 'admin.php', '', 'post' );
$Form->begin_form( 'fform' );
$Form->hidden_ctrl();
$Form->hiddens_by_key( get_memorized() );

$Form->begin_fieldset($this->name);
if( !empty($main_cats) )
{	// Build a dropdown
	$Form->select_input_options( 'qqq', $this->get_options($main_cats), '', '' );
}
if( !empty($main_cats) )
{	// Build multiple-select menu
	$Form->select_input_options( 'www', $this->get_options($extra_cats), '', '', array('multiple'=>'multiple') );
}
$Form->end_fieldset();

$Form->end_form( array( array( 'value' => 'Whatever' ) ) );


function get_options( $rows )
{	// Build options list
	$r = '';
	foreach( $rows as $row )
	{
		$r .= '<option ';
		if( isset($row->cat_parent_ID) )
		{
			$r .= 'id="parentID-'.$row->cat_parent_ID.'" ';
		}
		$r .= 'value="'.$row->cat_ID.'">'.$row->cat_name.'</option>';
	}
	return $r;
}

Feb 22, 2011 13:47

thank you very much.. let me see if i can make this into a plugin.shall it succeed, i ll post back here.


Form is loading...

Free CMS – This forum is powered by b2evolution CMS, a complete engine for your website.