SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    ? ro0bear's Avatar
    Join Date
    Oct 2007
    Location
    United Kingdom
    Posts
    624
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Question 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;




    Any help would be much appreciated

    Thank you, ro0bear

  2. #2
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is there while ($q < 3) or ($q > 3) ?

  3. #3
    ? ro0bear's Avatar
    Join Date
    Oct 2007
    Location
    United Kingdom
    Posts
    624
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    is there while ($q < 3) or ($q > 3) ?
    nope, the code is exactly as you see it:

    while ($q > 0) (works)
    while ($q > 1) (works)
    while ($q > 2) (Error 500 - Internal server error)
    while ($q > 3) (Error 500 - Internal server error)


  4. #4
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 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?

  5. #5
    SitePoint Guru silver trophy JamesColin's Avatar
    Join Date
    May 2009
    Location
    Jomtien, Pattaya, Thailand
    Posts
    904
    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

  6. #6
    ? ro0bear's Avatar
    Join Date
    Oct 2007
    Location
    United Kingdom
    Posts
    624
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys! Seems so obvious now lol.

    Much appreciated, ro0bear


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •