Link to home
Start Free TrialLog in
Avatar of maccaj51
maccaj51Flag for Afghanistan

asked on

Php event calendar

We currently have an events start date in this format : 2010-06-14 00:00:00   in a mysql database...

How would i create an events <table> for the next 5 months showing the events titles in php??

Any help would be great... but please for kind.. im new to all this! haha
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Can you post your mysql table structure, please?
Avatar of maccaj51

ASKER

`id` int(11) NOT NULL AUTO_INCREMENT,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `title` varchar(300) NOT NULL,
  `duration` varchar(300) NOT NULL,
  `venue` varchar(300) NOT NULL,
As a trivial example you can try this

<?php
   $sql = "SELECT * FROM table WHERE MONTH(date) BETWEEN MONTH(NOW) AND MONTH(NOW)+5";
   $res = mysql_query($sql);
   echo "<table>";
   while ($row = mysql_fetch_assoc($res)){
       echo "<tr><td>".$row['date']."</td><td>".$row['title']."</td></tr>";
   }
   echo "<table>";
?>

Let me know if you have some problems.

Cheers
ive got -
mysql_select_db("DBNAME", $con);
$sql = mysql_query("SELECT * FROM events WHERE MONTH(date) BETWEEN MONTH(NOW) AND MONTH(NOW)+5");
 $res = mysql_query($sql);
   echo "<table>";
   while ($row = mysql_fetch_assoc($res)){
       echo "<tr><td>".$row['date']."</td><td>".$row['title']."</td></tr>";
   }
   echo "<table>";
?>

and this doesnt work...

im tryin to get a full calendar with days with events highlighted?! any ideas?
How should the highlight look/work?
something like this would be fine...
http://webscripts.softpedia.com/screenshots/PHP-Calendar-Basic-20594.png

just applying a different class to populated days...
Hi,

    Glad to help you ....

    why do you want to re invent the wheel ...just download the available scripts with all the features and use it / customize it...

 Follow the links below

         http://supercali.inforest.com/demo.php
       
         http://www.easyphpcalendar.com/

 IF you want to build it by yourself then follow the link below

        http://www.devarticles.com/c/a/PHP/A-Useful-Event-Calendar-Written-In-PHP/

  If it helps then its cool else let me know ....


Regards,
Jerome Dennis D
i already have an events database so dont really want to have to input the items again....
"and this doesnt work..." This doesn't give any information: what is the error? Or the result?
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\cal.php on line 19
p.s thanks for all ur help so far
Give me a bit, I've got code somewhere for this.....
Sorry, there is an error in my query. Try this:

<?php
$sql = "SELECT * FROM events WHERE MONTH(date) BETWEEN MONTH('2010-10-04 00:00:00') AND MONTH(NOW())+5";
   $res = mysql_query($sql) or die (mysql_error());
   echo "<table>";
   while ($row = mysql_fetch_assoc($res)){
       echo "<tr><td>".$row['date']."</td><td>".$row['title']."</td></tr>";
   }
   echo "<table>";
?>

I've tested and it works.
For stylish you have to use css.

Cheers
For instance you could use something like in code snippet.

Cheers
<style type="text/css">
/*<![CDATA[*/
<!--
 .cell{
   width:100px;
   height:100px;
   border:solid 1px black;
 }
-->
/*]]>*/
</style>
<?php
$sql = "SELECT * FROM events WHERE MONTH(date) BETWEEN MONTH('2010-10-04 00:00:00') AND MONTH(NOW())+5";
   $res = mysql_query($sql) or die (mysql_error());
   echo "<table>";
   while ($row = mysql_fetch_assoc($res)){
       $day = date('l', $row['date']);
       echo "<tr><td class='cell'>".date('l d-m-Y' ,$row['date'])."</td><td class='cell'>".$row['id']."</td></tr>";
   }
   echo "<table>";
?>

Open in new window

Hi MarqusG,

I know how to show the events and their date just in a list... i really need to place them in a visible calendar!

Thanks for your help though
Well, I found this tutorial that seems to be exactly what you need. (Now I can't test it, but if you need I'll do it later)

the link is http://hubpages.com/hub/Simple-Event-calendar-PHP--MySQL

Cheers
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks bportlock... massive help!

Just quickly... how would i populate this with events from my database?
I've marked a spot in the code with a "sample" query that you can use to populate a variable called "$app". Uncomment the code and put the correct SQL in and (assuming you have a database connection) then it should work OK.
If i could give him more points i would!!!!!!
I am havin an issue with certain months and certain events not showing... especially November... should this script work fine with timestamp!?
You might need to re-jig the query a bit, but as long as you can get a date to compare in the MySQL statement then it should work. Look at using MySQL's DATE_FORMAT to redo the Timestamp

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

SELECT * FROM myTable
WHERE
      DATE_FORMAT( timeStampColumn, '%Y-%M-%D') = '$todaysDate'
ORDER BY timeStampColumn

or use GET_FORMAT

SELECT * FROM myTable
WHERE
      GET_FORMAT( timeStampColumn, 'ISO') = '$todaysDate'
ORDER BY timeStampColumn
got it to work... ur genius... doesnt seem to show 2md,3rd etc event if there is multiple events on same day tho!
Make sure that when you add to $app you CONCATENATE rather than assign. Use

$app .= $myNewEvent;

rather than

$app = $myNewEvent

Note the .= rather than the plain =
if i do that it comes up with this...

ive changed the code from

$app = $rw['title'];
to
$app .= $rw['title'];
Picture-1.png
// Data rows
          //
          for ( $row=0; $row < 6; $row++ ) {
               $t .= "<tr>\n";
               for ( $col=0; $col < 7; $col++ ) {
                    $day = $day = $days[$row*7 + $col];

                   
                    $todaysDate = sprintf("%04d-%02d-%02d", $y, $m, $day);
                    $rs = mysql_query("select title FROM events WHERE `date`='$todaysDate'");
                    $rw = mysql_fetch_assoc( $rs );

                    $app .= $rw['title'];                  

                    $t .= "<td class='day'>$day$app</td>\n";

               }
               $t .= "</tr>\n";
          }

          $t .= "</table>\n";

          return $t;
     
         
     }
I thought you had written the SQL insert to handle multiples.... the following is UNTESTED but should be close. I have marked the "new stuff"

// Data rows
          //
          for ( $row=0; $row < 6; $row++ ) {
               $t .= "<tr>\n";
               for ( $col=0; $col < 7; $col++ ) {
                    $day = $day = $days[$row*7 + $col];


                    $todaysDate = sprintf("%04d-%02d-%02d", $y, $m, $day);
                    $rs = mysql_query("select title FROM events WHERE `date`='$todaysDate'");

                    // New stuff starts -------------------

                    $app = "";

                    if ( $rs ) {
                         while( $rw = mysql_fetch_assoc( $rs ) )
                              $app .= $rw['title'];
                    }
                    
                    // New stuff ends ---------------------

                    $t .= "<td class='day'>$day$app</td>\n";

               }
               $t .= "</tr>\n";
          }

          $t .= "</table>\n";

          return $t;

Open in new window

Looking at the output, it might alos be an idea to add a BREAK before each appointment

$app .= "<br/>" . $rw['title'];

or maybe even two breaks

$app .= "<br/><br/>" . $rw['title'];
YOU ARE QUITE LITERALLY A HERO!!!!!! BEST EXPERT!!!!!
Does that mean it worked OK?

haha no its rubbish ;)...

WORKED A CHARM!!!
ive posted a related question... that u probably can help with!