Monthly calendar with sql data checked (Help need)

Discussion in 'PHP' started by KingCobra, May 16, 2009.

  1. #1
    [​IMG]

    Here is my code:

    
    <?php
    function showCalendar(){
        // Get key day informations. 
        // We need the first and last day of the month and the actual day
        $today    = getdate();
        $firstDay = getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
        $lastDay  = getdate(mktime(0,0,0,$today['mon']+1,0,$today['year']));
        
        
        // Create a table with the necessary header informations
        echo '<table>';
        echo '  <tr><th colspan="7">'.$today['month']." - ".$today['year']."</th></tr>";
        echo '<tr class="days">';
        echo '  <td>Mo</td><td>Tu</td><td>We</td><td>Th</td>';
        echo '  <td>Fr</td><td>Sa</td><td>Su</td></tr>';
        
        
        // Display the first calendar row with correct positioning
        echo '<tr>';
        for($i=1;$i<$firstDay['wday'];$i++){
            echo '<td>&nbsp;</td>';
        }
        $actday = 0;
        for($i=$firstDay['wday'];$i<=7;$i++){
            $actday++;
            if ($actday == $today['mday']) {
                $class = ' class="actday"';
            } else {
                $class = '';
            }
            echo "<td$class>$actday</td>";
        }
        echo '</tr>';
        
        //Get how many complete weeks are in the actual month
        $fullWeeks = floor(($lastDay['mday']-$actday)/7);
        
        for ($i=0;$i<$fullWeeks;$i++){
            echo '<tr>';
            for ($j=0;$j<7;$j++){
                $actday++;
                if ($actday == $today['mday']) {
                    $class = ' class="actday"';
                } else {
                    $class = '';
                }
                echo "<td$class>$actday</td>";
            }
            echo '</tr>';
        }
        
        //Now display the rest of the month
        if ($actday < $lastDay['mday']){
            echo '<tr>';
            
            for ($i=0; $i<7;$i++){
                $actday++;
                if ($actday == $today['mday']) {
                    $class = ' class="actday"';
                } else {
                    $class = '';
                }
                
                if ($actday <= $lastDay['mday']){
                    echo "<td$class>$actday</td>";
                }
                else {
                    echo '<td>&nbsp;</td>';
                }
            }
            
            
            echo '</tr>';
        }
        
        echo '</table>';
    }
    
    showCalendar();
    ?>
    
    Code (markup):
    Here is my sample mysql table and data (yyyy-mm-dd):
    Table name: post_table
    Field name: post_date

    post_date
    ----------
    2009-05-01
    2009-05-01
    2009-05-02
    2009-05-03
    2009-05-04
    2009-05-04
    2009-05-05
    2009-05-06
    2009-05-29
    2009-05-30

    I want to make a query and make a date linked in calendar if atleast a post exists in that same day. SEE THE IMAGE

    PLEASE HELP ME
     
    KingCobra, May 16, 2009 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    - Variable with the date

    - Loop Through table with mysql_fetch_array

    - Do the check!

    Something like;
    <?php
    $d = '2009-05-01';//date('Y-m-d') for the current
    $query = mysql_query("SELECT * FROM `post_table`") or die(mysql_error());
    while($r = mysql_fetch_array($query)){
    if($r['post_date']==$d){
    //Add link?
    }
    }
    Code (markup):
    Hope this helps a bit?
     
    Sky AK47, May 16, 2009 IP
  3. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Can't anybody complete the code with my requirment?
    I need it highly
     
    KingCobra, May 16, 2009 IP
  4. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #4
    Wtf...did you even READ my post? Also I don't understand why you need help with this while the code you wrote is more complicated.
     
    Sky AK47, May 16, 2009 IP