Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Event Calendar ajax problem
#1

[eluser]MarceloTM[/eluser]
Hello! I'm trying to make an event calendar for my site that needs to work as follows:
Initially the system get the current date and generate the calendar in the controller, at each day I call a function in the model passing the date (dd/mm/YYYY), to check if there are any events that day.

The initial call is working fine, this is the code:
Code:
$data['cal'] = $this->Cal_model->montaCalendario("", "");
The two empty params is to generate the actual month calendar.

So, i got two links in the calendar header, to switch months, Previous and Next.
When I click in these links, the calendar is generate ok, but the events are not searched,
the SQL always return 0 rows...

This is the jQuery ajax call to the controller:
Quote:function anterior(){
$('#event_info').html("");

var previous = $('.previous');

var month = anterior.attr('month');
var year = anterior.attr('year');

$.post("<?=base_url()?>calendario/buscarMes", { "month" : month, "year" : year },
function(data){
$('#cal_render').html(data);
});
}

This is the controller method:
Code:
function buscarMes(){
        
    $mes = trim($this->input->post('mes'));
    $ano = trim($this->input->post('ano'));    

    $result = $this->Cal_model->montaCalendario($mes, $ano);
    echo $result;
        
}

This is the method to build the calendar:
Code:
function montaCalendario($mes, $ano){
                
    $date = time();

    $day = date('d', $date);
      
    if($mes != "" && $ano != ""):
       $month = (int)$mes;
       $year = (int)$ano;
    else:
       $month = date('m', $date);
       $year = date('Y', $date);
    endif;
    
    $first_day = mktime(0,0,0, $month, 1, $year);
    
    $title = date('F', $first_day);
    
    $day_of_week = date('D', $first_day);
    
    switch($day_of_week){
        case "Sun" : $blank = 0; break;
        case "Mon" : $blank = 1; break;
        case "Tue" : $blank = 2; break;
        case "Wed" : $blank = 3; break;
        case "Thu" : $blank = 4; break;
        case "Fri" : $blank = 5; break;
        case "Sat" : $blank = 6; break;
    }
    
    $days_in_month = cal_days_in_month(0, $month, $year);
      
    if($month == '01'):
      
        $mesAnterior = '12';
        $anoAnterior = $year - 1;

        $mesProximo = $month + 1;
        $anoProximo = $year;
          
     elseif($month == '12'):
      
        $mesAnterior = $month - 1;
        $anoAnterior = $year;

        $mesProximo = '01';
        $anoProximo = $year + 1;
          
     else:
      
        $mesAnterior = $month - 1;
        $anoAnterior = $year;

        $mesProximo = $month + 1;
        $anoProximo = $year;
          
     endif;
      
     $cal = "[removed]
                $(document).ready(function(){
               $('#cal_table tr td.tip').tipsy({gravity: 'n'});
                  
            $('#cal_table tr td.active').hover(function(){
                $(this).find('span').css('display','block');
            }, function(){
                    $(this).find('span').css('display','none');
            });
             });
         [removed]<br />";
    
     $cal .= "<table id='cal_table' align='right' cellpadding='0' cellspacing='0'>";
     $cal .= "<tr><th style='font-size: 40px;' mes='".$mesAnterior."' ano='".$anoAnterior."' class='anterior'>&lt;</th>";
     $cal .= "<th colspan='5' style='font-size: 40px;'>".$this->retornaMes($month)." ".$year."</th>";
     $cal .= "<th style='font-size: 40px;' mes='".$mesProximo."' ano='".$anoProximo."' class='proximo'>&gt;</th></tr>";
     $cal .= "<tr><th style='width: 80px;'>Domingo</th>
                    <th style='width: 80px;'>Segunda</th>
                    <th style='width: 80px;'>Ter&ccedil;a</th>
                    <th style='width: 80px;'>Quarta</th>
                    <th style='width: 80px;'>Quinta</th>
                    <th style='width: 80px;'>Sexta</th>
                    <th style='width: 80px;'>S&aacute;bado</th>";
    
     $day_count = 1;
    
     $cal .= "<tr>";
    
     while($blank > 0){
         $cal .= "<td class='blank'></td>";
         $blank = $blank-1;
         $day_count++;
     }
    
     $day_num = 1;
    
     while($day_num <= $days_in_month){
           $data = $day_num."/".$month."/".$year;
              
           $cal .= $this->buscaEventoDia($data, $day_num);
            
            $day_num++;
            $day_count++;
    
             if($day_count > 7){
                $cal .= "</tr><tr>";
                $day_count = 1;
            }
     }
    
     while($day_count > 1 && $day_count <= 7){
           $cal .= "<td class='blank'></td>";
           $day_count++;
     }
    
     $cal .= "</tr></table>";
      
     return $cal;
        
}
#2

[eluser]MarceloTM[/eluser]
Continuing... (post too large...)

The line $cal .= $this->buscaEventoDia($data, $day_num); is the line that calls the methos to check if there are any events in that day, it's the following method:
Code:
function buscaEventoDia($data, $day_num){

     $query_str = "SELECT e.idevento,
              e.titulo,
              e.cidade,
              e.thumb
           FROM eventos e
           WHERE e.data = ?";
     $query = $this->db->query($query_str, $data);
        
     if($query->num_rows() == 1):
         $result = $query->row();
         return "<td style='background: url(".$result->thumb.") no-repeat center; cursor: pointer;' title='".$result->titulo."<br />".$result->cidade."'
                        onclick='carregarEvento(\"".$result->idevento."\");'
                        class='tip active'><span></span>".$day_num."</td>";
     else:
         return "<td class='active'><span></span>".$day_num."</td>";
     endif;

}

Here, it always returns 0 rows when calling the script from ajax...
I have already checked if the date is correct, tried to output the result via json, but none worked...
Can please anyone help me with this? I'm still very new to code igniter, so i can't even imagine what's going on...

Thanks in advance and sorry for the portuguese variable names, just think in "previous" when you see any "anterior" and "next" when you see any "proximo". And sorry for my broken english, i'm from Brazil. =)
#3

[eluser]MarceloTM[/eluser]
Anyone please!! It's very important to me and I really can't figure out the problem!!!
#4

[eluser]MarceloTM[/eluser]
Problem solved.
#5

[eluser]vasily[/eluser]
do you want to share with us your code. Because i am newbie and in need of an event calendar for my website.
#6

[eluser]MarceloTM[/eluser]
This was really a silly problem, and the solution is very simple.
The problem wasn't with tha ajax or the ci itself, the problem was the way the date was being passed to the sql string to be compared to the data on the DB.

To solve this, in the controller, add these lines:

Code:
if($month < 10):
    if($this->indexOf('0', $month) == -1):
    $month = "0".$month;
    endif;
endif;

while($day_num <= $days_in_month){

    if($day_num < 10):
    $day_num = "0".$day_num;
    endif;

Just two checkings to make sure the day and month have a 0 before the number, if it's lower then 10.
Look for that while loop int the code from the first post and insert those two checks.

Hope it helps.




Theme © iAndrew 2016 - Forum software by © MyBB