Page 1 of 1

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

Posted: Fri Feb 29, 2008 7:50 am
by Bodom78
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.

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

Posted: Sun Mar 02, 2008 2:24 am
by Bodom78
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 ) );

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

Posted: Mon Mar 03, 2008 9:53 am
by missstar
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

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

Posted: Wed Mar 05, 2008 9:03 am
by Bodom78
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.

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

Posted: Thu Mar 06, 2008 4:15 pm
by Spetter
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

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

Posted: Sat Mar 08, 2008 1:59 am
by Bodom78
The code uses " $db->setQuery($sql);"

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

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

Posted: Sat Mar 08, 2008 9:06 am
by Spetter
Thank you. Works great!!!

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

Posted: Mon Mar 10, 2008 5:19 pm
by missstar
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.

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

Posted: Tue Mar 11, 2008 11:13 am
by Bodom78
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 :)