Jump to content

Recommended Posts

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 by Barand
code tags added
Link to comment
https://forums.phpfreaks.com/topic/312690-sorting-by-year-and-month/
Share on other sites

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'));

   }

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++;

                }

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>

image.png.00c6f3584370df59292be482cc32edcd.png

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.