Page 2 of 2

Posted: Fri Jun 06, 2003 9:25 am
by mikusan
FULL CODE

Code: Select all

function MK_header()
{
	global $CFG;
	$change = array(
	'{main}' => 'Home',
	'{main_val}' => $CFG['site_url'],
	'{portfolio}' => 'Portfolio',
	'{portfolio_val}' => $CFG['site_url'] . 'portfolio.php',
	'{page_title}' => $CFG['site_name'],
	'{whatnot}' => 'Testing');
	return(Parse($change,'header'));
}

Code: Select all

function Parse($parse, $template)
{
	global $CFG;
	$array = file($CFG['path_layout'] . $template . '.thtml');
	$mkstring = implode ('', $array);

	while (list($k,$v) = each($parse))
		{
			$mkstring = str_replace($k, $v, $mkstring);
		}
	return($mkstring);
}
This is the important stuff i have about 1000 lines of code in just this file so i don't think it's apporpriate to post all of them... 8O
Latest php release...

Posted: Fri Jun 06, 2003 9:35 am
by nielsene
I just ran that code, with full error reporting and I do not get any errors/warnings (after creating a fake template file).

Two questions:
1) What version of PHP are you using?
2) What does the following code output when you run it?

Code: Select all

function Parse($parse, $template)
{
   global $CFG;
   $array = file($CFG['path_layout'] . $template . '.thtml');
   $mkstring = implode ('', $array);
   echo print_r($parse);  //  <-------------- NEW LINE
   while (list($k,$v) = each($parse))
      {
         $mkstring = str_replace($k, $v, $mkstring);
      }
   return($mkstring);
}

Re: Warning, poor coding...

Posted: Fri Jun 06, 2003 9:42 am
by Gleeb
mikusan wrote:

Code: Select all

while (list($k,$v) = each($change))
		{
			$mkstring = str_replace($k, $v, $mkstring);
		}
foreach()

Code: Select all

foreach($change as $k => $v)
{
   $mkstring = str_replace($k, $v, $mkstring);
}
Reads a bit easier to me, and I've got it working more often that the method you describe.

Posted: Fri Jun 06, 2003 9:50 am
by nielsene
But foreach or while .. each are basically two forms of the same thing. Both will gives errors if the array isn't an array, which is the problem at hand. We need to figure out why mikusan's arrays are getting messed up, first. Then we can debate pros/cons of array iterators.....

Posted: Fri Jun 06, 2003 9:59 am
by Gleeb
Sorry, got distracted ;) I see no reason for it to play with our heads. It works here... :|

Posted: Fri Jun 06, 2003 10:02 am
by nielsene
For what its worth, I like foreach too :), but it wouldn't (shouldn't) fix the problem....

Posted: Fri Jun 06, 2003 10:44 am
by mikusan
Okay, I have recently changed webserver, the phpinfo() on the new one (The one i keep getting the error) is 4.2.2
4.1.2 was the version on the other webserver, but there error displaying was not displayed, and heck i wasn't concerned with warnings per se. Now i have to, and it's also a form of poor coding in some sense... i cannot comprehend why it's busted

Posted: Fri Jun 06, 2003 10:46 am
by Gleeb

Code: Select all

print_r($change);
That'll tell you the contents of the array, do it and show us please, it'll help :)

Posted: Fri Jun 06, 2003 10:47 am
by nielsene
OK, I'm on a highly patched 4.1.2. Anyone here on 4.2.2 and can test the snippets above?

mikusan: what does a var_dump or a print_r display, as asked above?

Posted: Fri Jun 06, 2003 10:50 am
by mikusan
Definitely:

Array ( [{main}] => Home [{main_val}] => http://www.mywebsite.com/ [{portfolio}] => Portfolio [{portfolio_val}] => http://www.mywebsite.com/portfolio.php [{page_title}] =>My Website Name [{whatnot}] => Testing )

Posted: Fri Jun 06, 2003 10:53 am
by nielsene
Just to double check:
is that the output of print_r($change) inside MK_header() or the output of print_r($parse) in Parse()?

Posted: Fri Jun 06, 2003 6:33 pm
by mikusan
It's the same thing... i still get the error...

Posted: Fri Jun 06, 2003 6:50 pm
by mikusan
I DID IT I DID IT!!! I FIXED THIS BUGGER!!!!!!!!!

The code i showed you was one of many calls to the parser, however one of the calls that I foresaw was one that sent a blank $change to the parser (because there is nothing to replace)

so my function looked like this:

Code: Select all

function MK_footer()
{
	return(Parse($change,'footer'));
}
But this was the correct form that REALLY caught me...

Code: Select all

function MK_footer()
{
	$change = array();
	return(Parse($change,'footer'));
}
To conclude, for anyone interested to write their own parser to parse template files (which i think is a really good methid) then you should take a look at the tremendous mistake i did. I think it's not easy to spot that i was creating a string all the time, and it behaved very much like an array that is no question. Now i can go on and make the script better...

THANKS TO ALL OF YOU who have tried and helped to point me in the right direction....boy this one caught me up....