a-ok Posted July 22, 2019 Share Posted July 22, 2019 I've got a basic AJAX submission form set up for a poll – the user enters a first and last name or a person they'd like to nominate and upon submission it stores the first and last name, as well as IP address and timestamp within a repeater row. The user can only submit, using the same IP address, once within a 24 hour period (done by cross-checking their IP with entries in the repeater as well as the difference between timestamps). I now want to return a 'live top 5' – like a leaderboard – (taking all first and last name entries and doing a 'count'). This is where I'm stuck at the most efficient way to do this. Counts need to be case-insensitive (michael jordan and MICHAEL JORDAN and miCHAel joRdaN would total three, not one each) and then should, ideally, sentence-case the returned top 5 along with the count What do you think? Thanks in advance for any advice. Link to comment Share on other sites More sharing options...
BitPoet Posted July 23, 2019 Share Posted July 23, 2019 $count_arr = []; foreach($page->my_repeater as $rep) { $name_normalized = ucwords(strtolower($rep->lastname . " " . $rep->firstname)); $count_arr[$name_normalized]++; } arsort($count_arr, \SORT_NUMERIC); $leaders = array_slice($count_arr, 0, 5); The caveats are names with prefixes, like Scottish McSomething, which will end up being Mcsomething, and that there's no sorting among names with equal vote count. 1 Link to comment Share on other sites More sharing options...
teppo Posted July 23, 2019 Share Posted July 23, 2019 1 hour ago, BitPoet said: The caveats are names with prefixes, like Scottish McSomething, which will end up being Mcsomething, ... If you're interested in ironing this quirk out as well, here's one possible solution: https://www.media-division.com/correct-name-capitalization-in-php/ ? 1 Link to comment Share on other sites More sharing options...
a-ok Posted July 23, 2019 Author Share Posted July 23, 2019 6 hours ago, teppo said: If you're interested in ironing this quirk out as well, here's one possible solution: https://www.media-division.com/correct-name-capitalization-in-php/ ? This is very cool. Much appreciated. @BitPoet Many thanks for your help. I ended up using your example and doing something like so.. $submissionsNames = array(); foreach ($page->submissions as $submission) { $name = ucwords(strtolower($submission->submissions_firstname . " " . $submission->submissions_lastname)); array_push($submissionsNames, $name); } $submissionsNames = array_count_values($submissionsNames); ksort($submissionsNames); arsort($submissionsNames); $submissionsNames = array_slice($submissionsNames, 0, 5); Let me know if you think I've missed a trick? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now