Jump to content

Hi there,


andys2006

Recommended Posts

Hi there,

 

I'm trying to create an interactive calendar app for my new employers which has me a bit stuck. Basically, the company has 10 or 12 splinter companies which all host different events. What I want to create is a calendar app where you enter details about events that are coming up, select which company these apply to, and then when the calendar is displayed, you can choose to view all the events for a particular company for a particular month.

 

I've sorted the input/deletion of events from the system, and I'm now trying to get the events displaying on the calendar. I just downloaded a PHP calendar which I modified to display a single event using data from the database - however, once I tried to get it working for multiple events, I ran into problems. I'm having trouble figuring out what to try next - can anyone suggest what I could do?

 

Here is the code for the calendar script:

 

<?php

// Sets the database connection data to local variables

$host = "localhost";

$pw = "xxxxxxxx";

$user = "xxxxxxxxxxx";

$db = "xxxxxxxxxxxxx";

 

// Makes database connection

$db_connection =  mysql_connect ($host, $user, $pw) or die("could not connect to db");

 

// Select database to be used.

mysql_select_db($db);

 

$php_self = $_SERVER['PHP_SELF'];

 

$getEventQuery = "SELECT * FROM event_table WHERE evtMonth = '2'";

$getEventResult = mysql_query($getEventQuery) or die ("Event query failed: " . mysql_error());

 

// This just displays the two events for february to make sure they are being //pulled from the db.

 

while($getEventArray = mysql_fetch_array($getEventResult))

            {

            $dayVal = $getEventArray['evtDay'];

            echo $dayVal;

            $evtName = $getEventArray['evtName'];

            echo $evtName;

            }

 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Calendar</title>

<style type="text/css" media="all">

 

    body {

 

        background-color: #2A2A2A;

        color: #EEEEEE;

        font-family: Tahoma, Verdana, sans-serif;

        font-size: 10px;

 

    }

 

    table {

 

        width: 900px;

 

    }

 

    td {

        width: 20px;

        padding: 5px;

        border: 1px solid #666666;

        text-align: left;

 

    }

 

</style>

</head>

<body>

<?php

 

    // get this month and this years as an int

    $thismonth = ( int ) date( "m" );

    $thisyear = date( "Y" );

 

    // find out the number of days in the month

    $numdaysinmonth = cal_days_in_month( CAL_GREGORIAN, $thismonth, $thisyear );

 

    // create a calendar object

    $jd = cal_to_jd( CAL_GREGORIAN, date( "m" ),date( 1 ), date( "Y" ) );

 

    // get the start day as an int (0 = Sunday, 1 = Monday, etc)

    $startday = jddayofweek( $jd , 0 );

 

    // get the month as a name

    $monthname = jdmonthname( $jd, 1 )

 

?>

<table>

    <tr>

        <td colspan="7"><div align="center"><strong><?= $monthname ?></strong></div></td>

    </tr>

    <tr>

        <td><strong>S</strong></td>

        <td><strong>M</strong></td>

        <td><strong>T</strong></td>

        <td><strong>W</strong></td>

        <td><strong>T</strong></td>

        <td><strong>F</strong></td>

        <td><strong>S</strong></td>

    </tr>

    <tr>

<?php

 

    // put render empty cells

 

    $emptycells = 0;

 

    for( $counter = 0; $counter <  $startday; $counter ++ ) {

 

        echo "\t\t<td>-</td>\n";

        $emptycells ++;

 

    }

 

    // renders the days

 

    $rowcounter = $emptycells;

    $numinrow = 7;

 

 

    for( $counter = 1; $counter <= $numdaysinmonth; $counter ++ )

    {

 

 

        $rowcounter ++;

 

// Where I've modified it

 

            if($counter == $getEventArray[$counter])

            {

                printf("\t\t<td>%s - %s</td>\n", $counter, $getEventArray['evtName']);

            }

            else

            {

                echo "\t\t<td>$counter</td>\n";

            }

 

// to here

 

            if( $rowcounter % $numinrow == 0 )

            {

 

                echo "\t</tr>\n";

 

                if( $counter < $numdaysinmonth )

                {

 

                    echo "\t<tr>\n";

 

                }

 

            $rowcounter = 0;

 

            }

 

    }

 

    // clean up

    $numcellsleft = $numinrow - $rowcounter;

 

    if( $numcellsleft != $numinrow ) {

 

        for( $counter = 0; $counter < $numcellsleft; $counter ++ ) {

 

            echo "\t\t<td>-</td>\n";

            $emptycells ++;

 

        }

 

    }

 

?>

    </tr>

</table>

</body>

</html>

 

I guess my problem is that I can't figure how to iterate through the entries in the array - so I'm trying to put in while loops and foreaches which won't solve things because it'll then try to add in all the events from the database at once, rather than, for example:

 

event days from database: 4th, 8th, 22nd of Feb

 

script running:

 

counter = 1, add day value,

counter = 2, add day value,

counter = 3, add day value,

counter = 4, add day value and event details,

counter = 5, add day value,

counter = 6, add day value,

counter = 7, add day value,

counter = 8, add day value and event details,

counter = 9, add day value,

counter = 10, add day value,

counter = 11, add day value,

counter = 12, add day value,

counter = 13, add day value,

counter = 14, add day value,

counter = 15, add day value,

counter = 16, add day value,

counter = 17, add day value,

counter = 18, add day value,

counter = 19, add day value,

counter = 20, add day value,

counter = 21, add day value,

counter = 22, add day value and event details,

counter = 23, add day value,

counter = 24, add day value,

counter = 25, add day value,

counter = 26, add day value,

counter = 27, add day value,

counter = 28, add day value

 

terminate.

 

Thats what I want to happen, because that way the events are added in on the fly. Maybe if there was some way of adding the next element in the array into a local variable once the first event had been put onto the calendar?

 

However, if I have to use a while loop or a foreach to loop through the array finding the days, then I figure the program execution would do something like this:

 

script running:

 

counter = 1, add day value,

counter = 2, add day value,

counter = 3, add day value,

counter = 4, add day value and event details,

counter = 8, add day value and event details,

counter = 22, add day value and event details,

counter = 5, add day value,

....

counter = 28, add day value,

 

terminate.

 

Sorry about this, I'm trying to be as clear as possible, but I'm having trouble wrapping my head round this one, and since its uncharted territory for me, I'd appreciate any help you guys could provide :-D

 

Cheers

 

Andy

Andy2006 is online now Report Post  Edit/Delete Message

Link to comment
https://forums.phpfreaks.com/topic/39151-hi-there/
Share on other sites

Try this:

 

while( month = feb )
{
  $my_data = Get data from DB for date  
  Print '<td>' . $my_data .'</td>';
  ?? maybe insert a </tr><tr> ??
  add 1 day to date
}

 

What is wrong with that?

 

If you are displaying 1 month = 31 db accesses

1 year = 365 (ish)

 

If you are trying to remove these multiple db access, that is optimization.

 

**Do optimization last***, and only do it if you really really need to.

 

http://www.google.co.uk/search?num=100&hl=en&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=premature+optimization&spell=1

 

Keep It Simple Stupid  ;D  ;D  ;D  ;D

 

monk.e.boy

 

 

Link to comment
https://forums.phpfreaks.com/topic/39151-hi-there/#findComment-188607
Share on other sites

I wasn't trying to optimize it, I just hadn't ever taken anything like this on before, so was having trouble figuring out how it would work. So your saying that what I should do is use a while loop, and set up the database so it holds data for every day of the year? would that not be a nightmare in terms of setting up the DB? As in, you would have to have one table for each month, and one row for each day?

Link to comment
https://forums.phpfreaks.com/topic/39151-hi-there/#findComment-189343
Share on other sites

you would have to have one table for each month, and one row for each day?

 

No.

 

I just re-read your first post. This is your new job?  ;D

 

Make a DB table that has event and date.

 

Then go through the loop I said, the query would be something like:

 

SELECT * FROM my_events WHERE event_date = '01-01-2007'

 

Then on the next day, change the date string in the above query.

 

You may get 6 events for 1/1/2007, so display them all (use another loop)

 

monk.e.boy

Link to comment
https://forums.phpfreaks.com/topic/39151-hi-there/#findComment-189345
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.