Jump to content

If statement and Array values


rvdb86

Recommended Posts

Hi, I hope someone can help me with my code...

 

I have an array that is populated by a database table:

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

	$SeasonDates[$row['Id']]['startDate'] = $row['seasonStart'];	
	$SeasonDates[$row['Id']]['endDate'] = $row['seasonEnd'];
	}

 

Now the user provides a start date and end date, and I want to check these dates against the values in the season dates:

while ($startDate <= $endDate)
{

foreach ($SeasonDates as $key => $SeasonDate)
{	
	if ($startDate < date("Y-m-d", strtotime($SeasonDate["startDate"])) || $startDate > date("Y-m-d", strtotime($SeasonDate["endDate"])))
	{
		$lowSeasonDays[$LS++]["Month"] = date("m", strtotime($startDate));
		echo "<font color=\"#00CC33\">Low Season</font>";
	} else
	{
		$highSeasonDays[$HS++] = date("m", strtotime($startDate));
		echo "<font color=\"#FF0000\">High Season</font>";
	}
}

echo "<br />";

$startDate = date("Y-m-d", strtotime($startDate."+1 days"));
}

 

The code works, but doesnt work  :P for each date range in the array it checks the dates and if it falls within a season it prints "high season".

However because of the foreach statement the $lowSeasonDays array and $highSeasonDays array are not correct.

Let me explain in a bit more detail:

In the first loop of the foreach statement it checks the dates and each date that falls in season it increments highSeasonDays array and if it does not it increments thelowSeasonDays array. Great however the problems start on the second+ loop:

the dates are checked and even the dates that were marked as high season in the first loop are now considered low season... therefore the final sum of the $lowSeasonDays array and $highSeasonDays array are not correct

 

I need a way to check the dates against array WITHIN the if statement.

 

The problem is a bit complicated to explain so I hope I gave enough and clear details.

 

I would really appreciate any suggestion/help/comments/anything any one can offer!

Link to comment
https://forums.phpfreaks.com/topic/178101-if-statement-and-array-values/
Share on other sites

I was thinking of a different method to achieve the results (which also does not work  :shrug:) but I thought it might make the process a bit clearer and cleaner by using switch case function:

$numOfSeasons = count($highSeasonDates);

switch ($startDate){

	$ns = 0;
	while ($ns <= $numOfSeasons){

		case($startDate <  date("Y-m-d", strtotime($highSeasonDates[$ns]["startDate"])) && $startDate >  date("Y-m-d", strtotime($highSeasonDates[$ns]["endDate"]))):
		$lowSeasonDays[$LS++]["Month"] = date("m", strtotime($startDate));
		echo "<font color=\"#00CC33\">Low Season</font>";
		$ns++;
		break;
	}

	$ns = 0;
	while ($ns <= $numOfSeasons){

		case($startDate >=  date("Y-m-d", strtotime($highSeasonDates[$ns]["startDate"])) && $startDate <=  date("Y-m-d", strtotime($highSeasonDates[$ns]["endDate"]))):
		$highSeasonDays[$LS++]["Month"] = date("m", strtotime($startDate));
		echo "<font color=\"#FF0000\">High Season</font>";
		$ns++;
		break;
	}
}

 

I must admit that I am a noob at the switch case

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.