Page 1 of 1

Function dis-function

Posted: Fri Apr 20, 2007 4:00 am
by facets
Hiya,
I am having trouble accessing my array from within another function.
getDebtTop5() does not seem to able to access the array.
Can anyone see my error?

The output I see is just 'NULL'.

tia, will.

Code: Select all

function getAllShops(){

	global $linkID, $clientsID , $companyName, $shopArray;
	
	$clientQuery = 'select id, company_name from clients';
	$clientResult = mysql_query($clientQuery, $linkID) or die("Data not found.");
	      
	for($x = 0 ; $x < mysql_num_rows($clientResult) ; $x++){
 		$row = mysql_fetch_assoc($clientResult);
 		$shopArray[] = array($row['id'] => $row['company_name']);
	}    
	return $shopArray;
}

function getTotals() {
    
	global $linkID, $clientResult, $shopArray;
	
	getAllShops($shopArray);
    
	foreach ($shopArray as $outer_key => $single_array) {     	   
		foreach ($single_array as $inner_key => $value) {  
       		$debtQuery = "select sum(total) as total from statements where client_id = '$inner_key'";
       		$debttResult = mysql_query($debtQuery, $linkID) or die("Data not found.");
			for($x = 0 ; $x < mysql_num_rows($debttResult) ; $x++){
				$row = mysql_fetch_assoc($debttResult);
   			}
		}    
		$getTotalsArray[] = array($inner_key, $row['total']);    
   }
 	return $getTotalsArray;
}

function getDebtTop5() {
	
	global $getTotalsArray;
	
	getTotals($getTotalsArray); 
	
		echo "<hr><pre>";
		var_dump($getTotalsArray);
		echo "</pre>";    
}

Posted: Fri Apr 20, 2007 5:14 am
by facets
If I var_dump($getTotalsArray) from within the getTotals function I can see the array contents. So I definately know the array is propergated.

Posted: Fri Apr 20, 2007 5:18 am
by jayshields
Globals are generally a bad idea, and I don't see why you are attempting to use them in this example.

getTotals() returns the array you want to access, so why not just call getTotals() from within getDebtTop5() and use the result in there?

Posted: Fri Apr 20, 2007 5:36 am
by facets
thanks Jay, but isn't that what I am doing?

Re: Function dis-function

Posted: Fri Apr 20, 2007 5:44 am
by jayshields
Sorry, I didn't read your code properly. Basically there are alot of things wrong with your code. The main one being that your functions aren't taking any arguments.

I've made some changes to your code for you.

Code: Select all

function getAllShops($linkID){
	
	$clientQuery = 'select id, company_name from clients';
	$clientResult = mysql_query($clientQuery, $linkID) or die("Data not found.");
	      
	for($x = 0 ; $x < mysql_num_rows($clientResult) ; $x++){
 		$row = mysql_fetch_assoc($clientResult);
 		$shopArray[] = array($row['id'] => $row['company_name']);
	}    
	return $shopArray;
}

function getTotals($shopArray) {
    
	foreach ($shopArray as $outer_key => $single_array) {     	   
		foreach ($single_array as $inner_key => $value) {  
       		$debtQuery = "select sum(total) as total from statements where client_id = '$inner_key'";
       		$debttResult = mysql_query($debtQuery, $linkID) or die("Data not found.");
			for($x = 0 ; $x < mysql_num_rows($debttResult) ; $x++){
				$row = mysql_fetch_assoc($debttResult);
   			}
		}    
		$getTotalsArray[] = array($inner_key, $row['total']);    
   }
 	return $getTotalsArray;
}

function getDebtTop5($getTotalsArray) {
	
		echo "<hr><pre>";
		var_dump($getTotalsArray);
		echo "</pre>";    
}

//call it using this
getDebtTop5(getTotals(getAllShops($linkID)));
That will get you off the ground, but it's still very sloppy and shouldn't be structured like this. I suggest you read some PHP examples of maybe a general programming book to get an idea of how to write functions and access variables.

Posted: Fri Apr 20, 2007 7:19 am
by facets
thanks Jay. Work a treat.
Yup. Still lots to learn. My copy of 'PHP 5 Object, Pattern and Practices' was delivered today so hopefully that may help me out.