Kinds Of Sport For A Player

Hallo, people.

I need to add different kinds of sport(tennis, football, racing) in sportsman’s profile.

I want to make a lot of checkboxes in Register_user form.

But there are really a lot of different types of sport(nearly 50). So I am afraid to make additionally 50 fields in the Player table. I want to make a separate table Sports. And from one Register form I’m gonna filling two tables: tbl_player(with fields id,name, age, birthplace,…) and tbl_player_sports(id, player_id, sport_id)

Tell me please if it’s possible? Thanks.

Yes, it is possible. You can view it as like

Posts and Categories and for their relations post_categoires




Player table

id name etc


Sport table

id name


player_sport relation table 


id player_id sport_id

can I see a working example of a Yii_blog, where blog_posts have categories? In a demo-blog there are no categories for posts

I think, you can find the code in docs.

You can check it here

[sql]

I have 3 tables in the database tbl_player, tbl_player_sports and tbl_sports

CREATE TABLE IF NOT EXISTS tbl_player_sports (

player_id int(11) NOT NULL DEFAULT ‘0’,

sport_id int(11) NOT NULL DEFAULT ‘0’,

PRIMARY KEY (player_id,sport_id),

KEY FK_player_id (player_id),

KEY FK_sport_id (sport_id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO tbl_player_sports (player_id, sport_id) VALUES

(15, 1),

(15, 2),

(17, 2),

(17, 3);

ALTER TABLE tbl_player_sports

ADD CONSTRAINT FK_player_id FOREIGN KEY (player_id) REFERENCES tbl_player (id) ON DELETE CASCADE,

ADD CONSTRAINT FK_sport_id FOREIGN KEY (sport_id) REFERENCES tbl_sports (id) ON DELETE CASCADE;

CREATE TABLE IF NOT EXISTS tbl_sports (

id int(11) NOT NULL AUTO_INCREMENT,

name varchar(20) COLLATE utf8_unicode_ci NOT NULL,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;

– Дамп данных таблицы tbl_sports

INSERT INTO tbl_sports (id, name) VALUES

(1, ‘tennis’),

(2, ‘football’),

(3, ‘swimming’);

CREATE TABLE IF NOT EXISTS tbl_player (

id int(11) NOT NULL AUTO_INCREMENT,

username varchar(128) COLLATE utf8_unicode_ci NOT NULL,

password varchar(128) COLLATE utf8_unicode_ci NOT NULL,

salt varchar(128) COLLATE utf8_unicode_ci NOT NULL,

email varchar(128) COLLATE utf8_unicode_ci NOT NULL,

name varchar(64) COLLATE utf8_unicode_ci NOT NULL,

sname varchar(128) COLLATE utf8_unicode_ci NOT NULL,

fname varchar(128) COLLATE utf8_unicode_ci NOT NULL,

aim int(1) NOT NULL,

atp_rate int(11) DEFAULT NULL,

photo varchar(90) COLLATE utf8_unicode_ci NOT NULL,

profile text COLLATE utf8_unicode_ci,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=19 ;

INSERT INTO tbl_player (id, username, password, salt, email, name, sname, fname, aim, atp_rate, photo, profile) VALUES

(15, ‘root’, ‘8fabcc92999b55cc343646120f8f547c’, ‘28b206548469ce62182048fd9cf91760’, ‘we@g.net’, ‘as’, ‘we’, ‘as’, 0, 45, ‘48413086.jpg’, NULL),

(17, ‘zxc’, ‘1f9099ad686311ab046a6510508bff93’, ‘28b206548469ce62182048fd9cf91760’, ‘t@bigmir.net’, ‘zxc’, ‘zxc’, ‘zxc’, 0, 345, ‘1187134.jpg’, NULL),

[/sql]

And I have a model for a User with relation

            'sports'=>array(self::MANY_MANY, 'Sport','tbl player sports(player id, sport id)'),

Dear YII-programmers,

I want to add a group of checkboxes(tennis,swimming,football) to the sportsman’s register-form.

Submitting this form user is about to automatically fill tables tbl_user and tbl_player_sports.

So I need to change some code in a view, that corresponds to the register-form.

here is a piece of a working code:

[html]

<div class="row">

	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'sname'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'sname'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'sname'); ?&gt;


	&lt;/div&gt;

<div class="row">

	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'aim'); ?&gt;


	&lt;select name=&quot;Player[aim]&quot;&gt;


             &lt;option value=&quot;0&quot;&gt;find a sparring-partner&lt;/option&gt;


             &lt;option value=&quot;1&quot;&gt;find a couch&lt;/option&gt;


             &lt;option value=&quot;2&quot;&gt;challenge a player&lt;/option&gt;


             &lt;option value=&quot;3&quot;&gt;skills&lt;/option&gt;


            &lt;/select&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'aim'); ?&gt;


	&lt;/div&gt;

[/html]

Now, answer me, please. How should I add this checkboxes in this view file?