Page 1 of 1

php hcalendar array issue

Posted: Sat Feb 10, 2007 8:41 pm
by david swain
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


HELP !!!!!

It my first post here so I thought I would start in the traditional way !

I’m fairly new to web design and even newer to php. Basically im setting up a site and want an event calendar marked up in hCalendar. i have a mysql table with the following fields :

ID_cal smallint(6) No auto_increment
startDate_cal datetime Yes NULL
endDate_cal datetime Yes NULL
summary_cal varchar(255) utf8_unicode_ci Yes NULL
summaryURL_cal varchar(255) utf8_unicode_ci Yes NULL
location_cal varchar(255) utf8_unicode_ci Yes NULL
locationURL_cal varchar(255) utf8_unicode_ci Yes NULL
description_cal varchar(10000) utf8_unicode_ci Yes NUL
inclusive_cal tinyint(1) No
public_cal tinyint(1) No

Now I have the system sort of working… I can get it to spit out the results of the database WITH APPRORIATE MARKUP. The fields are I think fairly self explanatory with the exception of INCLUSIVE_CAL and PUBLIC_CAL. Public is to show the results for the public, some events are for me only, no problems with that. Inclusive_Cal is the one giving me problems.

Basically I do performances (acting gigs etc) and quiet often want to supply a range of dates; e.g. a run of shows for a week starting at 7.30 every night.

What I want to do is instead of adding 7 or 8 events to the table which seems inefficient is add one and get the code to ‘spit out‘ 7 discrete events. There are also things like matinees to consider which start at a different time. What i was thinking (using the above example) is specify the date range of a week as one event in the table and then add the matinee as a separate events: eg:

[b]Event 1[/b]
start date 2007-03-05 19:45:00
end date 2007-02-10
inclusive yes
rest of event details..

[b]Event 2[/b]
start date 2007-03-10 19:45:00
end date NULL
inclusive NO
rest of event details….

Then i basically want the php code to get the events from the table, create 7 events from the results of event one, an eight from the results of event 2, sort the accordingly to date and print to screen.

Sounds simple enough, and im sure it is the only problem is I have no idea how to do it !

In the example http://davidswain.co.uk/cal-include.php

I have got working I have used mysql_fetch_array to get the results from the table which will them put them out as a event with a date range of a week.

Yes it is but ugly and unformatted by why worry about that before it works ?
My code is:

Code: Select all

<?php require($_SERVER['DOCUMENT_ROOT']."/includes/inc-config.php"); ?>
<?php require($_SERVER['DOCUMENT_ROOT']."/includes/inc-db-functions.php"); ?>

<?php

$sql = 'SELECT * from tbl_event WHERE startDate_cal > ' . date(y) . ' ORDER BY startDate_cal ASC';
$result = db_connect($sql);

while ($row = mysql_fetch_array($result)) {

?>
<span class="vevent">
<a href=" <?php echo $row["summaryURL_cal"]; ?>" title="<?php echo $row["summaryURL_cal"]; ?>" class="summary url"><?php echo $row["summary_cal"]; ?></a></strong>

<abbr title="<?php echo date(c,strtotime($row["startDate_cal"])) ?>" class="dtstart"><?php echo date($row["startDate_cal"]); ?></abbr>

<?php
if ($row["endDate_cal"] == NULL) {

}
elseif (date($startDate) == date($endDate)) {
echo "–" . "<abbr title=\"" . date(c, strtotime($row["endDate_cal"])) . "\" class=dtend >" . $row["endDate_cal"] . "</abbr>";
}
else {
echo "–<abbr title=" . date(c, strtotime($row["endDate_cal"])) . " class=dtend>" . $row["endDate_cal"] . "</abbr>";
}

?>

<a href="<?php echo $row["locationURL_cal"]; ?>" class="location"><?php echo $row["location_cal"]; ?></a>
<span class="description"><?php echo $row["description_cal"]; ?></span></span><br />

<?php
}
?>
Please if you can help me do !

Thanks

David Swain


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Feb 10, 2007 9:45 pm
by Kieran Huggins
Welcome aboard David!

If I understand correctly you want to take two times (day 1 @ 5pm and day 7 @ 5pm) and do something for every day at 5pm in between?

If that's the case, I would use strtotime() to compare timestamps:

Code: Select all

$cur_day = strtotime($first_day);
while($cur_day <= strtotime($last_day)){

     //do stuff here, like printing a calendar entry

     // increment the current day by a day
     $cur_day = strtotime('+1 day',$cur_day);
}
Make sense? I just love strtotime() 8)

Posted: Sat Feb 10, 2007 10:38 pm
by david swain
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


excellent and very nearly!

if had to modify it a bit to get:

Code: Select all

while ($row = mysql_fetch_array($result)) {

?>
<span class="vevent">
<?php 
$cur_day = strtotime($row['startDate_cal']);
while($cur_day < strtotime($row['endDate_cal'])) {
?>
	<a href=" <?php echo $row["summaryURL_cal"]; ?>" title="<?php echo $row["summaryURL_cal"]; ?>" class="summary url"><?php echo $row["summary_cal"]; ?></a></strong>
	
	<abbr title="<?php echo $cur_day ?>" class="dtstart"><?php echo date(c, $cur_day) ?></abbr><br />
	</span>
	<?php
     // increment the current day by a day
     $cur_day = strtotime('+1 day',$cur_day);
}
?>


<a href=" <?php echo $row["summaryURL_cal"]; ?>" title="<?php echo $row["summaryURL_cal"]; ?>" class="summary url"><?php echo $row["summary_cal"]; ?></a></strong>
	
	<abbr title="<?php echo $cur_day ?>" class="dtstart"><?php echo date(c, $cur_day) ?></abbr><br />
	</span>
<?php 
	}
?>
and the results i get are :

2007-03-05T19:45:00+00:00
2007-03-06T19:45:00+00:00
2007-03-07T19:45:00+00:00
2007-03-08T19:45:00+00:00
2007-03-09T19:45:00+00:00
2007-03-10T19:45:00+00:00
2007-03-10T14:45:00+00:00

which is almost but not quite. what i need to do is pull the records, split them if necesary, sort them by date and then write them to screen.

But im certainly a lot closer !

Thanks Kieran!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Feb 10, 2007 11:12 pm
by Kieran Huggins
Don't forget to make it $cur_day <= strtotime($row['endDate_cal']) or you'll drop the last showtime ;-)

If you want to sort the final records by time, you could just add the timestamps to an array, sort the array, then iterate through it while spitting out your final code.