Namelus Posted November 20, 2013 Share Posted November 20, 2013 I am implementing a form using CActiveForm (Yii) and my database is in MySQL. My requirement is to implement a solution which would enroll and assign my users (candidates, filling the form) with date_of_test and and unique roll_number based on test_locations. I have multiple test_locations and they have specific seating capacity for a day e.g Arizona = 1000, Florida = 200. Now based on the seating capacity i need to assign roll_number (unique) and date_of_test to a candidate, e.g in a candidate table data would something be like: id | roll_number | date_of_test | state | <--- ID is auto incremented by MySQL ----------------------------------------------- 1 | 999 | 11/20/2013 | Arizona ----------------------------------------------- 2 | 50 | 11/20/2013 | Florida ----------------------------------------------- 3 | 1000 | 11/20/2013 | Arizona ----------------------------------------------- 4 | 1001 | 11/21/2013 | Arizona <--- Next Date given to candidate ----------------------------------------------- Test locations are assigned to candidates by application based on their present location i.e if someone is from Arizona state his test location would automatically be selected as Arizona (implemented via simple dependent dropdown). I am finding really hard to implement this logic of assigning roll_number and date_of_test to candidates, as i don't want anyone to have same roll number and anyone given a date_of_test more than the seating capacity of a location. Maybe Yii/PHP/MySQL has some function which i am not aware would assist me in implementing it easily. Thus, requiring assistance. Quote Link to comment https://forums.phpfreaks.com/topic/284109-assigning-unique-range-values-with-dates/ Share on other sites More sharing options...
Barand Posted November 21, 2013 Share Posted November 21, 2013 (edited) Instead of the primary key being the auto_incrementing ID col, define the table as state VARCHAR(20) NOT NULL, roll_number INT NOT NULL auto_increment, date_of_test DATE, PRIMARY KEY (state, roll_number) If the second part of the PK is auto_increment then it will number from 1 to N for each state. The date for each will be basedate + floor((roll_number-1)/state_capacity) days Edited November 21, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/284109-assigning-unique-range-values-with-dates/#findComment-1459454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.