jkkenzie Posted October 29, 2008 Share Posted October 29, 2008 I would like to generate a unique name or text that does not match a particular field values: say i have a field called ProjectName: and in the field list i have project1, project2 and project3, i would like to create something like project4 or anything else like xxgtdf BUT should not match the others. thanks Joe Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/ Share on other sites More sharing options...
bobbinsbro Posted October 29, 2008 Share Posted October 29, 2008 i think the easiest way to generate a random string is to use $hash = md5(time()); and use the first X chars of $hash (where x = the length of string you need). Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-677401 Share on other sites More sharing options...
Mchl Posted October 29, 2008 Share Posted October 29, 2008 In other words a unique identifier. Are you going to use it as a primary key for a table? If so, using INTEGER column with AUTO_INCREMENT is better choice. If you need a column to contain unique values, create a UNIQUE index on it. Then whenever you try to insert a duplicate value, you;ll get a mysql error. So you can do $success = FALSE; while(!$success) { $string = generateRandomString(); //you'll have to come up with code for this function $query = "INSERT INTO table (ProjectName) VALUES ('$string')"; if(mysql_query($query)) $success = TRUE; } Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-677403 Share on other sites More sharing options...
jkkenzie Posted October 29, 2008 Author Share Posted October 29, 2008 I am going to use it as a foreign key to identify a list or a group of records. So they will not be unique in the table they will reside but they will more than one in one table to identify a list of records. Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-677411 Share on other sites More sharing options...
Mchl Posted October 29, 2008 Share Posted October 29, 2008 Still perfect case for INTEGER AUTO_INCREMENT Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-677414 Share on other sites More sharing options...
runnerjp Posted October 29, 2008 Share Posted October 29, 2008 do as mchl said and just auto_increment your table... 1 2 3 4 thats how every ones does it Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-677432 Share on other sites More sharing options...
jkkenzie Posted October 30, 2008 Author Share Posted October 30, 2008 No you dont understand. A group of records will have one or the same name or id SAY: id|Project | name | details| 1 | x | Kenya | graph | 2 | x | uganda| graph | 3 | y | Kenya | graph2 | 4 | y | uganda| graph2 | You realise the project has unique identifying field x or y: Unique in the sense that it identifies a GROUP of records. I hope i am clear this time round. thanks for your help. Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-678216 Share on other sites More sharing options...
samshel Posted October 30, 2008 Share Posted October 30, 2008 you are trying to use it as a foriegn key, so it is mandatory that it should have a master table may be "projects" where it is a primary key and hence unique too..so in addition to your table, you should have one more table called projects which would have following fields: Table : projects Fields project_id - INT (auto-increment) project_name - varchar Then your table will be something like this id|Project | name | details| 1 | 1 | Kenya | graph | 2 | 1 | uganda| graph | 3 | 2 | Kenya | graph2 | 4 | 2 | uganda| graph2 | Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-678220 Share on other sites More sharing options...
jkkenzie Posted October 30, 2008 Author Share Posted October 30, 2008 Actually my project doesnt aloow for that table to be made, it is supposed to be a one time data. I wish i could store my results in a cache memory to be used by my graph when i move to the next page, but my calculations are alot. I move through 4 pages carrying selected fields values in variables (POST) then on the forth page i use those data to calaculate two other values(which i put them to fields in my tblcalc) this two other values are created when my while statement loops and stores in $A and $C which i save in my tblcalc as i loop . the project does not need the user creating a project name or project table, the data is fixed it is stored once. in a table tbldata, from which the user selects what needs to plotted on a graph. here am trying to store in my temporary table tblcalc which data in it is deleted maybe after ten minutes, THAT IS WHY I NEEDED THE UNIQUE FIELD WHICH WOULD IDENTIFY THOSE SELECTED VALUES BY USER FOR ATLEAST 10 MINUTES THEN IT COULD BE DELETED. thanks. Joseph Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-678242 Share on other sites More sharing options...
Mchl Posted October 30, 2008 Share Posted October 30, 2008 You've already got suggestions on how to achieve the capsed request. Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-678246 Share on other sites More sharing options...
jkkenzie Posted October 30, 2008 Author Share Posted October 30, 2008 I have used: $hash=md5(time()); $hash= substr($hash,1,6); $project = $hash; to get a unique project THEN delete all whose time is less than ten minute Thanks Jose Link to comment https://forums.phpfreaks.com/topic/130571-solved-randon-unique-field-name/#findComment-678272 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.