[solved] multi dimensional array question

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

Post Reply
ghost007
Forum Commoner
Posts: 49
Joined: Sat Nov 22, 2003 10:10 am

[solved] multi dimensional array question

Post by ghost007 »

hi,

well I'm trying to hack som free script that I found on the internet but i can't get it to work.

For the moment I managed to get this output:

HTML OUTPUT:
errorsArray
(
[0] => Array
(
[0] => username
[1] => test
[2] => 14
)

)
==end of output

=> username = the field that triggered the error
=> test = some free param I pass along to use later in my coding
=> 14 = the number of the error in an external file that contains all my error msg.

THE PROB: how can I uset the 14 to get my error msg from my external file. if I already managed to load the file in my script.

here are the functions I used:
function load_messages = to include the external file that contains the $error = array (.. error msgs.
function _errors allows me to add errors info if a form validation check fails.
function get_errors_message gives the multi dimentional array with all info about the errors that occured.

Code: Select all

<?php
function load_messages()
{
	$file = "error_msg.php";
	if(file_exists($file))
	{
		include_once ($file);
		foreach ($error as $key => $value)
		{
			$this->_messages[$key] = $value;
		}
		
		clearstatcache();
		if (!isset($this->_messages[999999])) 
		{
			return false;
		}
		return true;
	}else{
		clearstatcache();
		return false;
	}
}

function _errors($_field, $_data, $_error) 
{
	if (!empty($_field))
	{
		$this->_errors[] = array($_field,$_data,$_error);
	}else{
		$this->_errors[] = array($_data,$_error);
	}
}
function get_errors_message()
{
	if($this->load_messages())
	{
		$count = count ($this->_errors);
		for ($x=0; $x<$count; $x++)
		{
			if (@$this->_messages[$this->_errors[$x][1]])
			{
				$this->_errors[$x][1] = $this->_messages[$this->_errors[$x][1]];
			}else{
				$this->_errors[$x][1] = $this->_messages[999999];
			}
		}
		return $this->_errors;
	}else{ // failed to load messages, at some point return something else...?
		return $this->_errors;
	}
}
?>
I'm stuck after get_errors_message to match the error code with the values of the array error.

thx for any help on this :?

siech
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

The firs thing I looked at was how you are getting the information from the file. If I assume that your error message file looks (is formatted) something like this...

14|Failed Database Connection
15|Girlfriend is Evil
16|Dog failed to defecate in yard
17|Query has a syntax error

... then the below function should put it into an array. After that it's a trivial issue to cross the arrays to get the error message you want.

Code: Select all

<?php
function load_messages($file) 
	{ 
	$message_array=array();
   	if(file_exists($file)) 
   		{ 
   		$file_array=file($file);
      	foreach($file_array as $line) 
      		{ 
      		$split=explode('|', $line);
      		$message_array[]=$split[0];
      		$message_array[$split[0]]=$split[1];
      		}
      	return $message_array;	
      	}
	return false; /* It seems the file doesn't exist */      		 
    } 
?>
Now doing this should get your error message.

Code: Select all

<?php
$message_array=&load_messages('message_file.txt');
$your_error=$message_array[$errorsArray[2]];
?>
You could do this as a multi-dim array, but there is no need. Besides, the complexity level will increase dramatically. Not worth it in this case.

You will note that I put the file information in the function declaration. This is better becuase you may want to change your error message file or experiment with more than one. You could also put the seperator in the function declaration too. That would make the function even more flexible.

Anyway, there is a ton of conjecture here as I don't know what your error message file looks like. It just seemed like there was a lot of drama in your example to do such a simple (to me at least) thing.

Cheers,
BDKR
ghost007
Forum Commoner
Posts: 49
Joined: Sat Nov 22, 2003 10:10 am

Post by ghost007 »

ok thx for your answer I think I have to learn some more about how using arrays.:roll:

thx
siech
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

It's all good. You'll get there.

Cheers,
BDKR
Post Reply