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!
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?
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.
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.
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.