field type sql, but checkboxes instead of dropdown list?

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
rockuman_ex
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu May 10, 2012 7:13 am

field type sql, but checkboxes instead of dropdown list?

Post by rockuman_ex » Fri May 11, 2012 5:53 am

Hi,

I'm developing a custom component, my question is:

Is there away to create checkboxes based on sql?

eg:

Code: Select all

<field name="category_id" type="sql" class="inputbox"
			 query="SELECT id, title FROM #__mycomp_category order by title asc" 
			 key_field="id" 
			 value_field="title"
			filter="safehtml" />
From the above, i will get a drop down list, but how do make it as checkboxes?
eg:
[] Category 1
[] Category 2
[] etc...


Help?
Last edited by humvee on Sat May 12, 2012 7:53 am, edited 1 time in total.

rockuman_ex
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu May 10, 2012 7:13 am

Re: field type sql, but checkboxes instead of dropdown list?

Post by rockuman_ex » Fri May 11, 2012 6:53 am

I got it.

I have to create custom_fields :D

Now that i have created the custom fields, how do i capture the checkboxes values and store it as 1,3,4 etc?

I have created a new file in
\administrator\components\com_mycomp\models\fields\CategoryCheckBoses.php

and inside this file i have:

Code: Select all

<?php
defined('JPATH_BASE') or die;

jimport('joomla.html.html');
jimport('joomla.form.formfield');

class JFormFieldCategoryCheckBoxes extends JFormField
{
	protected $type = 'checkboxes';
	protected function getInput()
	{
		//Get form details
		$db =& JFactory::getDBO();
		$query = "SELECT id, title FROM #__copmitips_category WHERE state = 1 ORDER BY title";
		$db->setQuery($query);
		$categories = $db->loadObjectList();
		$var_list ='';
		foreach($categories as $category){
			$var_list.= '<input name="'.$this->name.'" type="checkbox" value="'.$category->id.'">'.$category->title.'<br />';
		}
		return $var_list;
	}
}
When i checked multiple check boxes, the only value that were captured is the last checkbox.

And this is my xml file:

Code: Select all

<field name="category_id" type="CategoryCheckBoxes" required="true" />
And how do i make the checkboxes ticked when it's being edited?

Thank you[Mod Note: Bump post by you deleted. Please do not bump your messages. Read the Forum Rules for full details.

User avatar
humvee
Joomla! Master
Joomla! Master
Posts: 14704
Joined: Wed Aug 17, 2005 10:27 pm
Location: Kent, England

Re: field type sql, but checkboxes instead of dropdown list?

Post by humvee » Sun May 13, 2012 10:22 am

[Mod note: Moved from General Forum to 2.5 coding Forum;]

rockuman_ex
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu May 10, 2012 7:13 am

Re: field type sql, but checkboxes instead of dropdown list?

Post by rockuman_ex » Sun May 13, 2012 11:09 pm

humvee wrote:[Mod note: Moved from General Forum to 2.5 coding Forum;]
thank you.

Didn't know where to post. Sorry newbie here.

Can anyone please guide me to the right direction?


rockuman_ex
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu May 10, 2012 7:13 am

Re: field type sql, but checkboxes instead of dropdown list?

Post by rockuman_ex » Sun May 13, 2012 11:50 pm

webdongle, thank you.

This is what i've done.
on my /administrator/components/com_mycomp/models/fields/CategoryCheckBoxes.php

Code: Select all

class JFormFieldCategoryCheckBoxes extends JFormField
{
	protected $type = 'checkboxes';
	protected function getInput()
	{
		$db =& JFactory::getDBO();
		$query = "SELECT id, title FROM #__copmitips_category WHERE state = 1 ORDER BY title";
		$db->setQuery($query);
		$categories = $db->loadObjectList();
		$var_list ='';
		foreach($categories as $category){
			$var_list.= '<li><input name="'.$this->name.'[]" type="checkbox" value="'.$category->id.'"><label for="jform_category_id'.$category->id.'">'.$category->title.'</label></li>';
		}
		return '<fieldset id="jform_category_id" class="checkboxes"><ul>'.$var_list.'</ul></fieldset>';
	}
}
On my administrator/components/com_mycomp/models/forms/item.xml

Code: Select all

<field name="category_id" type="CategoryCheckBoxes" required="true" />
then on my administrator/components/com_mycomp/tables/item.php

Code: Select all

public function bind($array, $ignore = '')
	{
		if (isset($array['category_id']) && is_array($array['category_id'])) {
			$array['category_id'] = implode(',', $array['category_id']);
		}
		return parent::bind($array, $ignore);
	}
I can save the value no problem (eg. 1,2,3)

But when I'm editing the form, the checkboxes aren't ticked.

Where do I make it ticked?

I'm stuck... please help.

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 44092
Joined: Sat Apr 05, 2008 9:58 pm

Re: field type sql, but checkboxes instead of dropdown list?

Post by Webdongle » Mon May 14, 2012 12:35 am

Best guess

You are using 'CheckBoxes' (plural) not 'CheckBox' (singular)
The checkboxes form field type provides a multiple checkboxes. Unlike most used standard form field types like textfield or checkbox, this field is not "out of the box" solution. It will create checkboxes for you, and submit their values in form of an array, but it will not store them in database.
http://docs.joomla.org/Checkboxes_form_field_type

<field name="mycheckbox" type="checkbox" label="Show title" description="" value="1" />
http://docs.joomla.org/Checkbox_form_field_type
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"When I'm right no one remembers but when I'm wrong no one forgets".

rockuman_ex
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu May 10, 2012 7:13 am

Re: field type sql, but checkboxes instead of dropdown list?

Post by rockuman_ex » Mon May 14, 2012 12:45 am

Webdongle, once again thank you.

yes, i'm wanting to have checkboxes, so user can select multiple options.

I can save the values no problem (eg. as 2,4,5) etc.

The problem is, when i go to edit page, the checkboxes aren't ticked (when it was already ticked). See attached image.
Untitled-1.jpg
I'm using a custom field (please see my previous post for my code).

Any idea?
You do not have the required permissions to view the files attached to this post.

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 44092
Joined: Sat Apr 05, 2008 9:58 pm

Re: field type sql, but checkboxes instead of dropdown list?

Post by Webdongle » Mon May 14, 2012 1:04 am

Best guess
value="1"

But like the wiki said 'It will create checkboxes for you, and submit their values in form of an array, but it will not store them in database'

Not used checkboxes only checkbox and radio buttons sorry. Can only point you to the documentation on this one.
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"When I'm right no one remembers but when I'm wrong no one forgets".

rockuman_ex
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu May 10, 2012 7:13 am

Re: field type sql, but checkboxes instead of dropdown list?

Post by rockuman_ex » Mon May 14, 2012 1:07 am

Webdongle wrote:Best guess
value="1"

But like the wiki said 'It will create checkboxes for you, and submit their values in form of an array, but it will not store them in database'

Not used checkboxes only checkbox and radio buttons sorry. Can only point you to the documentation on this one.
I can save the values into the database by adding the following code into the bind function

Code: Select all

 if (isset($array['category_id']) && is_array($array['category_id'])) {
         $array['category_id'] = implode(',', $array['category_id']);
      }
      return parent::bind($array, $ignore);
My problem is to retrieve the values, and make the checkboxes ticked when you are editing the data.

rockuman_ex
Joomla! Apprentice
Joomla! Apprentice
Posts: 12
Joined: Thu May 10, 2012 7:13 am

Re: field type sql, but checkboxes instead of dropdown list?

Post by rockuman_ex » Mon May 14, 2012 1:33 am

OMG...

SILLY ME!
To get the value -> $this->value

ARRGGHHHH!!!

SOLVED!

joomlyste
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Aug 17, 2011 10:12 pm

Re: field type sql, but checkboxes instead of dropdown list?

Post by joomlyste » Sat Aug 25, 2012 2:33 pm

hello
I'm newbie
I have the same problem when I edit the recording?
how you retrieve the value checked?
thank you in advance
(Excuse my english)

joomlyste
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Aug 17, 2011 10:12 pm

Re: field type sql, but checkboxes instead of dropdown list?

Post by joomlyste » Sun Aug 26, 2012 2:54 pm

hello
I add the variable $ checked with a function to evaluate the recover $ this-> value (array) and I did call to getValues ​​()
//
foreach ($ categories as $ category) {
$ checked = $ this-> getValues ​​($ category-> id);
$ var_list. = '<input <li> name="'.$this-> name. [] "type =" checkbox "
value = "'. $ category-> id.'" '. $ checked. '>
<label for="jform_category_id'.$category-> id. '">'. $ category-> title. '</ label> </ li>';
}
return '<fieldset id="jform_category_id" class="checkboxes"> <ul>'. $ var_list. '</ ul> </ fieldset>';
}
protected function getValues ​​($ categoryid) {
$ categoryid = (int) $ categoryid;
foreach ($ this-> value as $ values)
{
if ($ categoryid == $ values​​) {
$ checked = 'checked = "checked"';
}

}
return $ checked;
}

oorzaak
Joomla! Apprentice
Joomla! Apprentice
Posts: 40
Joined: Tue Feb 17, 2009 2:19 pm

Re: field type sql, but checkboxes instead of dropdown list?

Post by oorzaak » Thu Jan 31, 2013 7:37 am

So glad with people like you who share their knowledge. Thanks!

Frits

jaimeu
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Wed Jun 25, 2014 7:32 am
Contact:

Re: field type sql, but checkboxes instead of dropdown list?

Post by jaimeu » Wed Oct 22, 2014 11:00 am

the getValues function you're gonna want is actually this

Code: Select all

protected function getValues($catId, $values){
    $checked = '';
    $values = explode(',', $values);
    if(in_array($catId, $values)){
      $checked = 'checked="checked"';
    }
    return $checked;
}
and you want to call it in getInput() function like so

Code: Select all

$checked = $this->getValues($category->id, $this->value);
then lastly just drop it into the input as $checked

My first coding contribution to the Joomla Forum, yay.


Locked

Return to “Joomla! 2.5 Coding”