Function dis-function

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
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Function dis-function

Post 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>";    
}
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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?
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post by facets »

thanks Jay, but isn't that what I am doing?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Function dis-function

Post 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.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

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