Jump to content

Recommended Posts

My php file is crapping itself whenever I've got this uncommented.

 

I've played with commenting various pieces, and it's specifically the switch statement.  I also tried removing all commands from the switch comparisons, and I think it's the switch statement itself, not the resulting commands on a positive comparison.

 

Help as to why would be lovely thanks.

 

function findColumnsAndRows()
{
$fh = file($systemFile);

foreach ($fh as $line)
{
	$temp = explode(" ", $line);
	if ($temp[0] == "columns") echo "FOUND COLUMNS <br />\n";
	switch ($temp[0])
	{
		case "columns"
			$columns = (int)$temp[2];
			break
		case "rows"
			$rows = (int)$temp[2];
			break
		case default
			break
	}
}
}

I'm not sure where I've not described the issue...

 

I don't have that switch statement commented, and the page doesn't load anything, completely white.

 

I've also already given any debugging information I have.  The switch statement itself is causing the php to fail.

 

The problem is: There seems to be an issue with the switch statement.

Thanks.  I'm a noob with php, so stupid stuff, while embarassing, doesn't surprise me that it's happening.

 

I've made those changes, and tested, and still nothing.

 

Any ideas?  I can post the full file if you like? (It's only 50-odd lines atm)

From the looks of it you could use variable variables here (although it's probably a crappy way). That way you don't need the switch.

 

${$temp[0]} = $temp[2];

 

That will make a variable (either $columns or $rows, depending on what $temp[0] equals) and store $temp[2] in it.

You could do it with arrays as well.

 

Perhaps posting the full code will give us a better understanding of what it is you're trying to achieve.

Ok, here's the full code.

 

Basically what I'm trying to do is:

[*]Open systemFile

[*]Loop through the whole file, one line at a time

[*]Explode each line using space as the delimiter

[*]test the first item on each line ($temp[0]) to see if it's the columns or rows setting

[*]If it's rows or columns, set the appropriate variable to the value contained in $temp[2]

 

See below the main body of code for an example of the text found in $systemFile



<?php
#-------------------------------------------------------------------------------VARIABLES----------------------------------------------------------------------------
#Path to the Mugen installation (NO SLASH AT THE END)
$mugenPath = 'E:\GAMES\MUGEN';

#Path to the various data files inside Mugen
$selectFile = $mugenPath . '\DATA\select.def';
$systemFile = $mugenPath . '\DATA\system.def';

#find the columns and rows
$columns = 0;
$rows = 0;
#-------------------------------------------------------------------------------END VARIABLES------------------------------------------------------------------------

#-------------------------------------------------------------------------------FUNCTIONS----------------------------------------------------------------------------
function findColumnsAndRows()
{
$fh = file($systemFile);

foreach ($fh as $line)
{
$temp = explode(" ", $line);
if ($temp[0] == "columns") echo "FOUND COLUMNS <br />\n";
switch ($temp[0])
{
case "columns":
$columns = (int)$temp[2];
break;
case "rows":
$rows = (int)$temp[2];
break;
}
}
}

#---------------------------------------------------------------------------END FUNCTIONS----------------------------------------------------------------------------

if ($columns == 0 || $rows == 0)
{
findColumnsAndRows();
}

echo "columns" . $columns . "\n";
echo "rows" . $rows . "\n";

$lines = file($selectFile);
foreach ($lines as $line_num => $line)
{
print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n";
}
?>

;-------------------------------------------------------------------

;Character select definition

[select Info]

rows = 400

columns = 22

wrapping = 0           

pos = 6,1             

showemptyboxes = 0       

moveoveremptyboxes = 0 

cell.size = 14,14     

cell.spacing = 0

 

Does that help a bit more?

Some problems:

 

1) Your function has no idea what any of the variables outside of its scope are. To it, $systemFile is an undefined variable. So that means that your file() call shouldn't be doing anything. To fix this, you can pass $systemFile as a parameter to the function.

 

2) Your function doesn't return anything. Again your getting confused with the scope. Setting $rows and $columns inside the function is making those variables only available to the function. They are not the same as the variables outside. So you need to return both $rows and $columns inside the function as an array so that you can access the values

 

If you need help fixing them, then let us no.

Thanks for the info, makes sense, for some reason I thought I'd seen that php didn't care about scope... <forehead slap>

 

Anyway, I've had a stab at making those changes, and it's now actually processing the rest of the file, but not finding the columns or rows.

 

As you've seen in the sample I gave you, they are actually in there.  I've got that "FOUND COLUMNS" line in there just to see if it's finding it, and for some reason it's not.

 

Any ideas?

 

 

<?php
#-------------------------------------------------------------------------------VARIABLES----------------------------------------------------------------------------
#Path to the Mugen installation (NO SLASH AT THE END)
$mugenPath = 'E:\GAMES\MUGEN';

#Path to the various data files inside Mugen
$selectFile = $mugenPath . '\DATA\select.def';
$systemFile = $mugenPath . '\DATA\system.def';

#find the columns and rows
$columns = 0;
$rows = 0;
#-------------------------------------------------------------------------------END VARIABLES------------------------------------------------------------------------

#-------------------------------------------------------------------------------FUNCTIONS----------------------------------------------------------------------------
function findColumnsAndRows($systemFile)
{
$fh = file($systemFile);

foreach ($fh as $line)
{
	$temp = explode(" ", $line);
	if ($temp[0] == "columns") echo "FOUND COLUMNS <br />\n";
	switch ($temp[0])
	{
		case "columns":
			$columns = (int)$temp[2];
			break;
		case "rows":
			$rows = (int)$temp[2];
			break;
	}

}

return array ($columns, $rows);
}

#---------------------------------------------------------------------------END FUNCTIONS----------------------------------------------------------------------------

if ($columns == 0 || $rows == 0)
{
list($columns, $rows) = findColumnsAndRows();
}

echo "columns" . $columns . "\n";
echo "rows" . $rows . "\n";

$lines = file($selectFile); 
foreach ($lines as $line_num => $line) 
{
print "<font color=red>Line #{$line_num}</font> : " . $line . "<br />\n"; 
}
?>

You're not passing the $systemFile array to the function. What about turning errors on? Makes debugging so easier:

 

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
?>

 

Added $systemFile as function parameter:

 

if ($columns == 0 || $rows == 0)
{
   list($columns, $rows) = findColumnsAndRows($systemFile);
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.