#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Events Calendar advice requested


    Hi guys,
    Im tinkering with an events calendar for our railway club. The code so far:
    PHP Code:
    <?php
    /* Open up a connection to the mysql database on the same server as website */
    $db_link mysql_connect("localhost""root""")    or die("Unable to connect to mysql database"); 
        
    /* Select our database (there is more than one in my server) */
    mysql_select_db("trains"$db_link);
     
    /* draws a calendar */function draw_calendar($month,$year,$events = array()){ 
        
    /* draw table */    $calendar '<table cellpadding="0" cellspacing="0" class="calendar">';   
      
    /* table headings */    $headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); 
       
    $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';
         
    /* days and weeks vars now ... */    $running_day date('w',mktime(0,0,0,$month,1,$year)); 
       
    $days_in_month date('t',mktime(0,0,0,$month,1,$year)); 
       
    $days_in_this_week 1
       
    $day_counter 0;  
      
    $dates_array = array();   
      
    /* row for week one */ 
       
    $calendar.= '<tr class="calendar-row">';  

       
    /* print "blank" days until the first of the current week */   
     
    for($x 0$x $running_day$x++):   
         
    $calendar.= '<td class="calendar-day-np">&nbsp;</td>';
            
    $days_in_this_week++; 
       endfor; 
        
    /* keep going with days.... */
        
    for($list_day 1$list_day <= $days_in_month$list_day++): 
       
    $calendar.= '';
    /* add leading zero in the day number */    
    if($list_day 10) {  
           
    $list_day str_pad($list_day2'0'STR_PAD_LEFT);     
        }
    /* add leading zero in the month number */ 
       
    if($month 10) {     
        
    $month str_pad($month2'0'STR_PAD_LEFT);
             }
         
    $event_day $year.'-'.$month.'-'.$list_day
             
    $calendar.= '<td class="calendar-day"><div style="position:relative;height:100px;">';   
               
    /* add in the day number */       
         
    $calendar.= '<div class="day-number">'.$list_day.'</div>';                     
       
    $event_day $year.'-'.$month.'-'.$list_day;    
            echo 
    $event_day;  
              echo 
    "<br />";            if(isset($events[$event_day])) { 
                   foreach(
    $events[$event_day] as $event) {           
             
    $calendar.= '<div class="event">'.$event['title'].'</div>';       
             }   
             }   
             else {             
       
    $calendar.= str_repeat('<p>&nbsp;</p>',2);         
       }        
    $calendar.= '</div></td>';   
         if(
    $running_day == 6):  
              
    $calendar.= '</tr>';
                if((
    $day_counter+1) != $days_in_month):   
                 
    $calendar.= '<tr class="calendar-row">';   
             endif;       
         
    $running_day = -1
               
    $days_in_this_week 0;    
        endif;   
         
    $days_in_this_week++; 
    $running_day++; 
    $day_counter++;
        endfor;    
     
    /* finish the rest of the days in the week */ 
       
    if($days_in_this_week 8):   
         for(
    $x 1$x <= ($days_in_this_week); $x++):      
          
    $calendar.= '<td class="calendar-day-np">&nbsp;</td>';      
      endfor;  
      endif;   
      
    /* final row */    
    $calendar.= '</tr>';
              
    /* end the table */
        
    $calendar.= '</table>';    
     
    /** DEBUG **/   
     
    $calendar str_replace('</td>','</td>'."\n",$calendar);
        
    $calendar str_replace('</tr>','</tr>'."\n",$calendar);  
           
    /* all done, return result */
        
    return $calendar;
    }
     function 
    random_number() {    
    srand(time());  
      return (
    rand() % 7);
    }
     
    /* date settings */
    $month = (int) ($_GET['month'] ? $_GET['month'] : date('m'));$year = (int)  ($_GET['year'] ? $_GET['year'] : date('Y'));
     
    /* select month control */
    $select_month_control '<select name="month" id="month">';
    for(
    $x 1$x <= 12$x++) {    $select_month_control.= '<option value="'.$x.'"'.($x != $month '' ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
    }
    $select_month_control.= '</select>'
    /* select year control */
    $year_range 7;
    $select_year_control '<select name="year" id="year">';
    for(
    $x = ($year-floor($year_range/2));
     
    $x <= ($year+floor($year_range/2));
     
    $x++) {
       
    $select_year_control.= '<option value="'.$x.'"'.($x != $year '' ' selected="selected"').'>'.$x.'</option>';}$select_year_control.= '</select>';
     
    /* "next month" control */
    $next_month_link '<a href="?month='.($month != 12 $month 1).'&year='.($month != 12 $year $year 1).'" class="control">Next Month &gt;&gt;</a>'
    /* "previous month" control */
    $previous_month_link '<a href="?month='.($month != $month 12).'&year='.($month != $year $year 1).'" class="control">&lt;&lt;  Previous Month</a>'
     
    /* bringing the controls together */
    $controls '<form method="get">'.$select_month_control.$select_year_control.'&nbsp;<input type="submit" name="submit" value="Go" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$previous_month_link.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$next_month_link.' </form>'

    /* get all events for the given month    I had to rewrite this query to get    anything usable out of the mysql    database we already had. */ 
    $events = array();
    $query = (" SELECT event_name DATE_FORMAT( FROM_UNIXTIME(event_date), '%Y-%m-%d' )   
      AS event_date   FROM     calendar_events   WHERE     FROM_UNIXTIME(event_date)     
      LIKE '
    $year-%$month-%'");
     
    /* verify the query is correct */
    echo $query;echo "<hr />";echo "<br />";         
    $result mysql_query($query,$db_link) or die('error 2');while($row mysql_fetch_assoc($result)) { 
      
    $events[$row['event_date']][] = $row
    /* verify that the query gets results.
        Also generates a list of this months events */   
     
    echo $row['event_name']." ----- ".$row['event_shortdesc']; ----- ".$row['event_date'];"
       
    echo "<br />";
    }echo 
    "<br />";
    echo 
    "<br />";
     echo 
    '<h2 style="float:left; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>';
    echo 
    '<div style="float:left;">'.$controls.'</div>';
    echo 
    '<div stye="clear:both;"></div>';echo draw_calendar($month,$year,$events);
    echo 
    '<br /><br />';
    ?>
    Having serval issues:
    Line 81 and 82 which deals with getting the month and year are coming as undfined varibles and I cant figure out why.
    The SQL statement is echoing out on to the screen, again, puzzled as to why.
    Finally, Im getting this on line 111(where Im echoing the $row) Parse error: syntax error, unexpected '--' (T_DEC) in C:\Program Files (x86)\EasyPHP-12.1\www\calendar1.php on line 111.
    Im a otal newbie at PHPdebugging so apologies for the slightly stupid questions. If you spot any glaring errors that I have missed, feel free to flag them.
    Thanking you in advance!
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    Line 81 and 82 which deals with getting the month and year are coming as undfined varibles and I cant figure out why.
    PHP Code:
    $month = (int) ($_GET['month'] ? $_GET['month'] : date('m')); 
    You use $_GET['month'] without checking to see whether it exists. To check whether a variable exists, you must use isset or !empty, otherwise you will receive a notice about the variable being undefined.

    The SQL statement is echoing out on to the screen, again, puzzled as to why.
    PHP Code:
    $query = (" SELECT event_name DATE_FORMAT( FROM_UNIXTIME(event_date), '%Y-%m-%d' )   
      AS event_date   FROM     calendar_events   WHERE     FROM_UNIXTIME(event_date)     
      LIKE '
    $year-%$month-%'");
     
    /* verify the query is correct */
    echo $query
    Your code says echo $query

    Finally, Im getting this on line 111(where Im echoing the $row) Parse error: syntax error, unexpected '--' (T_DEC) in C:\Program Files (x86)\EasyPHP-12.1\www\calendar1.php on line 111.
    PHP Code:
    echo $row['event_name']." ----- ".$row['event_shortdesc']; ----- ".$row['event_date'];" 
    Everything after the first ; in this line is syntactically wrong, but you seem to know how to use echo fairly well so just try rewriting that.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    [QUOTE=E-Oreo]
    PHP Code:
    $month = (int) ($_GET['month'] ? $_GET['month'] : date('m')); 
    You use $_GET['month'] without checking to see whether it exists. To check whether a variable exists, you must use isset or !empty, otherwise you will receive a notice about the variable being undefined.

    Thanks for the advice thus far. Ive corrected te $row and the SQL string thing(I think!)

    Issets have always got me in a muddle. Can you explain a bit more about how to use them in this context?
    Addtional error has cropped up, too. "echo '<h2 style="float:left; padding-right:30px;">'.date('F',mktime(0,0,0,$month,1,$year)).' '.$year.'</h2>';
    Unexpected float, T-STRING error. I suspect a misplaced quote might be the culprit here, but when I change it the rest of thr code crys foul! How to fix this would also be helpful.
    (PHP is one of those languages Im finding when it works well, is streets ahead of most others but when it goes wrong can be a right pain in the ahem!)
  6. #4
  7. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    PHP will throw an E_NOTICE every time it encounters an undefined variable or array index while evaluating an expression.

    isset and empty are special because they are not actually functions, they are language tokens. They have more in common with if() and while() than they do with fopen() and print(). For this reason, it is possible to use them to determine whether a variable or array index is defined without triggering an E_NOTICE. They do not follow the same rules as functions, which means, for example, that you cannot do this: isset($x=null) Similarly, you should not use is_null($_GET['month']) to test whether a variable is defined, because is_null will still trigger an E_NOTICE in this situation.

    A ternary if operator consists of three sub-expressions, only two of which are evaluated:
    Code:
    (exp1) ? (exp2) : (exp3)
    exp1 is always evaluated. This is why putting an undefined variable here will cause an E_NOTICE to be thrown. When you are using isset with a ternary if operator, it is used for this first expression.

    exp2 is only evaluated if exp1 is true. Thus, exp2 may contain undefined variables and will not throw an E_NOTICE as long as exp1 is not true. The inverse is true of exp3.

    ----

    Using an editor with PHP syntax highlighting will help you identify syntax errors. The line you posted does not contain a syntax error, try looking on the line above it.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Using an editor with PHP syntax highlighting will help you identify syntax errors. The line you posted does not contain a syntax error, try looking on the line above it.
    Thank you for that useful explanation there. Am now using a editor with syntax highlighting so am hoping that and having had a few hours sleep(and coffee lol!) should solve it! Will look now at that line.

IMN logo majestic logo threadwatch logo seochat tools logo