FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Problem with Calander.





Diablosblizz
Hey, I am appearing to have problems with a Calender script. To get a feel of what I mean, view this image:



That is the error. If you see the 2 underlined it really shouldn't be there, I mean there should be a two but it shouldn't be underlined. Nor should there be negatives and additional numbers going into unknown days Wink. It should look similar to this:



The code I created SHOULD detect the day, being the second, and the month, being 6 (June) or 7 (July). Unfortuantely, my code apparently doesn't work, instead ignoring what month it is and just saying:

"Oh look, there is a second in this month, heck likes assign a event on that day! WOOT!"

Instead of:

"Oh look, there is a second in this month. What month is it? The seventh? Well, the database says that the sixth month should have an event, but not the seventh, let's just show the number without an event."

Make sense? Razz So, it's incorrectly showing the calender and it's incorrectly checking the month. Here is my code:

Code:
$date = $i - $startday +1;
$month = date("n");
$mysql = mysql_query("SELECT * FROM `calender` WHERE `date` = '$date' AND `month` = '$month'");
$num = mysql_num_rows($mysql);
               
if($num == 1) {
echo "<td align='center' valign='middle' height='20px'><a href='test.php?page=add&" . ($i - $startday +1) . "'>". ($i - $startday + 1) . "</a></td>\n";
} else {
echo "<td align='center' valign='middle' height='20px'>" . ($i - $startday +1) . "</td>\n";
}


Can anybody help, cause I am virtually stumped! Sad Thanks!
rvec
first I'd like to know how you get $i and $startday

second I'd like you to echo or var_dump all the variables anywhere in the script just after you changed them.

third $num == 1 means that it won't happen if you have 2 or more events at that day, so better make that > 0.
Diablosblizz
1. Here is the full code of all the varibles:

Code:
                <?php
  $monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
   if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
   if (!isset($_REQUEST["year"]))  $_REQUEST["year"]  = date("Y");
   
  $cMonth = $_REQUEST["month"];
  $cYear  = $_REQUEST["year"];
  $tYear = date("Y");
               
  $prev_year = $cYear;
  $next_year = $cYear;

    $prev_month = $cMonth-1;
    $next_month = $cMonth+1;

    if ($prev_month == 0 ) {
    $prev_month = 12;
    $prev_year = $cYear - 1;
  }
    if ($next_month == 13 ) {
    $next_month = 1;
    $next_year = $cYear + 1;
  }

// some HTML here for the tables

                   $timestamp = mktime(0,0,0,$cMonth,1,$cYear);
                   $maxday    = date("t",$timestamp);
                   $thismonth = getdate ($timestamp);
                   $startday  = $thismonth['wday'];

                  for ($i=0; $i<($maxday+$startday); $i++) {
                    if(($i % 7) == 0 ) echo "<tr>\n";
                    if($i < $startday) echo "<td></td>\n";
                    else
                    $date = $i - $startday +1;
                    $month = date("n");
               $mysql = mysql_query("SELECT * FROM `calender` WHERE `date` = '$date' AND `month` = '$month'");
               $num = mysql_num_rows($mysql);
               
               if($num > 0) {
                  echo "<td align='center' valign='middle' height='20px'><a href='test.php?page=add&" . ($i - $startday +1) . "'>". ($i - $startday + 1) . "</a></td>\n";
               } else {
                  echo "<td align='center' valign='middle' height='20px'>" . ($i - $startday +1) . "</td>\n";
               }
                    if(($i % 7) == 6 ) echo "</tr>\n";
                  } 
                 ?>


2. I don't really know why you want me to echo them, cause all that is changing are the days.

3. Done. Smile
rvec
I tried some debugging, but I think it would be better to make something from scratch. There are some errors you'll get when running the code (the else on line 35 is a nice one).

Also I don't understand why you have 4 variables with the same year in it :S.

Maybe you could tell me what you want the script exactly to do (input and output) and I'll write something.
Diablosblizz
Okay, sorry for such problems. What my main goal is to be able to have a calender that is able to hold events inside and users will be able to click on the day and be able to view what is happening that day. I am able to code the page where it shows what is going on that day. I basically need a calender that will show the days, correctly, and will show a link to a page if there is a event on that day. The events will be stored in a MySQL database. I am able to code the rest, such as adding an event, and emailing the user a week before the event and a day before the event.

Also, please don't worry about the cron jobs as this will not be hosted on FriHost, it will be hosted on my PS (personal server).

If you need any more information please ask! Very Happy Thanks!
rvec
then I'd suggest using the DATE data type in mysql instead of what you are using now. Then some events can also be only once, and not every year like is the case here:
Code:
$mysql = mysql_query("SELECT * FROM `calender` WHERE `date` = '$date' AND `month` = '$month'");


You could then just do
Code:
$mysql = mysql_query("SELECT * FROM `calender` WHERE `date` = '$date'");


That code to get $date even if it's send as a var could be under 5 lines .

I'll take a better look at this when I get back (work).

edit:
Code:
<?php
//first check for input
if (isset($_REQUEST["month"])) {
    $month = (int) $_REQUEST["month"];
   if (strlen($month) === 1) {$month = '0'. $month;}
   } else {
   $month = date('m');
}

if (isset($_REQUEST["year"])) {
    $year = (int) $_REQUEST["year"];
} else {
   $year = date('Y');
}
//then we want to use our values and make a date off it (notice I left out the day)
$date = $year .'-'. $month .'-';

//now we need a connection
$link = mysql_connect("localhost", "root", "");
mysql_select_db("database", $link);


// check the month in the database
$mysql = mysql_query("SELECT * FROM `calender` WHERE event_date LIKE '$date%'");

//put all the events in an array
while ($event = mysql_fetch_array($mysql, MYSQL_ASSOC)) {
   $events[] = $event;
}


//get the number of days in this month
$days = date('t', mktime(0, 0, 0, $month, 0, 2000));

//make a function to check if a date is an event

function is_event($needle, $haystack) {
   foreach ($haystack as $event) {
      if (in_array($needle, $event)) {
         return true;
      }
   }
   
   return false;
}


//make the calender
for ($i = 1; $i <= $days; $i++) {
   
   if (strlen($month) === 1) {
      $day = $date .'0'. $i;
   } else {
      $day = $date . $i;
   }
   
   if(is_event($day, $events)) {
      echo "<td align='center' valign='middle' height='20px'><a href='test.php?page=add&day=" . ($i) . "'>". ($i) . "</a></td>\n";
   } else {
      echo "<td align='center' valign='middle' height='20px'>" . ($i) . "</td>\n";
   }
}   

?>


That will return something like this:
Code:
<td align='center' valign='middle' height='20px'>14</td>
<td align='center' valign='middle' height='20px'><a href='test.php?page=add&day=15'>15</a></td>
Diablosblizz
This doesn't work, if it's supposed to. There is a problem with the foreach(). I don't see a $needle or $haystack variable so I am guessing this is just an example. Also, I don't understand why the mysql_query is using like instead of it exactly equalling it?

How would I come across fixing this? :S Thanks!
rvec
Diablosblizz wrote:
This doesn't work, if it's supposed to. There is a problem with the foreach(). I don't see a $needle or $haystack variable so I am guessing this is just an example. Also, I don't understand why the mysql_query is using like instead of it exactly equalling it?

How would I come across fixing this? :S Thanks!

it does work on my pc. I could check if it also works on frihost and my server, but I don't see any reason why it wouldn't.

The needle and haystack are the names of the variables in the function
Code:
function is_event($needle, $haystack) {

So if you call the function you give them values. Like here:
Code:
is_event($day, $events)


The database called 'database' should contain at least one table called calender and one collumn called 'event_date' which is the date of the event in the DATE datatype, making it easier to make a list of events based on date and stuff.

edit: seems to work fine on my server also:
http://www.yarontal.nl/calender.php
Diablosblizz
I've tried it on my local server and it doesn't work. On line 39 I get a:

Code:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\techcrew\cal.php on line 39
.

This is after every number.
rvec
ah yes to solve that (you get theat when there are no results from the query) try to add this on line 30:
Code:
if (!isset($events)) {
   $events = array();
}
Diablosblizz
Wow, this works finally! Thanks! One question though, I added a date for 2008 in January (just for testing out how to check different days, and it doesn't seem to appear. Though, it may be the URL I am using:

http://localhost/techcrew/cal.php?month=1

I have also tried the month to be 01. Am I doing something wrong?
rvec
try to make a table with this command (delete the existing table and execute this in phpmyadmin):
Code:
CREATE TABLE `calender` (
  `id` int(5) NOT NULL auto_increment,
  `event_date` date NOT NULL,
  `description` varchar(5000) NOT NULL,
  `name` varchar(50) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


You can add and delete columns just don't delete or rename the event_date column.
Diablosblizz
I did that, and I tested it with other months and it worked perfectly! But, for some reason, it doesn't feel like working for January. I think it's a coding error that thinks that the year for month one is the next year. So if it's 2008 and you view the month January then it thinks it's 2009.

EDIT: Nope that's not the problem, this is what I have (echoing the $year and $month variables at the end)

Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 2008 - 01


Nothing, but there is no link. Here is what the event_day looks like: 2008-01-01. What's going on? Sad Also, is there anyway to tell if it's the seventh day and then do a <tr> so it evens out the calender, currently it just has the S M T W T F S above the first 7 numbers then the list continues, and I can't figure it out.
rvec
add this after line 58 to get the day number (0-6) in $day_num:
Code:
$day_num = date('w', mktime(0, 0, 0, $month, $i, $year));


the other problem was a typo:
Code:
if (strlen($month) === 1) {

has to be :
Code:
if (strlen($i) === 1) {
Diablosblizz
This doesn't work either. I don't know what I am doing wrong but it certainly doesn't like me.

EDIT: Oh I think I figured it out! I echoed the month and noticed that when adding the ?month variable to the end of the URL it removed the extra 0 to the beginning of the month and I think that's what's messing it up. I'm not sure why, but that's what it seems. Also, it apparently doesn't work with other months anymore. :S
rvec
try this:
Code:
<?php
//first check for input
if (isset($_REQUEST["month"])) {
    $month = (int) $_REQUEST["month"];
   if (strlen($month) === 1) {   $month = '0'. $month;}
   if ($month > 12) { $month = 12;}
   if ($month < 1) { $month = 1;}
} else {
   $month = date('m');
}

if (isset($_REQUEST["year"])) {
    $year = (int) $_REQUEST["year"];
   if ($year < 1) { $year = 1;}
} else {
   $year = date('Y');
}
//then we want to use our values and make a date off it (notice I left out the day)
$date = $year .'-'. $month .'-';

//now we need a connection
$link = mysql_connect("192.168.1.118", "root", "");
mysql_select_db("database", $link);


// check the month in the database
$mysql = mysql_query("SELECT * FROM `calender` WHERE event_date LIKE '$date%'");

//put all the events in an array
while ($event = mysql_fetch_array($mysql, MYSQL_ASSOC)) {
   $events[] = $event;
}
if (!isset($events)) {
   $events = array();
}

//get the number of days in this month
$days = date('t', mktime(0, 0, 0, $month, 0, 2000));


//make a function to check if a date is an event

function is_event($needle, $haystack) {
   foreach ($haystack as $event) {
      if (in_array($needle, $event)) {
         return true;
      }
   }
   
   return false;
}


//make the calender
for ($i = 1; $i <= $days; $i++) {

   if (strlen($i) === 1) {
      $day = $date .'0'. $i;
   } else {
      $day = $date . $i;
   }

   $day_num = date('w', mktime(0, 0, 0, $month, $i, $year));
   
   if(is_event($day, $events)) {
      echo "<td align='center' valign='middle' height='20px'><a href='test.php?page=add&" . ($i) . "'>". ($i) . "</a></td>\n";
   } else {
      echo "<td align='center' valign='middle' height='20px'>" . ($i) . "</td>\n";
   }
}   

?>
Diablosblizz
Finally, success! Thank you very much! Last question though, do you think it's possible to check if it's the seventh day in the week (being Saturday) and doing a <BR> to even out the table. Somewhat of what I have in the working image in my first post? I've tried this:

Code:
if($i % 7) {
echo "<BR>";
}


This doesn't seem to work, but it's how the previous calender did it. :S
rvec
use $day_num instead of $i there
$i is the num of the month (1-31) and $day_num is the num of the day in the week (0-6)
Diablosblizz
:S It somewhat works. It just makes a list:

Code:
1
2
3
4
5
6
7   8
9
10
11
12
13
14   15
16
17
18
19
20
21   22
23
24
25
26
27
28   29
30
31

- THE CODE -

                        if(is_event($day, $events)) {
                  if($day_num % 7) {
                     echo "<tr>";
                     }
                     echo "<td><a href='calender.php?page=view&day=" . ($day) . "'>" . ($i) . "</a></td>";
                        } else {
                                             if($day_num % 7) {
                     echo "<tr>";
                     }
                        echo "<td>" . ($i) . "</td>";
                     }


I apologize for all these problems, I'm just not up to par.
rvec
don't use $day_num % 7
it's 0-6 like I said before. So better use $day_num == 6 to see if it's a saturday.

and use it before or after the is_event if, because then you can only make one instead of useing the same code twice.

also it'd be easier if you pasted the source code of the output page and how it shoul've been, then I can change the script so it does that.
Diablosblizz
Progress! Very Happy Although, it's skipping the 7th. It aligns fine, but when it gets to Sunday it skips (for the first week).
So like:

S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12
13 14 ...

I think you get the point. Why is this? :S I thank you for your time.

EDIT: Source:

Code:
<?php
include("functions.php");
//first check for input
switch($_GET['page']) {
   default:
if (isset($_REQUEST["month"])) {
    $month = (int) $_REQUEST["month"];
   if (strlen($month) === 1) {   $month = '0'. $month;}
   if ($month > 12) { $month = 12;}
   if ($month < 1) { $month = 1;}
} else {
   $month = date('m');
}

if (isset($_REQUEST["year"])) {
    $year = (int) $_REQUEST["year"];
   if ($year < 1) { $year = 1;}
} else {
   $year = date('Y');
}
//then we want to use our values and make a date off it (notice I left out the day)
$date = $year .'-'. $month .'-';

//now we need a connection
$link = mysql_connect("localhost", "root", "qweasdzxcKeffer");
mysql_select_db("youshout", $link);


// check the month in the database
$mysql = mysql_query("SELECT * FROM `calender` WHERE event_date LIKE '$date%'");

//put all the events in an array
while ($event = mysql_fetch_array($mysql, MYSQL_ASSOC)) {
   $events[] = $event;
}
if (!isset($events)) {
   $events = array();
}

//get the number of days in this month
$days = date('t', mktime(0, 0, 0, $month, 0, 2000));


//make a function to check if a date is an event

function is_event($needle, $haystack) {
   foreach ($haystack as $event) {
      if (in_array($needle, $event)) {
         return true;
      }
   }
   
   return false;
}


//make the calender
?>
       <table>
         <tr>
            <td>S</td>
            <td>M</td>
            <td>T</td>
            <td>W</td>
            <td>T</td>
            <td>F</td>
            <td>S</td>
         </tr>
         <tr>
         
<?php for ($i = 1; $i <= $days; $i++) {

   if (strlen($i) === 1) {
      $day = $date .'0'. $i;
   } else {
      $day = $date . $i;
   }
   $day_num = date('w', mktime(0, 0, 0, $month, $i, $year));
            if(is_event($day, $events)) {
                     if($day_num == 6) {
                     echo "<tr>";
                     }
                     echo "<td><a href='calender.php?page=view&day=" . ($day) . "'>" . ($i) . "</a></td>";
                        } else {
                                             if($day_num == 6) {
                     echo "<tr>";
                     }
                        echo "<td>" . ($i) . "</td>";
                     }
               }
            ?>
         </tr>
      </table>
<?php
break;

case 'view':
get_template_body();
$link = mysql_connect("localhost", "root", "qweasdzxcKeffer");
mysql_select_db("youshout", $link);
$day = $_GET['day'];
$mysqlqer = mysql_query("SELECT * FROM `calender` WHERE `event_date` = '$day'");
$amount = mysql_num_rows($mysqlqer);
   if($amount == 1) {
      echo "test";
   ?>
      <table width="100%">
         <tr>
         <td height="100px">
         Test
         </td>
         </tr>
      </table>
<?php
   }
get_template_footer();
break;
}
?>
Vrythramax
@Diablosblizz

Try re-reading your code ... maybe you will find the answer there.
rvec
This should work:

Code:
<?php
//the includes
include("functions.php");

//make the connection here so we don't need the code twice
$link = mysql_connect("192.168.1.118", "root", "");
mysql_select_db("database", $link);

//2 different views
switch($_GET['page']) {

case 'view':
   get_template_body();

   $day = $_GET['day'];
   $mysqlqer = mysql_query("SELECT * FROM `calender` WHERE `event_date` = '$day'");
   $amount = mysql_num_rows($mysqlqer);
   if($amount == 1) {
      echo <<<TEST

         <table width="100%">
            <tr>
            <td height="100px">
            Test
            </td>
            </tr>
         </table>
TEST;
   }
   get_template_footer();
break;


//the default which we DON'T want first
default:
   //first check for input
   if (isset($_REQUEST["month"])) {
       $month = (int) $_REQUEST["month"];
      if (strlen($month) === 1) {   $month = '0'. $month;}
      if ($month > 12) { $month = 12;}
      if ($month < 1) { $month = 1;}
   } else {
      $month = date('m');
   }

   if (isset($_REQUEST["year"])) {
       $year = (int) $_REQUEST["year"];
      if ($year < 1) { $year = 1;}
   } else {
      $year = date('Y');
   }
   //then we want to use our values and make a date off it (notice I left out the day)
   $date = $year .'-'. $month .'-';


   // check the month in the database
   $mysql = mysql_query("SELECT * FROM `calender` WHERE event_date LIKE '$date%'");

   //put all the events in an array
   while ($event = mysql_fetch_array($mysql, MYSQL_ASSOC)) {
      $events[] = $event;
   }
   if (!isset($events)) {
      $events = array();
   }

   //get the number of days in this month
   $days = date('t', mktime(0, 0, 0, $month, 0, 2000));


   //make a function to check if a date is an event

   function is_event($needle, $haystack) {
      foreach ($haystack as $event) {
         if (in_array($needle, $event)) {
            return true;
         }
      }
      
      return false;
   }


   
   //some HTML
   echo <<<HTML
<table>
         <tr>
            <td>S</td>
            <td>M</td>
            <td>T</td>
            <td>W</td>
            <td>T</td>
            <td>F</td>
            <td>S</td>
         </tr>
HTML;
   //make the calender
   for ($i = 1; $i <= $days; $i++) {
      
      if (strlen($i) === 1) {
         $day = $date .'0'. $i;
      } else {
         $day = $date . $i;
      }

      $day_num = date('w', mktime(0, 0, 0, $month, $i, $year));
      if ($day_num == 0) { echo "<tr>"; }
      
      if(is_event($day, $events)) {
         echo "<td align='center' valign='middle' height='20px'><a href='test.php?page=add&" . ($i) . "'>". ($i) . "</a></td>\n";
      } else {
         echo "<td align='center' valign='middle' height='20px'>" . ($i) . "</td>\n";
      }
      if($day_num == 6) {echo "</tr>";}
   }   
echo '</table>';
break;
}
?>


and maybe you should read up about <table> <tr> and <td> before you use them again Razz
Diablosblizz
Woot, that's perfect. Just so I don't have to start another post, am I able to change a iframe source with a link inside a iframe?

So, I have the calender in a iframe, and when I click a event I want it to pop up in the iframe called view that is on the same page as the calender just on another iframe.

Here is an image:



If you click "LINK", which is in a iframe, I need it to come in the second iframe called view. Is this possible?
rvec
http://www.trans4mind.com/personal_development/HTMLGuide/iframes3.htm

there you are Smile
Diablosblizz
Okay I'm sure you hate me now, for all the problems I've caused and I apologize. Now the problem is that it's always starting at Saturday, even if the month starts on Tuesday.



Sorry for all the hassle.
rvec
Code:
      if ($i == 1 && $day_num != 0) {
         for ($a = 1; $a <= $day_num; $a++) {
            echo '<td></td>';
         }
      }

add that after this line:
$day_num = date('w', mktime(0, 0, 0, $month, $i, $year));

The script is getting bigger Razz
Diablosblizz
Gah, now it's showing the right date but not displaying the right days in the month. For February, this year, it ends on 29 but on the calender (on my web) it ends on the 31st. :S
rvec
oops forgot that,
replace this:
Code:
//get the number of days in this month
   $days = date('t', mktime(0, 0, 0, $month, 0, 2000));

by this:
Code:
//get the number of days in this month
   $days = date('t', mktime(0, 0, 0, $month, 1, $year));


(so only change 2000 to $year and one 0 to a 1)

also move this line:
Code:
if (strlen($month) === 1) {   $month = '0'. $month;}

to after this:
Code:
    } else {
      $month = date('m');
    }
Diablosblizz
Holy heck, that was a lengthly event. It seems to be working smoothly, now I just need to fix my "always-crashing" server. Wink

Thank you very much for your time.
Diablosblizz
I apologize for bringing this back to life, I know you want more. Razz I just don't really want to touch this code because it will probably cause more problems. Now, I am looking at a feature where it links all the days except the ones with events. So let's say there was a event on the 12th only, there would be a link on each day except the 12th.

Is this possible? And I apologize for digging this thread up from it's grave. Wink
rvec
remember the
Code:
if(is_event($day, $events)) {
line?
That checks if a day is an event and returns true if it is.
so everything before the else is executed if the day is an event, and everything after it if it isn't......
You should be able to get it from there Wink
Related topics
Problem with Subdomains
Strange Fire fox problem
Logout problem (Cookie?)
How to solve this problem? Thanks
I have a problem....Mozilla Firefox
post nuke problem
anyone had this phpBB problem?
phpbb problem....
Transport yourself over the Net
Problem with my password
Pictures problem...
database problem
same problem over here...
Header("Location: PROBLEM");
Menu problem
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.