/    Sign up×
Community /Pin to ProfileBookmark

Event Array: Event Calendar

I’m building an event calendar, and so far, I’ve got the calendar part working correctly, but without it displaying any events.

To be honest, I’m not quite sure how to do this. I’ve found tutorials that show how to make dates with events on them into hyperlinks, but not how to display the actual event name in the calendar cell.

Sudo Code:

[code=php]
while ($row = mysql_fetch_array($sql))
{
$eventDays[$row[‘day’]] = $row[‘event_name’]; //create an array of event names with the day as an index (maybe?)
}
[/code]

and in the ‘FOR’ loop that’s building day cells:

[code=php]
for($current_day = 1; $current_day <= $days_in_month; $current_day++)
{
if(($week_day_position % 7) == 0)
{
$out .= “</tr><tr>n”; // start a new row
$week_block++;
$week_day_position = 0;
}
$out .= “<td class=”monthDay”>$current_day”;
// I guarantee PHP isn’t going to like this… (sort of like a query against an array?)
foreach($eventDays where $eventDays[‘day’] == $current_day)
{
$out .= $eventDays[‘day’];
}
// Ok, you can stop laughing at me now… 🙂
$out .= “</td>n”;
$week_day_position++;
}
[/code]

What is the best way of doing this? I figure I could put a query inside the loop, and have it run a query for every single day that it generates a box for, but that seems inefficient.

Thanks for your help ?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NapApr 15.2007 — You could do something like:
[code=php] $eventday = Date("Ymd") // Gives you a result eg: 20070416
$eventlist[$eventday][$eventnumber] = " Event description "
[/code]


So that for each day, you would have a list of events (1 - $eventnumber)

By using Date("Ymd") it makes each day a unique vector of the array and you don't have to worry about an event in the next (or previous) month (or year) getting confused.


Further in your program, you can locate the events by referencing the $eventlist array by using :

$eventlist[20070416][$i] or

$eventlist[$dateofevent][$i]

Hope this gives you some ideas?

Cheers,

Nap
Copy linkTweet thisAlerts:
@lightnbauthorApr 22.2007 — Thanks, Nap. I got it to display one event on the appropriate day using the code below, but am still not sure how to make it show more than one event on a given day if that is the case.

[code=php]
while ($row = mysql_fetch_array($sql))
{
$eventday = $row['event_date'];
$eventlist[$eventday] = $row['event_name'];
}
[/code]


And this happens inside the while loop that draws the calendar table:

[code=php]
$eventday = "$year-$month-$current_day";
$output .= $eventlist[$eventday];
[/code]



I think I could get it to draw them using:

[code=php]
foreach ($eventlist[$eventday] as $event_name)
{
$output .= $event_name;
}
[/code]


but how should I assign the event numbers to the sub-array following the query?
Copy linkTweet thisAlerts:
@NapApr 22.2007 — What you need is a second dimension in your event list.

That is to say each event has a DATE and an ID.

Event_ID could be:

1)simply an AUTO INCREMENT field in the SQL dB, or

2)a sequentual number for each day, or

3) the time of the event if the events are to be spaced out.

or combinations of the above.

[code=php]while ($row = mysql_fetch_array($sql))
{
$eventlist [$row['event_date']] [$row['event_id']] = $row['event_name'];
} [/code]


To output them try this (same as what you suggested below):
[code=php]$eventday = "$year-$month-$current_day";
foreach ($eventlist[$eventday] as $event_name)
{
$output .= $event_name;
}[/code]


Here is an example using static values in a 2 dimensional array:
[code=php]<?php
$eventlist['2007-04-16']['1'] = "hello1";
$eventlist['2007-04-16']['10'] = "hello10";
$eventlist['2007-04-16']['31'] = "hello31";
$eventlist['2007-04-16']['43'] = "hello43";
$eventlist['2007-04-16']['45'] = "hello45";
$eventlist['2007-04-16']['65'] = "hello65";
$eventlist['2007-04-16']['76'] = "hello76";

$eventday = '2007-04-16';
foreach ($eventlist[$eventday] as $event_name)
{
echo "$event_name<br />";
}

?>[/code]

Output Result:hello1
hello10
hello31
hello43
hello45
hello65
hello76

Cheers,

Nap
×

Success!

Help @lightnb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...