Display data by selecting year only using onChangeMonthYear

532 views Asked by At

I'm currently doing a project that will display daily data that needs the yy-mm-dd (e.g.: 2013-06-01), monthly data that needs yy-mm (e.g.: 2013-06) and yearly data that needs only the year (e.g.: 2013). My problem is:

  1. When you select a year (ui-datepicker-year), it should display the data for the whole year based on the year selected (e.g., selected year is 2013. So it should display data from 2013-01 to 2013-12).

So this is the jquery:

<script>
$(function() {
    $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'dd MM yy',

            //SELECTED DATE: 2013-06-01
            onSelect: function(dateText, inst) { 
                $.ajax({
                    type: "POST",
                    url: "content.php",
                    data: { "datepick" : dateText, "type" : "all"},
                    success: function(html) {
                        $("#content").empty().append(html);
                    }
                });
            },

            onChangeMonthYear: function(year, month, inst){
                $.ajax({
                    type: "POST",
                    url: "content.php",
                    data: { "datepick" : month, "type" : "month", "year" : year},
                    success: function(html) {
                        $("#content").empty().append(html);
                    }
                });
            }
    });
});
</script>
<div id="datepicker"> </div>

And this is the php file (content.php):

if(isset($_POST['datepick']) && !empty($_POST['datepick'])) {
    if($_POST['type']=="month") {
        $where = "MONTH(dbDate) = '{$_POST['datepick']}' AND YEAR(dbDate) = '{$_POST['year']}'";
    }
    else {
        $date = date_create($_POST['datepick']);
        $calendarDate = date_format($date, 'Y-m-d');
        $where = "DATE(dbDate) = '{$calendarDate}'";
    }
}
else {
    $calendarDate = date('Y-m-d');
    $where = "DATE(dbDate) = '{$calendarDate}'";
}

$sql = "SELECT name FROM db WHERE {$where};
while($row = mysql_fetch_array($query))
{
    $name = $row['name'];
}

But I don't know what to use for the yearly data. I've tried using if-else condition for the script but does not work:

onChangeMonthYear: function(year, month, inst){
    if($('.ui-datepicker-month :selected').click(function(){
        var month = $('.ui-datepicker-month :selected').val();
        var year = $('.ui-datepicker-year :selected').val();

        $.ajax({
        type: "POST",
        url: "content.php",
        data: { "datepick" : month, "type" : "month", "year" : year},
        success: function(html) {
            $("#content").empty().append(html);
        }
    });
}));

    else if($('.ui-datepicker-year :selected').click(function(){
        var year = $('.ui-datepicker-year :selected').val();
        $.ajax({
            type: "POST",
            url: "content.php",
            data: { "datepick" : year, "type" : "year"},
            success: function(html) {
                $("#content").empty().append(html);
            }
        });
    }));
}

Also I tried adding this else if condition after the *if($_POST['type']=="month")* in content.php:

else if($_POST['type']=="year") {
    $where = "YEAR(dbDate) = '{$_POST['year']}'";
}
0

There are 0 answers