Back to Code repository sumary

Show a list of fixed values (not from the db) in a select

Explanations

This is the simplest way to show a list of fixed values in a select widget.

Code snippet

First, you have to create a p4a_array_source object anywhere, for example, in your XxxApplication :

...
	function XxxApplication()
	{
		...
 
		// Create the source with fixed values
		$this->build("p4a_array_source","my_fixed_values_src");
		$a = array();
		
		$a[]["your_field_name"] = "First fixed value";
		$a[]["your_field_name"] = "Second fixed value";
		$a[]["your_field_name"] = "Third fixed value";
		
		$this->my_fixed_values_src->load($a);
		$this->my_fixed_values_src->setPk("your_field_name");
		...
	}
...

In this case, the corresponding field in your table is “your_field_name”. And in this field, you will find the value “First fixed value”, “Second fixed value” or “Third fixed value” according to the user’s choice.

Then you can use your db_array_source in your app, for example in the constructor of your mask :

...
		// Do this before to anchor() this field
		$this->fields->your_field_name->setType("select");
		$this->fields->your_field_name->setSource($p4a->my_fixed_values_src);
...

If you want a key/value data source, you can do this:

		$a[] = array("id"=>1, "desc"=>"First fixed value");
		$a[] = array("id"=>2, "desc"=>"Second fixed value");
		$a[] = array("id"=>3, "desc"=>"Third fixed value");
		$this->my_fixed_values_src->load($a);
		$this->my_fixed_values_src->setPk("your_field_name");

and then

		$this->my_fixed_values_src->load($a);
		$this->my_fixed_values_src->setPk("your_field_name");
		$this->my_fixed_values_src->setSourceValueField("id");
		$this->my_fixed_values_src->setSourceDescriptionField("desc");

where the last two lines are NOT mandatory cause they should be autodetected. In this second case, the corresponding field in your table is “id”. And in this field, you will find the value 1, 2 or 3 when the user select “First fixed value”, “Second fixed value” or “Third fixed value”.

User's comments

2007-01-07: Please ask question and say comments after here - thank’s - pespie
2007-01-07: Maybe we can use a key/value data source instead of having only one field, what you think? anyway great great work! - balliano
2007-01-07: done :) - balliano
2007-01-08: made a little correction, and added some explanations about fields name in DB - pespie

 
show-a-list-of-fixed-values-not-from-the-db-in-a-select.txt · Last modified: 2007/01/09 08:09 by 213.103.50.112
 
Creative Commons License Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki