PDA

View Full Version : Updating month <a name""> dynamically



Schmoopy
01-29-2009, 11:00 AM
I have recently put events for my site into a database so it requires less work when updating. But I'm just trying to think of a way to code the following:

Query database for the first event in a specific month, for example I have 3 events. Two at 03/01/09 (d/m/y), and another at 05/01/09. Since there can be two events on the same day, I want the one that comes first alphabetically to have the <a name="jan"> tag so it points to the beginning of that month. So it needs to loop through, check for the first occurrence of an event in a given month and then if there are duplicates, go to the first alphabetically. I feel this is a little bit tough of me, so any guidance would be greatly appreciated,

Thanks,

Jack.

P.S - Just ask if you need anything explained in greater detail.

Schmoopy
01-30-2009, 01:55 AM
Bump, I know this thread hasn't been up that long but I really need a reply as soon as possible, can't put up the new version until I get this fixed,

Thanks,

Jack.

Nile
01-30-2009, 02:04 AM
Don't worry. I'm always trying. :) I am trying, but I also have a few other things.. if you want I can give you the current code:


$months = array(
1 => "Janurary",
2 => "Feburary",
3 => "March",
4 => "April",
5 => "May",
6 => "June",
7 => "July",
8 => "Agust",
9 => "September",
10 => "October",
11 => "November",
12 => "December"
);

function organizeMonths($date){ /*array*/
global $months;
for($i = 0; $i < count($date); ++$i){
$date[$i] = explode("/", $date[$i]);
foreach($months as $key => $value){
if($date[$i][0] == $value){
$date[$i] = $key.":".$value;
}
}
return sort($date);
}
}
echo organizeMonths(array("Janurary/03/2009"));

But it doesn't work yet. =/

Schmoopy
01-30-2009, 02:11 AM
Ok, well I just appreciate any effort :D I'll check tomorrow if you've done any more. Thanks for the code so far anyway :)

Nile
01-30-2009, 03:45 AM
Ok, I found an easier way. You can mess around with it(it organizes by month - then date, you can change this by changing the mktime() return in dmytoMk().


function dmytoMk($date) {
$date = explode("/",$date);
return mktime(0, 0, 0, $date[0], $date[1], 0);
}
$arr = array('03/01/09','02/07/01','03/01/31');
foreach($arr as $key => $value){
$arr[$key] = date("md", dmytoMk($value))." - ". $value;
}
sort($arr);
echo implode("<br>",$arr);

It doesn't organize by year because:
1) It would be harder
2) I assume your already putting the events in the proper year.

You need the first value that may look like: 0301, but after you sort($arr) you can remove it with maybe a strreplace, or an explode.

Schmoopy
01-30-2009, 12:27 PM
Thanks for that, but with me being not so great at this - how can I translate this into putting the <a name="jan"> anchor dynamically after it finds the first event in that month. Here is a link to the site so you have more of an understanding of what I'm on about.

Click here (http://www.bristoldnb.co.uk)

Nile
01-30-2009, 01:07 PM
Here ya go:


function dmytoMk($date) {
$date = explode("/",$date);
return mktime(0, 0, 0, $date[0], $date[1], 0);
}
$arr = array('12/01/09','02/07/01','05/01/31');
foreach($arr as $key => $value){
$arr[$key] = date("md", dmytoMk($value))." || ".date("M", dmytoMk($value));
}
sort($arr);
$month = array();
$output = "";
for($i=0;$i<count($arr);++$i){
$month[] = explode(" || ", $arr[$i]);
$output .= "<a name=\"{$month[$i][1]}\">{$var}</a>";
}
echo $str;

I guess you can put that all in one function so you don't have to do that every page - unless you're using while().

Schmoopy
01-30-2009, 04:47 PM
Ah great, thanks for that :)

Nile
01-30-2009, 08:31 PM
Glad to help you Schmoopy!