egillsigurdur Posted September 4, 2009 Share Posted September 4, 2009 I have this signup form for a sports team, and I'd like to be able to sort them into an Excel file by age. It is only requested that you give up your ID number, and I have no idea how I'm going to do that. This is what the Icelandic ID number is like: 2306823269. 23:day of month. 06:month. 82:year born (1982) 3269:some number that doesn't matter. So, how would I do this? I also have to take in count that many of these children are born in 20--. I already know how to put info in Excel, just not how to sort them from the MySQL database by this. Thank you. Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/ Share on other sites More sharing options...
trq Posted September 4, 2009 Share Posted September 4, 2009 Your not going to be able to sort them in a query. These numbers mean nothing to mysql. You'll need to write you own parser of sorts, using strtotime, again, however, theres no way of handling the problem of people being born in 20** if the data isn't stored in the first place. Id say your pretty screwed. Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-912256 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 How about this: $id = 2306823269; $values = array ( //Sorts all data out of ID into array. 'day' => substr( $id, 0, 2); //Returns '23' 'month' => substr( $id, 2, 2); //Returns '06' 'year' => substr( $id, 4, 2); //Returns '82' 'other' => substr( $id, 6, 4); //Returns '3269' ); $currentYear = date('y'); //Returns a two-digit representation of the current year. if ($values['year'] <= $currentYear) { //Evaluates between a 1900 birthdate or a 2000 birthdate. //Born in 2000's. $values['year'] = '20' . $values['year'] } else { //Born in 1900's. $values['year'] = '19' . $values['year'] } Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-912275 Share on other sites More sharing options...
trq Posted September 4, 2009 Share Posted September 4, 2009 What of someone born in 1907? Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-912314 Share on other sites More sharing options...
egillsigurdur Posted September 4, 2009 Author Share Posted September 4, 2009 No one older than 90 will be entering this sport team. Thanks bundyxc, but how will I make this to a "SELECT FROM 'blahblah' sort by 'icelandic_id_number'"; Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-912531 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 What are you trying to do? The script I wrote will take any Icelandic ID and distinguish the four values from it (day/month/year/other). Are you trying to input the four values into a database, or do you have a full Icelandic ID in the database that you need seperated? If you let me know what you're doing, I can help you out. Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-912657 Share on other sites More sharing options...
egillsigurdur Posted September 5, 2009 Author Share Posted September 5, 2009 I have everything in a database, every row has an Icelandic ID number to it, 0311942919, 3108012140, 1212053280, you know. They all include the birthday of a person. I have the usual mysql_connect("###", "###", "###") or die(mysql_error()); mysql_select_db("###") or die(mysql_error()); $data = mysql_query("SELECT * FROM $table") or die(mysql_error()); and then some complex while() to print it out to an excel file. How could I do a $data = mysql_query("SELECT * FROM $table ORDER BY 'idnumber' ASC")); yet let PHP know what to sort by? First sort by year, then month, then day. I got this working in a HTML table with some weird JavaScript, but don't know how in the world I'm going to do this. Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-913360 Share on other sites More sharing options...
PFMaBiSmAd Posted September 5, 2009 Share Posted September 5, 2009 Untested but should work - ORDER BY STR_TO_DATE(LEFT(idnumber,6),'%d%m%y') Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-913362 Share on other sites More sharing options...
bundyxc Posted September 5, 2009 Share Posted September 5, 2009 See, my problem right now is that I don't know what you're trying to do, or what sort of table you're making in Excel... Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-913364 Share on other sites More sharing options...
egillsigurdur Posted September 5, 2009 Author Share Posted September 5, 2009 Pretty much just printing every single row in a table. Simply need to know how to sort them by ID number. Thanks. Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-913371 Share on other sites More sharing options...
PFMaBiSmAd Posted September 6, 2009 Share Posted September 6, 2009 The following takes into account the two digit year and will work until the year 2098, assuming you are not going to put in any values for people with a birth year greater than the current year (i.e. those yet to be born) - SELECT *,CONCAT_WS('-',IF(SUBSTRING(idnumber,5,2) > SUBSTRING(YEAR(CURDATE()),3,2),CONCAT('19',SUBSTRING(idnumber,5,2)),CONCAT('20',SUBSTRING(idnumber,5,2))),SUBSTRING(idnumber,3,2),LEFT(idnumber,2)) as date FROM your_table ORDER BY date Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-913414 Share on other sites More sharing options...
egillsigurdur Posted September 7, 2009 Author Share Posted September 7, 2009 Awesome!!! Thank you so much! Link to comment https://forums.phpfreaks.com/topic/173078-solved-sort-info-by-icelandic-id-number/#findComment-913984 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.