Jump to content


dropdown list populated by mysql table?

dropdown

  • Please log in to reply
9 replies to this topic

#1 jcklabatt

jcklabatt

    Member

  • Members
  • PipPip
  • 10 posts

Posted 09 March 2013 - 11:56 PM

Hi,

 

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

 

Thanks in advance for any guidance or examples.

 

Jack



#2 heruprambadi

heruprambadi

    Advanced Member

  • Members
  • PipPipPip
  • 83 posts
  • LocationIndonesia

Posted 10 March 2013 - 05:49 AM

hi jcklabatt, you should read this http://www.grocerycr...ns/set_relation



#3 jcklabatt

jcklabatt

    Member

  • Members
  • PipPip
  • 10 posts

Posted 10 March 2013 - 02: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



#4 jcklabatt

jcklabatt

    Member

  • Members
  • PipPip
  • 10 posts

Posted 10 March 2013 - 05: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



#5 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 10 March 2013 - 07: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. 


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#6 jcklabatt

jcklabatt

    Member

  • Members
  • PipPip
  • 10 posts

Posted 10 March 2013 - 09: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



#7 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

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.grocerycr...ns/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.grocerycr...ployees_example

 

Good luck.


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#8 jcklabatt

jcklabatt

    Member

  • Members
  • PipPip
  • 10 posts

Posted 11 March 2013 - 04: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?



#9 DREON

DREON

    Advanced Member

  • Members
  • PipPipPip
  • 118 posts

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');



#10 jcklabatt

jcklabatt

    Member

  • Members
  • PipPip
  • 10 posts

Posted 12 March 2013 - 09: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







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users