Jump to content

[SOLVED] Calendar tutorial help


adam291086

Recommended Posts

Hello, I had a post about this before, but i have since found a tutorial that answers my questions. The problem now is as follows.

 

The tutorial  doesn't show you how to echo out the calendar. I have been trying for hours to get this to work and have resulted in nothing. Can someone push me in the right direction. The code below is for my calendar.

 

<?php
function daysInMonths($time){
    if(!$time) $time=time();
    $month=strftime("%m",$time);
    $year=strftime("%Y",$time);
    return cal_days_in_month(CAL_GREGORIAN,$month,$year);
}
function renderCalendar($time){
    if(!$time) $time=time();
    //Get Month And Year
    $month=strftime("%m",$time);
    $year=strftime("%Y",$time);
    //Render Current Calendar
    $dayCount=cal_days_in_month(CAL_GREGORIAN,$month,$year);
    $calArray=array();
    for($day=0;$day<$dayCount;$day++){
        $tmNow=mktime(0,0,0,$month,$day+1,$year);
        $yearWeek=(int)strftime("%W",$tmNow);
        if($yearWeek>52) $yearWeek-=52;
        if(!is_array($calArray[$yearWeek])){
            $calArray[$yearWeek]=array();
            for($tday=0;$tday<7;$tday++){
                $calArray[$yearWeek][$tday+1]=false;
            }
        }
        $dayInfo=array(
            "time" => $tmNow,
            "mday" => ($day+1),
            "wday" => (int)strftime("%w",$tmNow),
            "yday" => (int)strftime("%j",$tmNow)
        );
        if(!$dayInfo['wday']) $dayInfo['wday']=7;
        $calArray[$yearWeek][$dayInfo['wday']]=$dayInfo; 
    }
    $calInfo=array(-1=>array(),0=>array(),1=>array());
    $calInfo[0]=mktime(0,0,0,$month,1,$year);
   
//Get Next Month
    $nextmonth=$month+1; $nextyear=$year;
    if($nextmonth==13){ $nextyear++; $nextmonth-=12; }
    $calInfo[1]=mktime(0,0,0,$nextmonth,1,$nextyear);
   
//Get Previous Month
    $prevmonth=$month-1; $prevyear=$year;
    if($nextmonth==0){ $prevyear--; $prevmonth+=12; }
    $calInfo[-1]=mktime(0,0,0,$prevmonth,1,$prevyear);
   
//Return Calendar
    $cal=array('Calendar'=>$calArray,'Info'=>$calInfo);
    return $cal;
}


    
$Calendar = renderCalendar(); //Generate for current month
$Months = $Calendar['Info'];
$Calendar = $Calendar['Calendar'];


//These are all Unix Timestamps
$PreviousMonth = $Months[-1];
$CurrentMonth = $Months[0];
$NextMonth = $Months[1];


//These are the year weeks from 1-52
//that fit in the month of the calendar.
$YearWeeks = array_keys($Calendar);

//We parse the $Calendar array by $YearWeeks

foreach($YearWeeks as $YearWeek){
    $WeekDays = $Calendar[$YearWeek];
    // And we further parse the $WeekDays for output
    foreach($WeekDays as $WeekDay){
        $DayTimeStamp = $WeekDay['time'];
        $MonthDay = $WeekDay['mday'];
        $YearDay = $WeekDay['yday'];
        //Keep in mind on next line I loose the $WeekDay initial value
        //but I assigned all it's children to dedicated variable so who cares?
        $WeekDay = $WeekDay['wday'];
        //Here be dragons! Keep reading ...
    }
}



?>

Link to comment
Share on other sites

Are you sure you got everything the tutorial provided? I don't see any point to a tutorial that has no output.

 

The code comments read:

 

<?php

// And we further parse the $WeekDays for output

then a few lines later,

//Here be dragons! Keep reading ...

 

then the script ends. What are we supposed to keep reading...?

 

Anyways, what are your needs for a calendar? I just developed one for a form I'm putting together for another user here. It works, it has output. Let me know what you expect the calendar to do for you...

 

PhREEEk

Link to comment
Share on other sites

Well the keep reading bit is where we add in the MYSQL for the database interaction. This is where i got the tutorial from http://www.tellinya.com/read/2007/10/19/201.html. It just say i should be able to work it out, which is not helpful.

 

What i want is a simple calendar that interacts with a database. I.e the days where an event is happening to be shaded grey and if the user clicks on that day the page displays all the event info

 

Thanks for any help.

Link to comment
Share on other sites

I did my best and took the tutorial as low as possible (for me). If you do not understand how to use it after reading that page ... no offense ... but you really need to get the PHP manual and dig harder!

I made it making sure it will fit anyone's needs for a calendar. I took the coder 90% but I need 10% from him to be worthy of using my code.

 

Others had the same problem with the original php calendar script but this second detailed mysql integration tutorial solved their problem.

 

OP, next time your copy / paste source code do mention where you got it from!

Thanks.

Link to comment
Share on other sites

That is pretty harsh. I am new to php and love it. I like setting challenges for myslef. I follow tutorials and learn how they work. I get the basic idea of how your caledar works. Like i said

 

Can someone push me in the right direction.
I dont want people to write it for me. Just give me an idea how. Therefore i can become worthy of your Precious code.
Link to comment
Share on other sites

LOL... seriously...

 

Well Adam, I am very busy, but I threw together a small example of code to get this Elite Programmer's calendar to output something. It prints the current month, and that's all the time I could spend on something I don't intend to use. Read through the code I added, and you'll pick up on how his arrays work. There are much easier ways to traverse his array structure, but I coded to allow a beginner coder see what's happening. If you are not really up to speed on arrays, then you won't go far with his script, however, forcing yourself to understand this script will increase your understanding of arrays, which I do believe is vital to grasping the power of any programming language. If you just need a frickin' calendar, and need it now, keep googling for it, this isn't it. To use this calendar, you do need a certain level of programming. If you're not in a real big hurry, I'm sure a lot of the folks around here will help you integrate a 'event' feature into this, but in all honesty, there are tons of these PHP Calendars out there... this is as far away from brain surgery as you can get.

 

@5ubliminal

We hang out in this Forum to help people who are just learning. We try to be teachers, not look down our noses at them as if they are not worthy. Ok, so it's obvious you are not a teacher-type. I accept that, but please keep your condescending comments to your blog. I find your attitude offensive on this particular Support Forum, which exists to teach and help newcomers to PHP. If you do not wish your code to be experimented with by newcomers or professionals alike, do not post your code publicly.

 

<?php
function daysInMonths($time){
    if(!$time) $time=time();
    $month=strftime("%m",$time);
    $year=strftime("%Y",$time);
    return cal_days_in_month(CAL_GREGORIAN,$month,$year);
}
function renderCalendar($time){
    if(!$time) $time=time();
    //Get Month And Year
    $month=strftime("%m",$time);
    $year=strftime("%Y",$time);
    //Render Current Calendar
    $dayCount=cal_days_in_month(CAL_GREGORIAN,$month,$year);
    $calArray=array();
    for($day=0;$day<$dayCount;$day++){
        $tmNow=mktime(0,0,0,$month,$day+1,$year);
        $yearWeek=(int)strftime("%W",$tmNow);
        if($yearWeek>52) $yearWeek-=52;
        if(!is_array($calArray[$yearWeek])){
            $calArray[$yearWeek]=array();
            for($tday=0;$tday<7;$tday++){
                $calArray[$yearWeek][$tday+1]=false;
            }
        }
        $dayInfo=array(
            "time" => $tmNow,
            "mday" => ($day+1),
            "wday" => (int)strftime("%w",$tmNow),
            "yday" => (int)strftime("%j",$tmNow)
        );
        if(!$dayInfo['wday']) $dayInfo['wday']=7;
        $calArray[$yearWeek][$dayInfo['wday']]=$dayInfo; 
    }
    $calInfo=array(-1=>array(),0=>array(),1=>array());
    $calInfo[0]=mktime(0,0,0,$month,1,$year);
   
//Get Next Month
    $nextmonth=$month+1; $nextyear=$year;
    if($nextmonth==13){ $nextyear++; $nextmonth-=12; }
    $calInfo[1]=mktime(0,0,0,$nextmonth,1,$nextyear);
   
//Get Previous Month
    $prevmonth=$month-1; $prevyear=$year;
    if($nextmonth==0){ $prevyear--; $prevmonth+=12; }
    $calInfo[-1]=mktime(0,0,0,$prevmonth,1,$prevyear);
   
//Return Calendar
    $cal=array('Calendar'=>$calArray,'Info'=>$calInfo);
    return $cal;
}


    
$Calendar = renderCalendar(time()); //Generate for current month
$Months = $Calendar['Info'];
$Calendar = $Calendar['Calendar'];

echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>PHP Calendar</title>
<body>
';

echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"1\">
  <tr>
    <td colspan=\"7\">" . date("F Y", $Months[0]) . "</td>
  </tr>
";
foreach($Calendar as $key => $val) {
    echo "  <tr>
";
    for ($x=1 ; $x < 8 ; $x++) {
        if ( empty($Calendar[$key][$x]['mday']) ) {
            $Calendar[$key][$x]['mday'] = ' ';
        }
        echo "    <td>" . $Calendar[$key][$x]['mday'] . "</td>
";
    }
    echo "  </tr>
";
}
echo "</table>
</body>
</html>";

//These are all Unix Timestamps
$PreviousMonth = $Months[-1];
$CurrentMonth = $Months[0];
$NextMonth = $Months[1];


//These are the year weeks from 1-52
//that fit in the month of the calendar.
$YearWeeks = array_keys($Calendar);

//We parse the $Calendar array by $YearWeeks

foreach($YearWeeks as $YearWeek){
    $WeekDays = $Calendar[$YearWeek];
    // And we further parse the $WeekDays for output
    foreach($WeekDays as $WeekDay){
        $DayTimeStamp = $WeekDay['time'];
        $MonthDay = $WeekDay['mday'];
        $YearDay = $WeekDay['yday'];
        //Keep in mind on next line I loose the $WeekDay initial value
        //but I assigned all it's children to dedicated variable so who cares?
        $WeekDay = $WeekDay['wday'];
        //Here be dragons! Keep reading ...
    }
}


?>

 

PhREEEk

Link to comment
Share on other sites

That is pretty harsh. I am new to php and love it. I like setting challenges for myslef. I follow tutorials and learn how they work.

 

I follow tutorials and learn ... is not a statement that makes me happy.

Just wondering? Have you ever read a PHP book? Something over 250 pages?

You really think copy / pasting code others give you and working your a$$ off to understand it is productive in any way?

Maybe you have a lot of free time and like to bang your head against walls but do consider the easy way out!

Learn, understand and reverse engineering of code will be a breeze.

 

Buy a book first! Learn it by heart. Reach a medium level and then start learning from tutorials online.

As many know, sometimes you must pay for knowhow. That's why books are not free!

 

I know I'm a jerk but I also know you know I'm right!

Take this kick in the a$$ as a step forward and, if you wanna go PRO, read books first! And you'll save a lot of time too.

 

Hit the bookstore and buy a decent PHP book ... you'll thank me later!  ;)

Link to comment
Share on other sites

I don't think anyone will thank you at all.

 

Most of us know to buy books.  I read your two links you posted here, and I must say your remarks even in the tutorial would make me never read anything you write again.

 

Also, you should run a spell checker on your tutorial site.

 

That is pretty harsh. I am new to php and love it. I like setting challenges for myslef. I follow tutorials and learn how they work.

 

I follow tutorials and learn ... is not a statement that makes me happy.

Just wondering? Have you ever read a PHP book? Something over 250 pages?

You really think copy / pasting code others give you and working your a$$ off to understand it is productive in any way?

Maybe you have a lot of free time and like to bang your head against walls but do consider the easy way out!

Learn, understand and reverse engineering of code will be a breeze.

 

Buy a book first! Learn it by heart. Reach a medium level and then start learning from tutorials online.

As many know, sometimes you must pay for knowhow. That's why books are not free!

 

I know I'm a jerk but I also know you know I'm right!

Take this kick in the a$$ as a step forward and, if you wanna go PRO, read books first! And you'll save a lot of time too.

 

Hit the bookstore and buy a decent PHP book ... you'll thank me later!  ;)

Link to comment
Share on other sites

Sure, but only if you let me write it on your b00b!

 

Where I come from ... Legends outnumber beginners!

 

PS: Phreek ... I would really reconsider adding HTML code within PHP.

It won't look great in WYSIWYG editors one may use to build the UI and have an idea of the outcome.

Further updates will be a PITA also if HTML is not in separate blocks from PHP.

 

Open <? tag, close ?> tag ... sing with me!

Link to comment
Share on other sites

ok, i have another question. I have looked at the code given by PHP_PhREEEk. As i am wanting to connect it to a database and shade event days in grey. Would i do something like the following?

 

$result = mysql_query( "SELECT `date` FROM `table` ") or die('Query failed. ' . mysql_error());
while($row = mysql_fetch_array($result))
{
$adam = $row['date'] ;
}

 

Then you i do an if statement along the lines of

 

if ( $adam ==$Calendar['time']{
echo "    <td bgcolor="#666666">" . $Calendar[$key][$x]['mday'] . "</td>
}
else{
echo "    <td>" . $Calendar[$key][$x]['mday'] . "</td>
}

 

Is this the right idea?

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.