While loop making too many arrays
I need help, badly. What I am making right now is a date picker. Pretty much updating on it for my own use.
I needed to block out specific date ranges. I found a code to do this, and it utilizes arrays. I like that.
What I then needed was a way to create the array with every date within the range, because I only entered the Start Date and the End Date. Found one. Works like a charm.
But now, I have a problem. It creates a new array using the same $. So the only array that the calender registers is the newest one. Essentially, what I need is just 1 array.
I've tried a few things, but nothing seems to work. Been thinking about this for a while now. Any help?
function createDateRangeArray($strDateFrom,$strDateTo) //Changes a Range of Dates to Specific Dates
{
$aryRange = array(); //Creates an Array
$iDateFrom = mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo = mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo >= $iDateFrom)
{
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo)
{
$iDateFrom += 86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
}
}
return $aryRange; //Returns to step 1 and adds another value into the array
}
$d = "SELECT startdate, enddate FROM classdetails";
$f = mysql_query($d);
while ($e = mysql_fet开发者_Python百科ch_array($f))
{
while (list($g, $h) = each($e)) { $$g = $h; }
{
$aryDates = createDateRangeArray($startdate,$enddate);
print_r($aryDates);
echo "<br />";
}
}
And for those wondering, I do include references of where some of my work is taken from, even if heavily modified.
As you can see, the problem lies with the fact that once it creates an array, it just creates a new one. I've tried using ifelse statements, empty(), isset(), increments (didn't even know how to use them, just thought a long time and deleted it).
So, what can I do here?
What I always get back is (I only have 2 rows of dummy data):
Array ( [0] => 2010-12-16 [1] => 2010-12-17 [2] => 2010-12-18 [3] => 2010-12-19 [4] => 2010-12-20 [5] => 2010-12-21 [6] => 2010-12-22 [7] => 2010-12-23 )
Array ( [0] => 2010-12-25 [1] => 2010-12-26 [2] => 2010-12-27 [3] => 2010-12-28 [4] => 2010-12-29 )
The problem lies with the loop itself. The first instance does what it is tole to do. The second instance onwards, $aryDates just gets replaced.
Any help would be greatly appreciated.
while (list($g, $h) = each($e)) { $$g = $h; }
Oh good god. Do NOT use variable variables. Especially when you've got a perfectly good array being generated by the mysql fetch call as is. THis is why you're having trouble debugging your code.
You could rewrite the while loop like this
while ($e = mysql_fetch_array($f)) {
$aryDates = createDateRangeArray($e['startdate'],$e['enddate']);
print_r($aryDates);
echo "<br />";
}
Far more readable, far easier to follow what's happening, and absolutely NO NEED WHATSOEVER for variable variables.
You penance is 500 lashes with a wet noodle while repeating the following mantra: "I shall not use variable variables"
Beyond that, of course it's creating a new array each time, you're telling it to at the top of your createDateRange function:
$aryRange = array(); //Creates an Array
If you want to reuse the same array over multiple function calls, make it a static variable:
static $aryRange = array();
which will preserve it across successive calls, which would then let you append each calls's dates.
精彩评论