select all Sundays red in a calendar class?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phpsaleon
Forum Newbie
Posts: 2
Joined: Wed Mar 05, 2014 7:55 am

select all Sundays red in a calendar class?

Post by phpsaleon »

Hello,

I am new to PHP.

I would be very grateful if anyone could help me how I could get all Sundays in my calendar with red colour.

Thanks in advance.


Here is my calendar class:

Code: Select all

<?php

class CDisplayAlmanac
{
    public $day;
    public $month;
    public $year;
    public $timestamp;
    private $prevMonth;
    private $nextMonth;
    private $dayOfMonth;
    private $fullMonth;
    private $noMonth;
    private $firstDayTimeStamp;
    private $dateArray;
    private $firstDay;
    private $lengthMonth;
    private $totalSlots;
    private $endSlots;
    private $addToEnd;
    private $gridLength;
    private $displayHTML;
    
    // create values for the almanac 
    public function __construct($d = 0, $m = 0, $y = 0)
    {       
          if($d == 0)
          {
                $this->day = date("j");         
          }
          else
          {
                $this->day = $d;
          }
            if($m == 0)
            {
                $this->month = date("n");
            }
            else
            {
                $this->month = $m;  
            }
             
            if($y == 0)
            {
                $this->year = date("Y");
            }
            else
            {
                $this->year = $y;
            }

				// create values
            $this->prevYear= $this->year;
            $this->nextYear= $this->year;
            $this->prevMonth= $this->month-1;
            $this->nextMonth= $this->month+1;
            
            if ($this->prevMonth == 0 ) 
            {
                $this->prevMonth= 12;
                $this->prevYear = $this->year - 1;
            }
            if ($this->nextMonth == 13 ) 
            {
                 $this->nextMonth = 1;
                 $this->nextYear = $this->year + 1;
            }
             
            $this->timestamp = mktime(0,0,0,$this->month,$this->day,$this->year);
            $this->dayOfMonth = date('j',$this->timestamp);
            $this->fullMonth = date('F',$this->timestamp);
            $this->noMonth = date('n',$this->timestamp);
            $this->firstDayTimeStamp = mktime(0,0,0,$this->noMonth,1,$this->year);
            $this->dateArray = getdate($this->firstDayTimeStamp );
            $this->firstDay = $this->dateArray['wday'];
            
            if($this->firstDay == 0)
            {
                $this->firstDay = 6;
            }
            else
            {
                $this->firstDay--;
            }
            
            $this->lengthMonth = date('t',$this->timestamp);
            $this->totalSlots = $this->firstDay + $this->lengthMonth;
            $this->totalSlots--;
            $this->endSlots = $this->totalSlots % 7;
            $this->addToEnd = 7 - $this->endSlots;
            $this->gridLength = $this->totalSlots + $this->addToEnd;
         
    }
     
    // create table and its contents  
    public function displayAlmanac()
    {
        $this->displayHTML = "<table>";
        $this->displayHTML .= "<tr><td colspan=\"7\" class=\"almanac\"><img src='img/general.jpg' alt='image of a general'/>{$this->fullMonth} - {$this->year}</td></tr>";
        $this->displayHTML .= "<tr class=\"mainCal\"><td>Mon</td><td>Tue</td><td>Wed</td><td>Thur</td><td>Fri</td><td>Sat</td>";    
        $this->displayHTML .= "<td class='sunday'>Sun</td></tr>";
 
        for ($i=0; $i<$this->gridLength; $i++) 
        {
            if(($i % 7) == 0 )
            {
                $this->displayHTML .=  "<tr>";
            }
            // days in unselected month
            if($i < $this->firstDay || ($i) > ($this->totalSlots))
            {
                $this->displayHTML .=  "<td class='notused'></td>";
            }
            else
            {
                if(($i - $this->firstDay + 1) == $this->day)
                {
                    $this->displayHTML .=  "<td class='sunday'>". ($i - $this->firstDay + 1) . "</td>";
                }
                else
                {
                    $this->displayHTML .=  "<td>". ($i - $this->firstDay + 1) . "</td>";
                } 
                            
            }
            if(($i % 7) == 6 )
            {
                $this->displayHTML .=  "</tr>";
            }            
        }
         
        $this->displayHTML .= "<tr class=\"mainCal\">";
        $this->displayHTML .= "<td><a href=\"{$_SERVER['PHP_SELF']}?month={$this->prevMonth}&year={$this->prevYear}\"><</a></td>";
        $this->displayHTML .= "<td colspan=\"5\">Choose Month</td>";
        $this->displayHTML .= "<td><a href=\"{$_SERVER['PHP_SELF']}?month={$this->nextMonth}&year={$this->nextYear}\">></a></td>";
        $this->displayHTML .= "</tr></table>";
 
      return $this->displayHTML;     
    }  
}
?>
And my stylesheet:

Code: Select all

table
{
    font-family: Helvetica, arial;
    font-size: 22px;
}
th, td
{
    padding:20px;
    background-color: #fff;
}
tr:nth-of-type(odd)
{
    color:#000;
}
.almanac
{
    text-align:center; 
}
tr.mainCal td
{
    width:30px;
    text-align:center;
    background-color: #C7CCDF;     
}
table a:link
{
    text-decoration:none;
    color:#fff;
    background-color: #000;
	 padding: 10px;
}
table a:hover
{
    color:#6C3;
}
.pickedDay
{
    border:2px solid #f00; 
    display:inline-block;
    padding:2px;
}
.sunday
{
	color: #f00;
}
Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: select all Sundays red in a calendar class?

Post by requinix »

It looks like you already have something to do that. Is it not working? What HTML does it output?
phpsaleon
Forum Newbie
Posts: 2
Joined: Wed Mar 05, 2014 7:55 am

Re: select all Sundays red in a calendar class?

Post by phpsaleon »

Hello Requinix :)

Thank you very much for the reply.

Have managed to get the name Sunday red on the calendar, so that seems ok.

Unfortunately I have not managed to get all of Sunday's date red :(

I forgot to include the page that creates and shows the calendar object.

This is the code that creates the calendar object:

<?php
// include config.php file
include(__DIR__.'/config.php');

// store it in variables in the Linux container.
$linux['title'] = "Almanac";
// import stylesheet for Almanac
$linux['stylesheets'][] = 'css/almanac.css';

// get values for the Almanac.
if(!isset($_GET["day"]))
{
$_GET["day"] = date("j");
}
if(!isset($_GET["month"]))
{
$_GET["month"] = date("n");
}
if(!isset($_GET["year"]))
{
$_GET["year"] = date("Y");
}

// store values in corresponding variables.
$currentDay = $_GET["day"];
$currentMonth = $_GET["month"];
$currentYear = $_GET["year"];

// create Almanac object
$obj = new CDisplayAlmanac($currentDay, $currentMonth, $currentYear);
$myAlmanac = $obj->displayAlmanac();

# main contents
$linux['main'] = <<<EOD

<article class="almanac">
<h1>Almanac</h1>
<p>{$myAlmanac}
</article>

EOD;

// Finally, leave it all to Linux.
include(LINUX_THEME_PATH);
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: select all Sundays red in a calendar class?

Post by requinix »

So lacking the outputted HTML, I looked at the code more carefully. You added the class=sunday but where it is will highlight the current day - not Sunday.

The logic that tells you the day is Sunday happened earlier with the $i % 7 == 0. Put a variable in there to track the class name you need to add to the <td> cell, and be sure to clear it if the day isn't Sunday. Then use that class name in the three places you output the <td>.
Post Reply