⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

dropdown list populated by mysql table?



jcklabatt
  • profile picture
  • Member

Posted 09 March 2013 - 23:56 PM

Hi,

 

How can I create a dropdown list populated by mysql table items?

 

Thanks in advance for any guidance or examples.

 

Jack


heruprambadi
  • profile picture
  • Member

Posted 10 March 2013 - 05:49 AM

hi jcklabatt, you should read this http://www.grocerycrud.com/documentation/options_functions/set_relation


jcklabatt
  • profile picture
  • Member

Posted 10 March 2013 - 14:44 PM

Hi heruprambadi,

 

Thanks for the reply. The information was there right in front of me but I guess I needed someone to point it out to me. My bad. Thanks for the link.

 

Jack


jcklabatt
  • profile picture
  • Member

Posted 10 March 2013 - 17:37 PM

Hi again,

 

I'm trying to wrap my head around the "set_relation" function and I need a some help. Let me explain: I have five tables, four of which are related. When a user is adding a New Record and they select "colors" from the dropdown, I would like to give them an option based upon their selection. For example, if a user chooses "red", the next field on the form would present the options within that category (light red, red, dark red).

 

And in addition to the related tables, I have a table that does not have a relation.

 

How can I make this happen? here's the tables:

 

TABLE colors
VALUES ('red');
VALUES ('blue');
VALUES ('green');

TABLE red
VALUES (NULL, 'ltr', 'Light Red');
VALUES (NULL, 'red', 'Red');
VALUES (NULL, 'dkr', 'Dark Red');

TABLE blue
VALUES (NULL, 'ltb', 'Light Blue');
VALUES (NULL, 'blu', 'Blue');
VALUES (NULL, 'dkb', 'Dark Blue');

TABLE green
VALUES (NULL, 'ltg', 'Light Green');
VALUES (NULL, 'grn', 'Green');
VALUES (NULL, 'dkg', 'Dark Green');


TABLE fruit
VALUES (NULL, 'APP', 'Apples');
VALUES (NULL, 'PEC', 'Peaches');
VALUES (NULL, 'PER', 'Pears');

 

 Note: I would like to list the abbreviations in the dropdowns, not the full names.

 

Thanks in advance,

Jack


davidoster
  • profile picture
  • Member

Posted 10 March 2013 - 19:36 PM

Your database structure is disparate. You need to get all the color options in one table and categorize them via unique fields, for example

 

table colors as you have it

 

table color_attributes with fields:

- FK_color_id (from table colors)

- field1 (values e.g. NULL)

- field2 (values e.g. 'ltr', 'red', 'dkr', 'ltb', 'blu', 'dkb' etc)

- field3 (values e.g. 'Light Red', 'Red', 'Dark Red', 'Light Blue', 'Blue', 'Dark Blue' etc)

 

so in essence you combine all three tables in just one!

Then your relation function is straight forward as it is described on the manuals. 


jcklabatt
  • profile picture
  • Member

Posted 10 March 2013 - 21:40 PM

Thanks davidoster, I have consolidated my database tables per your suggestion.

 

Maybe I'm getting ahead of myself here, before I try to set a relation between two tables I need to figure out how to populate a single dropdown list with a database values. I've tried almost every API function from the list and I just can't seem to figure out the correct code.

 

Can someone show me how this is done?

 

Thanks in advance,

Jack


davidoster
  • profile picture
  • Member

Posted 11 March 2013 - 01:10 AM

Since you're starting to learn how to use GC lib, why don't you use the built in examples first in order to understand how the various library functions work?

The answer to your question is already being given, http://www.grocerycrud.com/documentation/options_functions/set_relation

 

this sample code is an extract from the examples that come along with grocery crud lib,

 

public function employees_management()
	{
			$crud = new grocery_CRUD();

			$crud->set_theme('datatables');
			$crud->set_table('employees');
			$crud->set_relation('officeCode','offices','city');
			$crud->display_as('officeCode','Office City');
			$crud->set_subject('Employee');

			$crud->required_fields('lastName');

			$crud->set_field_upload('file_url','assets/uploads/files');

			$output = $crud->render();

			$this->_example_output($output);
	}

 

which is this, http://www.grocerycrud.com/examples/employees_example

 

Good luck.


jcklabatt
  • profile picture
  • Member

Posted 11 March 2013 - 16:42 PM

Hi davidoster for the reply. Yes, the demo includes a dropdown list but it does indicate that those items are coming from a database table. From the example

 

$crud->set_relation('officeCode','offices','city');

 

which is the database table officeCode? offices? or city?

 

That's my problem, there's not enough information to see how the database/table connection is made.

 

So, if I have a table named "city", how can I show the results in a dropdown list?


DREON
  • profile picture
  • Member

Posted 12 March 2013 - 03:48 AM

@jcklabatt sir you should have relation column on your main table from your relation table..

 

bold letter is my explanation.

 

ex.

 

$crud->set_theme('datatables');
            $crud->set_table('employees'); - this is your main table
            $crud->set_relation('officeCode','offices','city');

          officeCode - this is column on your main table that related to your 2nd table.

          offices - this is your 2nd table

          city - this is the column on your 2nd table and also this is what you show you drop down.

          (how to show my drop down - click edit)


            $crud->display_as('officeCode','Office City');
            $crud->set_subject('Employee');


jcklabatt
  • profile picture
  • Member

Posted 12 March 2013 - 21:44 PM

Hi Dreon,

 

Thank you for providing the response. I've figured it out by following davidoster's recommendation with regards to reading the manuals. Everything that I've been looking for was in the example section just like davidoster had mentioned. Once again, thanks for the help.

 

To davidoster:

 

I feel like a jackass. You had suggested a solution but my stubbornness revealed its ugly head instead. 

 

Thank you for pointing me in the right direction.

 

Jack