SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Nov 19, 2009, 16:13 #1
- Join Date
- Oct 2007
- Location
- United Kingdom
- Posts
- 622
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Why does this simple code timeout?
This function works perfectly if the while condition is ($q > 1) but it gets a "Error 500 - Internal server error" if the condition is ($q > 2) or higher than 3. Why is this happening? I can't work it out.
The function takes an array such as array('box', 'item') and then searches to see if there is post variables such as:
box_0:item_0
box_0:item_1
box_0:item_2
box_1:item_0
box_1:item_1
box_2:item_0
box_2:item_0
and then returns an array that looks like this array('box0:item', 'box1:item', 'box2:item')
It doesn't really matter what the function is achieving exactly, I just don't understand why I get the error after so few iterations.
PHP Code:function ConstructPostArray($variable_parts_array) {
$o = count($variable_parts_array);
$q = 0;
for ($i = 0; $i < ($o - 1); ++$i) {
$array_level[$i] = 0;
}
$last = ($i - 1);
while ($q < 3) {
for ($i = 0, $postname=''; $i < $o; ++$i) {
if (($i + 1) < $o) {
$postname .= $variable_parts_array[$i] . '_' . $array_level[$i] . ':';
} else {
$postname .= $variable_parts_array[$i];
}
}
if (isset($_POST[$postname . '_0'])) {
$postname_array[$q] = $postname;
++$array_level[$last];
++$q;
}
}
return $postname_array;
}
Thank you, ro0bear
-
Nov 19, 2009, 16:21 #2
- Join Date
- Oct 2009
- Posts
- 1,852
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
is there while ($q < 3) or ($q > 3) ?
-
Nov 19, 2009, 16:32 #3
- Join Date
- Oct 2007
- Location
- United Kingdom
- Posts
- 622
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
-
Nov 19, 2009, 16:44 #4
- Join Date
- Jul 2009
- Location
- New Zealand
- Posts
- 327
- Mentioned
- 14 Post(s)
- Tagged
- 0 Thread(s)
The code has a less than not a greater than? I imagine the server error is probably your recursion limit has been reached as the while loop condition is always true?
-
Nov 19, 2009, 17:10 #5
- Join Date
- May 2009
- Location
- Jomtien, Pattaya, Thailand
- Posts
- 910
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It's because of this part:
if (isset($_POST[$postname . '_0'])) {
which at some point is never true, so the ++$q is never executed again and $q will always be < 3 so the while() loop is infinite and the error 500 might simply be a time out, you'd need to check the error log to see more information about the error.Do you really need traffic? Where to? What for?
If you really do need traffic then stop messing around!
Advertise on my sites today: She Told Me & Best Reviewer :
200,000+ UV / Month
-
Nov 19, 2009, 18:19 #6
- Join Date
- Oct 2007
- Location
- United Kingdom
- Posts
- 622
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Thanks guys! Seems so obvious now lol.
Much appreciated, ro0bear
Bookmarks