Jump to content

[SOLVED] Color today and event dates


SkyRanger

Recommended Posts

I am trying to figure out how to color in todays date and to color in event days.  I have treid a number of things but still having problems.  This is what I have with test codes removed.  Looked like a mess.  Was wondering if anybody had any ideas on how to do this.

 

$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "<table border=\"1\" cellpadding=\"5\"><tr>\n";
foreach ($days as $day) {
        echo "<td style=\"background-color: #CCCCCC; text-align: center; width: 14%\">
              <strong>$day</strong></td>\n";
}

for ($count=0; $count < (6*7); $count++) {
        $dayArray = getdate($start);
        if (($count % 7) == 0) {
                if ($dayArray["mon"] != $month) {
                        break;
                } else {
                        echo "</tr><tr>\n";
                }
        }
        if ($count < $firstDayArray["wday"] || $dayArray["mon"] != $month) {
                echo "<td> </td>\n";
        } else {
                $chkEvent_sql = "SELECT event_title FROM calendar_events WHERE month(event_start) = '".$month."' AND dayofmonth(event_start) = '".$dayArray["mday"]."' AND year(event_start) = '".$year."' ORDER BY event_start";
                $chkEvent_res = mysql_query($chkEvent_sql, $mysql) or die(mysql_error($mysql));

                if (mysql_num_rows($chkEvent_res) > 0) {
                        $event_title = "<br/>";
                        while ($ev = mysql_fetch_array($chkEvent_res)) {
                                $event_title .= stripslashes($ev["event_title"])."<br/>";
                        }
                        mysql_free_result($chkEvent_res);
                } else {
                        $event_title = "";
                }

                echo "<td valign=\"top\"><center><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a></center>".$event_title."</td>\n";

                unset($event_title);

                $start += ADAY;
        }
}

Link to comment
Share on other sites


$current = date("l");

foreach ($days as $day) {
  if ($day == $current) {
        echo "<td style=\"background-color: #CCCCCC; text-align: center; width: 14%\">
              <strong>$day</strong></td>\n";
  }
}

 

Is that what you mean?

Link to comment
Share on other sites


$current = date("l");

foreach ($days as $day) {
  if ($day == $current) {
        echo "<td style=\"background-color: #CCCCCC; text-align: center; width: 14%\">
              <strong>$day</strong></td>\n";
  }
}

 

Is that what you mean?

 

I tried replacing

 

foreach ($days as $day) {
        echo "<td style=\"background-color: #CCCCCC; text-align: center; width: 14%\">
              <strong>$day</strong></td>\n";
}

 

With what you gave me, but still didn't work.

Link to comment
Share on other sites

Ok, I have tried this:

 

        $event_title = "";
                }

                $current = date("j");
			if($dayArray["mday"] == $current) {
				echo "<td background-color=red height=\"74\" valign=\"top\"><center><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a></center>".$event_title."</td>\n";
										  } else {
			echo "<td height=\"74\" valign=\"top\"><center><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a></center>".$event_title."</td>\n";
										  }

 

Still not working, anybody have any ideas?

Link to comment
Share on other sites

ok, I noticed allot of people looking at this post and figured I may not have explained myself clearly.  I got the first part of my problem fixed, highlighting todays date.  What I am trying to do now is have it now highlight any days that have events on it, so when the script checks that database

if (mysql_num_rows($chkEvent_res) > 0)

and there is an event entry it will color in the TD but if there is no event it will leave it at the default color.

 

Here is what it looks like so far.  I tend on removing the text for event once I can get the color to show. 

http://calendar.ess-hosting.com/calendar.php

Link to comment
Share on other sites

Ok, got the today's date to highlight, but for some reason I cannot get the dates where events have been posted to highlight.  Is there a way to do this.  This is what I have so far:

 

$days = Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
echo "<table border=\"1\" cellpadding=\"5\">
<tr>
    <td colspan=\"7\"><div align=middle><strong><font size=14>".$firstDayArray['month']." " . $firstDayArray['year'] ."</font></strong></div></td>
  </tr>
<tr>\n";
foreach ($days as $day) {
        echo "<td style=\"background-color: #CCCCCC; text-align: center; width: 74\">
              <strong>$day</strong></td>\n";
}

for ($count=0; $count < (6*7); $count++) {
        $dayArray = getdate($start);
        if (($count % 7) == 0) {
                if ($dayArray["mon"] != $month) {
                        break;
                } else {
                        echo "</tr><tr>\n";
                }
        }
        if ($count < $firstDayArray["wday"] || $dayArray["mon"] != $month) {
                echo "<td> </td>\n";
        } else {
                $chkEvent_sql = "SELECT event_title FROM calendar_events WHERE month(event_start) = '".$month."' AND dayofmonth(event_start) = '".$dayArray["mday"]."' AND year(event_start) = '".$year."' ORDER BY event_start";
                $chkEvent_res = mysql_query($chkEvent_sql, $mysql) or die(mysql_error($mysql));

                if (mysql_num_rows($chkEvent_res) > 0) {
                        $event_title = "<br/>";
                        while ($ev = mysql_fetch_array($chkEvent_res)) {
                                $event_title .= stripslashes($ev["event_title"])."<br/>";
                        }
                        mysql_free_result($chkEvent_res);
                } else {
                        $event_title = "";
                }

                $currentm = date("n");
			$currentd = date("j");
			$currenty = date("Y");
			if($month == $currentm && $dayArray["mday"] == $currentd && $firstDayArray['year'] == $currenty) {
				echo "<td bgcolor=yellow height=\"74\" valign=\"top\"><center><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a><br/>".$event_title."</center></td>\n";
										  } else {
			echo "<td height=\"74\" valign=\"top\"><center><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a><br/>".$event_title."</center></td>\n";
										  }

                unset($event_title);

                $start += ADAY;
        }
}
echo "</tr></table>";
mysql_close($mysql);
?>

 

I have tried a number of different things but either a getting errors or b nothing is changing.

Link to comment
Share on other sites

Sorry for so many posts but I guess as I ask the question I either end up fixing it myself or I can't seem to solve the problem.  The last problem I am having is trying to color in the calendar box for posted events.  I have tried a number of things over the past few hours and still come up stuck.

 

I think it has something to do with this code:

 

if (mysql_num_rows($chkEvent_res) > 0)

 

But not completely sure.  It has taken me 2 weeks to get this far.  Not much farther and my event calendar is almost done.

Link to comment
Share on other sites

You are correct when you state that you haven't been clear.  Additionally, when someone replied to you with a suggestion, your answer was:  doesn't work, nobody wants to waste their time.  Your question at this point seems to have nothing to do with PHP, but rather is an html/style question. Are you able to create a pure html mockup which employs whatever coloring you are trying to accomplish?  If so, then you should be able to automate the coloring.  I don't see any reason why you would be trying to complicate this with javascript, but then, your original post didn't do a very good job of explaining what it is that you wanted in the first place.

Link to comment
Share on other sites

Yeah Giz, Unfortionalty I realized that too late.  My bad.  Ref my latest problem.  Yeah it is possible and html problem I think.  The 2 parts that I had problem was:

 

1) Trying to get the today block to color in.  Got that part fixed as you can see at http://calendar.ess-hosting.com

 

The second part that I am having problem is which I think is PHP is that I need my script to just color in the days that have events in them when php pulls the info out of mysql.

 

Again I apologize for the confusion.  After reading my own post, I confused myself.

Link to comment
Share on other sites

It's hard for me to find out where in your code to place it, but the easiest thing to do would be to add a class to the <td> if there is an event on that day.

 

Looks like this line:

echo "<td height=\"74\" valign=\"top\"><center><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a><br/>".$event_title."</center></td>\n";

 

You could add a class to it:

echo "<td height=\"74\" valign=\"top\" class="$eventDay"><center><a href=\"javascript:eventWindow('event.php?m=".$month."&d=".$dayArray["mday"]."&y=$year');\">".$dayArray["mday"]."</a><br/>".$event_title."</center></td>\n";

 

And change your other code:

                if (mysql_num_rows($chkEvent_res) > 0) {
                                $event_title = "Event!";
                                $eventDay = "eventDay";
                        }
                        mysql_free_result($chkEvent_res);
                } else {
                        $event_title = "";
                        $eventDay = "";
                }

to simply be " if eventcount > 0 , make text == Event(s)"(or something similar), as well as make the $eventDay CSS null or active(meaning exists).

 

I suggest this because some days will have more events than others and it will skew your table layout.  Then you could also make the day # a mouseover event to display the list of events if you wanted, rather than clicking.

 

Make sense?

Link to comment
Share on other sites

No worries.  In essence here is your problem:

 

As you've seen, you can color the background of the TD.  Which you do in this block of code:

 

if ($month == $currentm && $dayArray["mday"] == $currentd && $firstDayArray['year'] == $currenty) {
    echo "".$dayArray["mday"]."
".$event_title."\n";
} else {
    echo "".$dayArray["mday"]."
".$event_title."\n";
}

 

One problem is that you are replicating two lines of code to do the exact same thing, when the only thing you really are interested is setting the "bgcolor=" attribute in the td.  The second problem is that you never determine whether or not there is an event.  Based on the way you have written your code, you could do this using empty() on the $event_title variable.  If there's an event empty() will be false.  What I'd suggest is that you initially determine what your bgcolor needs to be (default, today, or event).  Of course you have the issue that if there's an event today, you haven't indicated what you want to do.  I'm just going to give you code that sticks with it being yellow.

 

Just prior to your if else statement above, insert code something along these lines:

 

 

$bgc = '';  // default no bgcolor
if ($dayArray['mday'] == $currentd) {
  $bgc = ' bgcolor="yellow"';  
} elseif (!empty($event_title)) {
  // Set event color
  $bgc = ' bgcolor="green"';
}

if ($month == $currentm && $firstDayArray['year'] == $currenty) {
    echo '".$dayArray["mday"]."
".$event_title."\n";
}

 

Notice that you no longer need the if - else of the original block, so this should replace what you had previously.

Link to comment
Share on other sites

OMG, you guys are awesome, sorry for all the problems.  I have been staring at this code for so long I never even seen that problem.  Thank you very much, that fixed the problem.

 

Now that problem is fixed i have this other problem......lol, just kidding, again thanks guys for all of you help.

Link to comment
Share on other sites

Crap, thought my problems were fixed,  I guess I just ran into another problem  The problem being is that I lost all of my previous and next months, with that piece of code you gave me giz.

 

Cancel that, Guess I deleted a bit too much code, works perfectly.  Thanks Giz

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.