dbert Posted May 13, 2021 Share Posted May 13, 2021 (edited) Hello this code is supposed to sort by year and month, but it doesn't. It sort by year and months are random. Any suggestions? Many thanks, $sortdata =[]; $namecolumn = array_column($data, '0'); foreach($data as $datecolumn){ $dateexplode = explode('/', $datecolumn[3]); $sortdata[] = (isset($dateexplode[1])) ? $dateexplode[1] : ((isset($dateexplode[0])) ? $dateexplode[0] : ''); } array_multisort($namecolumn, (($sorttype=='DESC') ? SORT_DESC : SORT_ASC), $sortdata, (($sorttype=='DESC') ? SORT_DESC : SORT_ASC), $data); Edited May 13, 2021 by Barand code tags added Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/ Share on other sites More sharing options...
Barand Posted May 13, 2021 Share Posted May 13, 2021 4 minutes ago, dbert said: Any suggestions? None with out knowing what the data you are processing looks like. Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/#findComment-1586527 Share on other sites More sharing options...
gw1500se Posted May 13, 2021 Share Posted May 13, 2021 Also please use the code icon (<>) in the menu and select PHP. Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/#findComment-1586528 Share on other sites More sharing options...
dbert Posted May 13, 2021 Author Share Posted May 13, 2021 Can this help? deb Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/#findComment-1586529 Share on other sites More sharing options...
dbert Posted May 14, 2021 Author Share Posted May 14, 2021 and this is where the data is entered: <?php echo $this->Form->select('ContinuingEducationActivity/month', $monthArray, array('id' => 'month', )) ?> <?php echo $this->Form->select('ContinuingEducationActivity/year', $endYearArray, array('id' => 'year', 'value'=>$default_year)) ?> and this is one of the controller: function global_init(){ $monthArray = array( NULL => '', '01' => '01', '02'=> '02', '03'=> '03', '04'=> '04', '05'=> '05', '06'=> '06', '07'=> '07', '08'=> '08', '09'=> '09' , '10'=> '10' , '11' => '11', '12' => '12' ); $this->set('monthArray', $monthArray ); $endYearArray = array( 'Present'=>'Present', '1950'=>'1950', '1951'=>'1951', '1952'=>'1952', '1953'=>'1953', '1954'=>'1954', '1955'=>'1955', '1956'=>'1956', '1957'=>'1957', '1958'=>'1958', '1959'=>'1959', '1960'=>'1960', '1961'=>'1961', '1962'=>'1962', '1963'=>'1963', '1964'=>'1964', '1965'=>'1965', '1966'=>'1966', '1967'=>'1967', '1968'=>'1968', '1969'=>'1969', '1970'=>'1970', '1971'=>'1971', '1972'=>'1972', '1973'=>'1973', '1974'=>'1974', '1975'=>'1975', '1976'=>'1976', '1977'=>'1977', '1978'=>'1978', '1979'=>'1979', '1980'=>'1980', '1981'=>'1981', '1982'=>'1982', '1983'=>'1983', '1984'=>'1984', '1985'=>'1985', '1986'=>'1986', '1987'=>'1987', '1988'=>'1988', '1989'=>'1989', '1990'=>'1990', '1991'=>'1991', '1992'=>'1992', '1993'=>'1993', '1994'=>'1994', '1995'=>'1995', '1996'=>'1996', '1997'=>'1997', '1998'=>'1998', '1999'=>'1999', '2000'=>'2000', '2001'=>'2001', '2002'=>'2002', '2003'=>'2003', '2004'=>'2004', '2005'=>'2005', '2006'=>'2006', '2007'=>'2007', '2008'=>'2008', '2009'=>'2009', '2010'=>'2010', '2011'=>'2011', '2012'=>'2012', '2013'=>'2013', '2014'=>'2014', '2015'=>'2015', '2016'=>'2016', '2017'=>'2017', '2018'=>'2018', '2019'=>'2019', '2020'=>'2020', '2021'=>'2021', '2022'=>'2022', '2023'=>'2023', '2024'=>'2024', '2025'=>'2025', '2026'=>'2026', '2027'=>'2027', '2028'=>'2028', '2029'=>'2029', '2030' ); $this->set('endYearArray', $endYearArray ); $this->set('default_year', date('Y')); } Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/#findComment-1586530 Share on other sites More sharing options...
Barand Posted May 14, 2021 Share Posted May 14, 2021 Curiously your table structure (from phpMyAdmin) shows year and month in separate columns yet your code is exploding datecolumn using "/"? What does this output... echo '<pre>'; var_export($data); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/#findComment-1586538 Share on other sites More sharing options...
dbert Posted May 14, 2021 Author Share Posted May 14, 2021 Hi Barand, it is because of this: if( $report_type < 5) { $array_data[$i][0] = $row['role'] .' '. $row['role_other'] ; $array_data[$i][1] = $row['description'] ; $array_data[$i][2] = ($row['is_UBC'] ? 'Yes' : null ) ; if ( $row['month'] <> '') $row['month'] = $row['month'] . '/'; $array_data[$i][3] = ltrim($row['month'] .''. $row['year']) ; $array_data[$i][100] = false; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/#findComment-1586547 Share on other sites More sharing options...
dbert Posted May 14, 2021 Author Share Posted May 14, 2021 and this is how report looks like, you will notice that the months are scattered all over the place. Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/#findComment-1586548 Share on other sites More sharing options...
Barand Posted May 14, 2021 Share Posted May 14, 2021 Why can it not be as simple as ... $res = $db->query("SELECT user_id , month , year , description , CASE is_UBC WHEN 1 THEN 'Yes' ELSE '' END as ubc FROM dbert ORDER BY year, month "); $tdata = ''; foreach ($res as $r) { $date = sprintf("%02d/%4d", $r['month'], $r['year']); $tdata .= "<tr><td>{$r['user_id']}</td> <td>{$r['description']}</td> <td>{$r['ubc']}</td> <td>$date</td> </tr>\n"; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf8"> <title>Sample</title> <style type='text/css'> table { width: 800px; border-collapse: collapse; } th, td { padding: 5px; } </style> </head> <body> <table border="1"> <tr><th>User ID</th> <th>Description</th> <th>UBC</th> <th>Date</th> </tr> <?=$tdata?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/#findComment-1586549 Share on other sites More sharing options...
dbert Posted May 14, 2021 Author Share Posted May 14, 2021 Let me test it, thanks Barand. Quote Link to comment https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/#findComment-1586550 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.