Populating a list like JHTML::_('list.category' ...

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
Bodom78
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Fri Nov 04, 2005 9:23 am

Populating a list like JHTML::_('list.category' ...

Post by Bodom78 » Fri Feb 29, 2008 7:50 am

Hey Guys and Gals,

I have my own Categories table in the database that I need to display in the drop down list. Could someone please give me a hand with this.

I found this in the weblinks component:

Code: Select all

$lists['catid'] = JHTML::_('list.category',  'filter_catid', $option, intval( $filter_catid ), $javascript );
But I'm guessing that this is a built in request that only works for the default Joomla Categories table.

If some one could help me out with doing the same on a custom database table, that would be fantastic.

In the meantime I'll continue going through the core component to see what I can find.

Cheers.

Bodom78
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Fri Nov 04, 2005 9:23 am

Re: Populating a list like JHTML::_('list.category' ...

Post by Bodom78 » Sun Mar 02, 2008 2:24 am

OK, I got it by placing this in my view.html

Code: Select all

$sql = 'SELECT id, cattitle'
		. ' FROM #__customcategories'
		;
		$db->setQuery($sql);

		$catlist[]			= JHTML::_('select.option',  '0', JText::_( '- Select Category -' ), 'id', 'cattitle' );
		$catlist			= array_merge( $catlist, $db->loadObjectList() );
		$lists['catid']		= JHTML::_('select.genericlist', $catlist, 'filter_catid', 'class="inputbox" size="1" onchange="document.adminForm.submit();"','id', 'cattitle', intval( $filter_catid ) );

User avatar
missstar
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Mar 03, 2008 7:52 am

Re: Populating a list like JHTML::_('list.category' ...

Post by missstar » Mon Mar 03, 2008 9:53 am

may i ask you how you save it to database
i mean how we can add the result of a drop down list to database?
(i use MVC style)
sorry if i ask it wrong place.
Thank you

Bodom78
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Fri Nov 04, 2005 9:23 am

Re: Populating a list like JHTML::_('list.category' ...

Post by Bodom78 » Wed Mar 05, 2008 9:03 am

In my viewhtml.php I built the list like this:

Code: Select all

// build the catagory list
		$sql = 'SELECT id, cattitle'
		. ' FROM #__whosracing_cat'
		;
		$db->setQuery($sql);

		$catlist[]			= JHTML::_('select.option',  '0', JText::_( '- Select Category -' ), 'id', 'cattitle' );
		$catlist			= array_merge( $catlist, $db->loadObjectList() );
		$lists['catid']		= JHTML::_('select.genericlist', $catlist, 'catid', 'class="inputbox" size="1"','id', 'cattitle', $item->catid );
		
		//-- push data to template
		$this->assignRef( 'lists',	$lists);
JHTML::_('select.option', '0', JText::_( '- Select Category -' ), 'id', 'cattitle' ) <-- id is the table I'm saving to so the default store($data) function like you find in the Weblinks otr Banners component works.

Hope that helps.

Spetter
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Feb 29, 2008 8:15 pm

Re: Populating a list like JHTML::_('list.category' ...

Post by Spetter » Thu Mar 06, 2008 4:15 pm

I get an error when i use the code in view.html.php...

Can someone help me please?
Notice: Undefined variable: db in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\administrator\components\com_productmanager\views\productmanager\view.html.php on line 32

Bodom78
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Fri Nov 04, 2005 9:23 am

Re: Populating a list like JHTML::_('list.category' ...

Post by Bodom78 » Sat Mar 08, 2008 1:59 am

The code uses " $db->setQuery($sql);"

so you will need to include $db =& JFactory::getDBO();
before the posted code.

Spetter
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Fri Feb 29, 2008 8:15 pm

Re: Populating a list like JHTML::_('list.category' ...

Post by Spetter » Sat Mar 08, 2008 9:06 am

Thank you. Works great!!!

User avatar
missstar
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Mon Mar 03, 2008 7:52 am

Re: Populating a list like JHTML::_('list.category' ...

Post by missstar » Mon Mar 10, 2008 5:19 pm

Thank you so much for the answer.i highly appreciate your kindness.
do you have any idea how i can build a 3 drop down list for 1 field in database?
suppose database filed is timestamp or datetime.
then i have to use different drop down lists for each part of date mount and year.
i know how use php date functions.
my problem is by storing.
endless thanks for your attention and help.

Bodom78
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Fri Nov 04, 2005 9:23 am

Re: Populating a list like JHTML::_('list.category' ...

Post by Bodom78 » Tue Mar 11, 2008 11:13 am

If your after a calendar, you can just use

Code: Select all

 <?php echo JHTML::_('calendar', $this->row->created, 'created', 'created', '%Y-%m-%d %H:%M:%S', array('class'=>'inputbox', 'size'=>'25',  'maxlength'=>'19')); ?>
this will echo out a input filed and the DHTML date selector.


If you prefer to merge post data from 3 input fields you could intercept the post values right before the "store" function in your model.

So lets say your 3 Drop Downs are "day", "month", "year" and you want to bind them to a database field called "joined_date"

Code: Select all

function store($data)
{
	
        $row =& $this->getTable('default');
        
        //-- merge the posted date drop down fields and assign to joined_date
        $data = JRequest::get( 'post' );
        $data['joined_date'] = $data['day'] . $data['month'] . $data['year'];

        
        //-- now the general bind functions in Joomla
        //-- Bind the form fields to the table
	if (!$row->bind($data)) {
		$this->setError($this->_db->getErrorMsg());
		return false;
	}

	// if new item, order last in appropriate group
	if (!$row->id) {
		$where = 'catid = ' . (int) $row->catid ;
		$row->ordering = $row->getNextOrder( $where );
	}

	// Make sure the table is valid
	if (!$row->check()) {
		$this->setError($this->_db->getErrorMsg());
		return false;
	}

	// Store the web link table to the database
	if (!$row->store()) {
		$this->setError($this->_db->getErrorMsg());
		return false;
	}

	return true;

}
If someone has a more efficient way, be sure to let me know :)


Post Reply