Warning, poor coding...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post 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...
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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);
}
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Re: Warning, poor coding...

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.....
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

Sorry, got distracted ;) I see no reason for it to play with our heads. It works here... :|
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

For what its worth, I like foreach too :), but it wouldn't (shouldn't) fix the problem....
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post 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
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post 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 :)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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?
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post 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 )
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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()?
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

It's the same thing... i still get the error...
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post 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....
Post Reply