EricOnAdventure Posted May 26, 2016 Share Posted May 26, 2016 I am creating an automatic English Resume writing program for non-Native English speakers and all has went well so far, but some people simply don't understand that the resume should be reverse chronological so i need to sort the jobs by year. Each job looks something like this $JOBDISPLAY10a= "<div><div class='colleft'>". $Jobtitle10a.", ". $Company10a.", ". $Jobcountry10a."</div><div class='colright'>". $JOBSTART10a." - ".$JOBFINISH10a."</div></div>".$jobdutyfull10a. "<br>"; I then echo $JOBDISPLAY10a echo $JOBDISPLAY11a ect. I know that this is pretty good foundation to sort the jobs but I am unsure of how to sort the echo $JOBDISPLAYX variables by the $JOBFINISHX variables, especially since $JOBFINISHX could = Present. What makes this even tougher is that multiple jobs may be listed as "present" Meaning that I would have to order them, and only them, with $JOBSTARTX. I've tried various approaches but I cannot get it to function in a reverse chronological sense with Present at the top. Any ideas? Thanks -Eric Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted May 26, 2016 Solution Share Posted May 26, 2016 If you have the data in a database, sort it when you retrieve the date using an ORDER BY clause. Otherwise, don't use variables like $JOBDISPLAY10, $JOBDISPLAY11, use an array and sort the array. EG <?php $jobdisplay = [ 10 => [ 'title' => 'Job title 10', 'company' => 'Company name 10', 'country' => 'Somewhere', 'start' => '2016-05-01', 'finish' => 'present', 'duties' => 'description of duties ...' ], 11 => [ 'title' => 'Job title 11', 'company' => 'Company name 11', 'country' => 'Somewhere else', 'start' => '2014-01-01', 'finish' => '2014-12-31', 'duties' => 'another description of duties ...' ], 12 => [ 'title' => 'Job title 12', 'company' => 'Company name 12', 'country' => 'Somewhere', 'start' => '2016-04-01', 'finish' => 'present', 'duties' => 'yet another description of duties ...' ], 13 => [ 'title' => 'Job title 13', 'company' => 'Company name 13', 'country' => 'Somewhere other', 'start' => '2015-01-01', 'finish' => '2016-03-31', 'duties' => 'and yet another description of duties ...' ] ]; function customSort($a, $b) { $finA = $a['finish']=='present' ? date('Y-m-d') : $a['finish']; $finB = $b['finish']=='present' ? date('Y-m-d') : $b['finish']; $x = strcmp($finB, $finA); // if same finish dates, sort by start date if ($x==0) { return strcmp($b['start'], $a['start']); } return $x; } usort($jobdisplay, 'customSort'); echo '<pre>'; foreach ($jobdisplay as $j) { printf('%-20s | %-15s | %-15s<br>', $j['title'], $j['start'], $j['finish']); } echo '</pre>'; ? Gives Job title 10 | 2016-05-01 | present Job title 12 | 2016-04-01 | present Job title 13 | 2015-01-01 | 2016-03-31 Job title 11 | 2014-01-01 | 2014-12-31 1 Quote Link to comment Share on other sites More sharing options...
EricOnAdventure Posted May 27, 2016 Author Share Posted May 27, 2016 Wow, ingenious! Thanks for your expertise Barand. 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.