ReyC Posted March 16, 2008 Share Posted March 16, 2008 Background: I'm very much a php noob, but am a very good bodger of most languages/systems... I'm currently attempting an import of all our students into our Moodle setup from SIMS.net via a very bodged up version of David Willingtons script. The users are authenticated via AD, and as such I needed to be able to make up the usernames in the script from what SIMS has provided. The format we have for users is: <registration group>-<firstname> EG. jy-james Which would be fine, except for those cases where we have multiple students with the same first name in the same form (and it seems we have a lot). In these cases, the username format for all affected users is: <registration group>-<firstname><initial of surname> The data I'm working with includes multiple copies of a username, one copy for each class or subject he/she is involved with. What do I need? To be able to implement a check which is able to detect duplicate usernames and apply the "fixed" format of appending the initial of the students surname. Suggestions very very welcome! I'm currently looking at running loops to go thru all usernames and compare all surnames but this seems unwieldy and very basic, and I'm wondering if there might be some clever php god who might know of some array functions/tweaks that would make this a lot easier/neater... Rey A very small example of the data held is given below just in case that helps. Username Surname FirstName Class Subject je-arron bulldog Arron 10C/Bi1 Biology je-arron bulldog Arron 10E/Eg1 Engine je-arron bulldog Arron 10B/Bi1 Biology je-arron bulldog Arron 10X/Pe4 P. E. je-arron bulldog Arron 10E/Eg1 Engine je-arron bulldog Arron 10A/Ph1 Physics je-arron bulldog Arron 10X/Se4 PSE. je-arron bulldog Arron 10B/Ch1 Chemistry je-arron bulldog Arron 10C/Ch1 Chemistry je-arron bulldog Arron 10F/It1 I.T. je-arron bulldog Arron 10F/It1 I.T. je-arron bulldog Arron 10X/En3 English je-arron bulldog Arron 10X3/Ma Maths je-arron bulldog Arron 10F/It1 I.T. je-arron bulldog Arron 10C/Ph1 Physics je-arron bulldog Arron 10A/Ch1 Chemistry je-arron bulldog Arron 10D/Eg1 Engine je-arron trimm Arron 10A/Hu1 Hums je-arron trimm Arron 10X/Pe2 P. E. je-arron trimm Arron 10C/Rs1 Religious je-arron trimm Arron 10E/Sc3 Sci. je-arron trimm Arron 10X2/Ma Maths je-arron trimm Arron 10X/En2 English je-arron trimm Arron 10E/Sc3 Sci. je-arron trimm Arron 10E/Sc3 Sci. je-arron trimm Arron 10F/Sc3 Sci. je-arron trimm Arron 10C/Rs1 Religious je-arron trimm Arron 10B/It1 I.T. je-arron trimm Arron 10E/Sc3 Sci. je-arron trimm Arron 10D/Gy1 Geog. je-arron trimm Arron 10F/Sc3 Sci. je-arron trimm Arron 10F/Sc3 Sci. je-arron trimm Arron 10F/Sc3 Sci. je-arron trimm Arron 10X/Se4 PSE. je-arron trimm Arron 10B/It1 I.T. Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 16, 2008 Share Posted March 16, 2008 I think your be able to do all this by adding the users date off birth to the database aswell............ Quote Link to comment Share on other sites More sharing options...
redarrow Posted March 16, 2008 Share Posted March 16, 2008 Sorry got it, Just add there NI number then each child is defined as soly them...... Quote Link to comment Share on other sites More sharing options...
Barand Posted March 16, 2008 Share Posted March 16, 2008 I modified your sample to include a couple that aren't affected je-arron bulldog Arron 10C/Bi1 Biology je-arron bulldog Arron 10E/Eg1 Engine je-arron bulldog Arron 10B/Bi1 Biology je-arron bulldog Arron 10X/Pe4 P. E. je-arron bulldog Arron 10E/Eg1 Engine je-arron bulldog Arron 10A/Ph1 Physics je-arron bulldog Arron 10X/Se4 PSE. je-arron bulldog Arron 10B/Ch1 Chemistry je-arron trimm Arron 10C/Rs1 Religious je-arron trimm Arron 10B/It1 I.T. je-arron trimm Arron 10E/Sc3 Sci. je-arron trimm Arron 10D/Gy1 Geog. je-arron trimm Arron 10F/Sc3 Sci. je-arron trimm Arron 10F/Sc3 Sci. je-arron trimm Arron 10F/Sc3 Sci. je-arron trimm Arron 10X/Se4 PSE. je-arron trimm Arron 10B/It1 I.T. je-clark kent Clark 10C/Ch1 Chemistry je-clark kent Clark 10F/It1 I.T. je-clark kent Clark 10F/It1 I.T. je-clark kent Clark 10X/En3 English je-clark kent Clark 10X3/Ma Maths je-clark kent Clark 10F/It1 I.T. je-clark kent Clark 10C/Ph1 Physics je-clark kent Clark 10A/Ch1 Chemistry je-clark kent Clark 10D/Eg1 Engine je-harry potter Harry 10A/Hu1 Hums je-harry potter Harry 10X/Pe2 P. E. je-harry potter Harry 10C/Rs1 Religious je-harry potter Harry 10E/Sc3 Sci. je-harry potter Harry 10X2/Ma Maths je-harry potter Harry 10X/En2 English je-harry potter Harry 10E/Sc3 Sci. je-harry potter Harry 10E/Sc3 Sci. je-harry potter Harry 10F/Sc3 Sci. and assumed it's in a file called "register.txt" <?php /*********** * read file and put in array * key = lastname_firstname, value = username * * This will give one entry for each student */ $students = array(); $fp = fopen ('register.txt', 'r'); while ($data = fscanf($fp, '%s %s %s %s %s')) { $students[$data[1].'_'.$data[2]] = $data[0]; } fclose ($fp); /*************** * now we have * * $students = Array * ( * [bulldog_Arron] => je-arron * [trimm_Arron] => je-arron * [kent_Clark] => je-clark * [potter_Harry] => je-harry * ) * * Find duplicate entries */ $counts = array_count_values($students); foreach ($counts as $username => $count) { if ($count > 1) { append_initial ($students, $username); } } /********** * now we have * * Array * ( * [bulldog_Arron] => je-arronb * [trimm_Arron] => je-arront * [kent_Clark] => je-clark * [potter_Harry] => je-harry * ) * * which can be re-applied to the original data */ /**************************************** * function to append intial of surname *****************************************/ function append_initial (&$students, $username) { // get duped items $dupes = array_keys ($students, $username); foreach ($dupes as $key) { // append initial $students[$key] .= $key[0]; } } ?> Quote Link to comment Share on other sites More sharing options...
ReyC Posted March 16, 2008 Author Share Posted March 16, 2008 See I knew there'd be some clever wotsit Barand that looks awesome, I shall investigate tomorrow, but on inspection it'll looks like it'll work like a charm - I've literally spent hours looking at this and getting used to how php structures get put together. I'm under some serious time pressure and am very very appreciative! Wheres the /buys drink button on this ? /bow Rey Quote Link to comment Share on other sites More sharing options...
ReyC Posted March 17, 2008 Author Share Posted March 17, 2008 Just tried this out, incorporated it into the rest of my scriptage and it works perfectly. Many many thanks Barand...now I all have to do is work out how it works, I've not used the function parameters with the '&' feature until now! Rey Quote Link to comment Share on other sites More sharing options...
Barand Posted March 17, 2008 Share Posted March 17, 2008 The "&" passes the array "by reference" so the original array is updated from within the function. Without the "&" a copy of the array would have been passed. Quote Link to comment 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.