Jump to content

Looping/incrementing days already in a loop


dave_bonham

Recommended Posts

Hey guys and girls,

 

I've been having this bugging problem for days now and it's really frustrating me, so i need a fresh set of eyes to show me where i'm going wrong.

 

I have a little calendar script that works great from my database, for linked entries etc.., problem is i can't get it to span multiple days.

 

This bit of code is causing the problem.

// simulate the start and end day
$startArray = array('03');
$endArray = array('06');
// This will test the loop array
} else if(in_array($i, $startArray)){
	while($startArray < $endArray){
		echo $adj."<td id='live'><a href='cal2.php?d=$i'>".$i."</a><br />"; 
		echo " </td>";
	}

 

so what i'd like to have happen is from the 3rd to the 6th be styled, but it comes out blank. the while loop doesn't work, i've tried incrementing $i in the loop but it spirals away and doesn't stop at 6.

 

single days, todays date, and consecutive seperate entries work fine.

 

note: it's in an elseif statement with a missing end curly bracket as it is part of a working set of if statements.

else if($startArray[0] < $endArray[0]){
		for($i = $startArray[0]; $i < $endArray[0]; $i++){
			echo $adj."<td id='live'><a href='cal2.php?d=$i'>".$i."</a><br />"; 
			echo " </td>";
	}

another bit of code to try and deal with the same issue again produces nothing.

 

hoping someone can ease my pain  :shrug:

You'll need something a little smarter than that.

Right now you have something that prints a calendar, right? Functional except for the bit about highlighting multiple days (or highlighting any at all, I don't know). Since that works it'll be best to add as little code as possible.

 

For the highlighting, keep a counter variable that counts how much highlighting is to be done. It starts off at zero=no highlighting; go through an array of ranges (easier than arrays of start and end dates) and increment if the current day is within.

$startArray = array(3);
$endArray = array(6);

// turn the two arrays into one multidimensional array
// (would be great if you had this to start, but if not...)
$ranges = array();
for (reset($startArray), reset($endArray); key($startArray) !== null && key($endArray) !== null; next($startArray), next($endArray)) {
$ranges[] = array(current($startArray), current($endArray));
}
// now $ranges = array(array(3, 6));

// while printing out each $day in the month {
$highlight = 0; // a counter (to support overlapping periods)
foreach ($ranges as $range) {
	if ($range[0] 	}

if ($highlight > 0) {
	// print a highlighted day
	// could use different styles for highlight=1, highlight=2, etc
} else {
	// print a normal day
}
// }

There is a smarter way of doing it (by adjusting $highlight when you enter and leave a period) but this should be enough to deal with for now.

absolutely brilliant.

worked first time, no head scratching whatsoever.

 

thank you ever so much dude. sending good kudos your way.

 

i have so called 'friends' who consider themselves experts at this stuff, all i got was oh just make them objects blah blah ... and a total stranger solves it first go.

 

this is why i love the intertweb  :D

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.