Page 1 of 1

Get array value from other function

Posted: Wed Mar 09, 2005 6:44 pm
by husniteja
I have two function which the first function for read file and the second for view

Code: Select all

function readFile(){
	$fd = fopen("guest.dat","r");
	$i=0;
	while(!feof($fd)){
	      $i++;
	      $name[] = trim(fgets($fd),255);
	      $comment[] = trim(fgets($fd),255);
	      $space[] = trim(fgets($fd),255);
	}
	fclose($fd);
}

function view(){
	global $HTTP_GET_VARS;
	$pageLength = 5;
	$this->readFile();

	if(isset($HTTP_GET_VARS["start"]) && is_numeric($HTTP_GET_VARS["start"])){
	$mulai = $HTTP_GET_VARS["start"];
			}else{
				$start = 0;
			}

	for($i=$start; $i<$start+$pageLength&& $i<$this->readFile();$i++){
		//Want to print the array from readFile

	          }
}
I want to print the value of array from readFile(), i have tried with the same function and it's no problem, in the same function i just use the for statement to print name, coment and space.

But i want to separate my function, and i don't know how to call the name, comment and space in view function. I use

Code: Select all

echo $this->readFile().name[$i];
may be i am wrong because there no meaning for this statement


feyd | hey look ma,

Code: Select all

is on. [/color]

Posted: Wed Mar 09, 2005 6:56 pm
by feyd
remove line 14.

You never set the data built in readFile() to anywhere outside the function. $start is only set in one code path. You don't return anything from readFile().

Posted: Wed Mar 09, 2005 7:02 pm
by husniteja
hi feyd

Oke, may be my code is not complete, i still use the global and $start.

And i have tried to get the return value from readFile(), i use return $i (not show in the last code). but since i just return the $i, how to call the name, comment and space ?