Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: ziycon on May 18, 2012, 07:24:03 AM

Title: PHP While Loop!?
Post by: ziycon on May 18, 2012, 07:24:03 AM
Maybe its due to me looking at the code so long I just can't see whats going on but can anyone tell me why the below code is timing out?


$level = 1;

while($level >= 0) {
$breadcrumbNav = $linkArray[$level][1].$breadcrumbNav;
        // $linkArray[$level][5] will always be less then the current value for $level but not always a concurrent decrement
$level = $linkArray[$level][5];
}
Title: Re: PHP While Loop!?
Post by: Herman's Mixen on May 18, 2012, 07:40:25 AM
global variable is $linkarray is used before its defined
Title: Re: PHP While Loop!?
Post by: ziycon on May 18, 2012, 08:01:45 AM
Sorry, I should have said, everything is defined before hand, there is so much code I didn't post it all as I think its an issue with my logic in the while loop as if I change the below line:
$level = $linkArray[$level][5];

To:
echo $linkArray[$level][5];
$level--;


It outputs ok.
Title: Re: PHP While Loop!?
Post by: Herman's Mixen on May 18, 2012, 08:05:56 AM
 i debugged the code coz m lazy today... while ihave not full code cant make any sense out of it...the output is debugged oke anyway... got just 2 errors before the globals are defined before they are used :P

$breadcrumbNav

and

$linkArray
Title: Re: PHP While Loop!?
Post by: ziycon on May 18, 2012, 08:13:47 AM
Maybe this might make more sense, I cleaned it up to remove non relevant code.

$linkParent = array();
$currentPage =  $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

$linkArray = self::getNavArray(); // returns an array of all available links on the site in the format (id, link_text, link, sequence, pages_to_display_on, parent)

for($i=0;$i<sizeof($linkArray);$i++) {
if(strstr($currentPage,$linkArray[$i][2]))
$linkParent = $linkArray[$i][5];
}

$level = $linkParent;

while($level >= 0) {
$breadcrumbNav = $linkArray[$level][1].$breadcrumbNav;
$level = $linkArray[$level][5];
}
Title: Re: PHP While Loop!?
Post by: Herman's Mixen on May 18, 2012, 08:16:33 AM
now only $breadcrumbNav aint defined... but parsed good ;)

line 16 gives the error not used before defined anyway


$breadcrumbNav = $linkArray[$level][1].$breadcrumbNav;
Title: Re: PHP While Loop!?
Post by: ziycon on May 18, 2012, 08:19:24 AM
Sorry I'm having a forgetful day, it was one of the lines I removed, here is the proper code.


$breadcrumbNav = '';
$linkParent = array();
$currentPage =  $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

$linkArray = self::getNavArray(); // returns an array of all available links on the site in the format (id, link_text, link, sequence, pages_to_display_on, parent)

for($i=0;$i<sizeof($linkArray);$i++) {
if(strstr($currentPage,$linkArray[$i][2]))
$linkParent = $linkArray[$i][5];
}

$level = $linkParent;

while($level >= 0) {
$breadcrumbNav = $linkArray[$level][1].$breadcrumbNav;
$level = $linkArray[$level][5];
}


It seems that its timing out on the last line of the while loop: $level = $linkArray[$level][5];
Title: Re: PHP While Loop!?
Post by: Herman's Mixen on May 18, 2012, 08:25:44 AM
parsed ok without errors so it good ;)

no time out but check your configuratios apache/php/mysql can be horrible sometimes :P

what are you using for development ?
Title: Re: PHP While Loop!?
Post by: ziycon on May 18, 2012, 09:26:09 AM
Got it working, it was my logic with the while loop and the array. Thanks for your help. :)
Title: Re: PHP While Loop!?
Post by: Herman's Mixen on May 18, 2012, 09:36:54 AM
be welcome :P