Jump to content

[SOLVED] Help with event calendar or good tutorial to follow


Styles2304

Recommended Posts

Ok, here is the code that controls how the calendar is created:

 

<?php
include "conn.inc.php";

$day = $_GET["day"];
$month = $_GET["month"];
$year = $_GET["year"];

if ($day == "")
  $day = date("j");
  
if ($month == "")
  $month = date("n");
if ($year == "")
  $year = date("Y");
  
  
$currentTimeStamp = strtotime("$year-$month-$day");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
?>

<LINK REL=StyleSheet HREF="calendar.css" TYPE="text/css">

<table width='168' height="121" border='0' cellspacing='2' cellpadding='0'>
  <tr>
    <td colspan="7" class="title">
      <font class="title">
      <center>
      <?php echo date("F");?>
      </center>
      </font>
    </td>
  </tr>

  <tr class="daylabels">
    <td class='blank' width='24'>Sn</td>
    <td class='blank' width='24'>Mn</td>
    <td class='blank' width='24'>Tu</td>
    <td class='blank' width='24'>Wd</td>
    <td class='blank' width='24'>Th</td>
    <td class='blank' width='24'>Fr</td>
    <td class='blank' width='24'>St</td>
  </tr>

  <tr class="date">
<?php
for ($i = 1; $i < $numDays + 1; $i++, $counter++) {
  $timeStamp = strtotime("$year-$month-$i");

  if ($i == 1) {
//Figures out when the first day of the month is
    $firstDay = date("w",$timeStamp);

    for($j = 0; $j < $firstDay; $j ++, $counter++)
      echo "<td width='24' height='16' class='blank'> </td>";
  }
  
  if($counter % 7 == 0) {
    echo "</tr><tr class='date'>";
  }

  if (date("w", $timeStamp) == 0 || date("w", $timeStamp) == 6) {
    echo "<td width='24' height='16' class='weekend'>$i</td>";
    
  } else {
  
  if ($i == date("d") && $month == date("m") && $year == date("Y")){
    echo "<td width='24' height='16' class='today'>$i</td>";

  } else {

    echo "<td width='24' height='16' class='normal'>$i</td>"; 

}
}
}
?>

</table>

 

I'd like to do a typical event calendar where if there is an event (which is stored in a database) it will highlight whatever day the event takes place on.

 

I'm sorry if this is kind of a broad question but can someone either show me or point me towards a tutorial that can tell me how to accomplish this?

 

I know it will require a foreach loop (at least I think so) and it will check, in this case, the day of the event against $i and if they equal it will set that particular table column's class to event I'm just not sure how to actually implement that with the code I currently have.

Link to comment
Share on other sites

I guess I can narrow my question . . . how do I make it so that if a check of the database returns true, it will change the class of the table?

 

Something like if event == y where date == whatever . . . it will change the class when $i == date. Does that make sense?

 

Well, actually, I could probably even do that myself but I need to do a check of "date" in this case assuming that it holds many dates . . . I don't even know it's so damn aggravating when you don't even know enough to be able to ask the right damn question.

Link to comment
Share on other sites

I really don't know what else to do other than bump this up.

 

Just to re-iterate . . . I'm wanting to pull the dates of events out of the database (I know how to do that) and then compare them to $i . . . if any of the dates == $i then I want to do:

 

<td width='24' height='16' class='event'><a href='##?IndexNo=' . $IndexNo . '">$i</a></td>

 

for that particular date and then continue down the list. I know this requires a foreach or a for but I'm not clear on the mechanics yet.

 

I'm pretty desperate here as I've been working on this control panel for my Church for awhile now only to realize that I'm kind of over my head. I've gotten everything else but this figured out (thanks to you guys).

Link to comment
Share on other sites

<?php
$query = mysql_query("Your query here");

$i = "What you want to match";
while($rs = mysql_fetch_assoc($query) {
    //Insert your code here for what you want to happen, for each result in the database
    if($rs["the name of the field you want to match"] == $i) {
        echo("<td width='24' height='16' class='event'><a href='##?IndexNo=$IndexNo'>$i</a></td>");
    }else {
        //Here you echo what you want it to look like if it does NOT match what you want
    }
}
?>

Link to comment
Share on other sites

<?php
$query = "SELECT * FROM dates GROUP BY date"; // This will return each date once only
$result = mysql_query($query);
$num = mysql_num_rows($result);
if ($num > 0) { // if there is at least 1 record
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        if ($row['date'] == $yourdate) {
          $query1 = "SELECT * FROM dates WHERE date='{$row['date']}'"; // Select each record for each each single date
          $result1  mysql_query($query1);
          $num1 = mysql_num_rows($result1);
          if ($num1 > 0) { // There is at least 1 record on that day
                 while ($row1 = mysql_fetch_array($result1, MYSQL_ASSOC)) {
                         echo '<tr><td>'.$row['event_name'].'</td><td>'.$row['date'].'</td></tr>'; // echo each record
                 }
          }
        } // else the event is not on your specified day
    }
} else { // could not find any records
    echo 'There are no records.';
}
?>

 

Obviously i've made up the table and column names but if you rewrite what i've just done and implementit into your script it should get records for each day and if there is more than 1 record on that day, it will display them all. I think that is what your after anyway.

 

The if statement "if ($row['date'] == $yourdate) {" was added after submitting my form and readin what Xaero had wrote.

 

I'm not fully sure which you are trying to do but hopefully you can gain something from this post.

Link to comment
Share on other sites

I'm just not sure how to incorporate all the loops . . . it's tricky stuff. Is my base code for the calendar itself wrong or is this just a harder than usual code to figure out?

 

I'm gonna download yours and see if I can't figure out what I'm doing wrong. Thank you.

Link to comment
Share on other sites

I think I understand your code enough to make it work except for one part.

 

Andy, can you explain this a little a bit? :

 

    { 
        $found = $myrow['ev_dat'];
        $pieces = explode("-", $found);
        $dd = intval($pieces[2]);
        $ev_dat[$dd] = $myrow['id'];
    }

Link to comment
Share on other sites

$found is something from the database row (the event date in yyyy-mm-dd format, as I recall)

 

explode() breaks that up into pieces at the - delimiter into an array (three elements, numbered 0,1, and 2)

 

$pieces[2] is the third element of the date, i.e. the day number

 

$ev_dat[] is then an array element linked back to the database record for a day's information

 

Does that help?

Link to comment
Share on other sites

Ok . . . yeah that makes sense I didn't think to link the data format to explode so I was a little confused.

 

Maybe i should just back off of this one . . . but I still can't understand how it does the check for each day without using foreach or something

 

I appreciate all your help but I guess I'll just try to find an alternative like doing it by hand in html >_<

Link to comment
Share on other sites

Ok well, I looked through your stuff some more and while I didn't understand all of it . . . I did this:

 

$query = "SELECT EventDate FROM CalendarEvents WHERE (MONTH(EventDate) = " . $month . ");";

$result = mysql_query($query, $link)
  or die(mysql_error());

$EventDate = array();

while ($row = mysql_fetch_array($result)) {
  $found = $row['EventDate'];
  $pieces = explode("-", $found);
  $ed = ($pieces[2]);
  
  $EventDate[$ed] = "1";
}

 

coupled with :

 

if ($EventDate[$i]) {
    echo "<td width='24' height='16' class='event'><a href='showevent.php?day=$i' class='calendar'>$i</a></td>";

 

That pretty much covered it. I had additional code in there in case it was an event AND today but it's just slightly modified.

 

Thank you guys for making me think.

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.