Welcome Guest, Not a member yet? Register   Sign In
Decade Range based on Year
#1

[eluser]weetstraw[/eluser]
Right now, I'm using a rather manual approach to defining the date range of decade based on the given year. Does anyone know a smarter solution?


Code:
function awards()
{
    $year=$this->uri->segment(3);
    
    if(count($this->Project_model->awardsList($year)) < 1) {
        $year = '2009';
    }
    
    switch ($year[2]) {
        case(0):
            $min=2000;
            $max=2009;
            break;
            
        case(9):
            $min=1990;
            $max=1999;
            break;
        
        case(8):
            $min=1980;
            $max=1989;
            break;
        
        case(7):
            $min=1970;
            $max=1979;
            break;
            
        case(6):
            $min=1960;
            $max=1969;
            break;
    }
    
    $data=array(
        'css'             => array('projects'),
        'menu'             => 'projects',
        'active'        => $this->uri->segment(2),
        'awards'        => $this->Project_model->awardsList($year),
        'year'            => $year,
        'years'            => $this->Project_model->awardYear($min, $max)
    );
    
    $this->load->view('projects-awards_view', $data);
}
#2

[eluser]Ben Edmunds[/eluser]
You can use mktime() and date().

You should be able to find everything you need here: http://php.net/manual/en/function.date.php


If your still not sure after reading those let me know and I'll give you some code to go off of.
#3

[eluser]jedd[/eluser]
If $year is always four digits, you could just substr the first 3, and append a 0 to get $min and a 9 to get $max.
#4

[eluser]sophistry[/eluser]
Code:
$min=$year[0].$year[1].$year[2].'0';
$max=$year[0].$year[1].$year[2].'9'

OR as jedd suggested:
Code:
$yr3=substr($year,0,3);
$min=$yr3.'0';
$max=$yr3.'9';
#5

[eluser]jedd[/eluser]
In the interests of total boredom over here (plus some exasperation as I try to get my head around jquery ui - and am failing dismally) here's some thoughts.

My re-interpretation of your code:

Code:
function  awards ( $year = '9999' )
    if ($year > date ("Y"))
        $year = date ("Y");
    
    $data=array(
        'css'           => array('projects'),
        'menu'          => 'projects',
        'active'        => $this->uri->segment(2),
        'awards'        => $this->Project_model->awardsList($year),
        'year'          => $year,
        'years'         => $this->Project_model->awardYear(substr($year,0,3) . '0' , substr($year,0,3) . '9')
    );
    
    $this->load->view('projects-awards_view', $data);
}



Assumptions I don't like :

The year you're getting as the first parameter of your controller method is sane - it isn't necessarily going to be. Hopefully your model method sensibly checks sanity, and/or escapes the input data. I'm a big fan of is_numeric(). Where you do your data validity checks is a matter of debate - I'm happy to do some in the controller - so I'd probably do the numeric & 4-character strlen tests here.

When you call : $this->Project_model->awardsList($year) -- you're assuming that if the year is current, you'll get results. Hopefully you're handling a lack of results elegantly in your view, but you might want to handle them better in here - just setting the year back to 2009 and then calling that same model method again later seems .. inelegant.


Notes:

Min/max creation - could also be done in three lines this way:
Code:
$min = $max = $year;
$min[3] = '0';
$max[3] = '9';

I suspect performance deltas on any of the options presented are trivial .. but they're all less ugly (and more versatile) than the switch() approach Wink




Theme © iAndrew 2016 - Forum software by © MyBB