Reputation: 1
Ok, I'm not seeing why this doesn't work, perhaps one of you gent's can help me out.
$new_upline = RetrieveUpline($root_referral_id, array());
echo ("The upline after return: <BR>");
var_dump ($new_upline);
function RetrieveUpline($root_ref_id, $upline){
$referrer = mysql_query("SELECT id, username, referral_ID, total_points_earned, isbanned FROM members WHERE id = ".$root_ref_id);
$rows = mysql_num_rows($referrer);
$upline_size = count($upline);
if ($rows>0){
while($feed = mysql_fetch_array($referrer, MYSQL_ASSOC)){
$upline[$upline_size] = $feed;
RetrieveUpline($upline[$upline_size]['referral_ID'], $upline);
}
}else{
echo ("The upline before return: <BR>");
var_dump($upline);
return $upline;
}
}
The var_dump inside the function works as expected. The return just returns nothing, even if I set it to raw text. I know it's probably something easy, but I'm burnt out on is right now.
Upvotes: 0
Views: 1240
Reputation: 21
I know it was not the question but, maybe you should read something about getting "Hierarchical Data" and working with it. The way you do it now is very ... mh let me say, "slow".
Upvotes: 0
Reputation: 88647
Try this version:
<?php
function RetrieveUpline($root_ref_id, $upline = array()) {
// Sanitize input
$root_ref_id = (int) $root_ref_id;
// Do query
$query = "
SELECT id, username, referral_ID, total_points_earned, isbanned
FROM members
WHERE id = $root_ref_id
";
$result = mysql_query($query); // What if this query fails? Add error handling here...
// Loop results
while ($feed = mysql_fetch_assoc($result)) {
$upline[] = $feed;
$upline = RetrieveUpline($feed['referral_ID'], $upline);
}
// Free mysql result resource
mysql_free_result($result);
// Return new array
return $upline;
}
$new_upline = RetrieveUpline($root_referral_id);
var_dump($new_upline);
You need to either pass the $upline
argument by reference, or return the result from either option - whether there are results or not. I would go for the returning every result option, as it means you don't need to initialise the result array before calling the function. The disadvantage to this approach is that it will be considerably more memory hungry, so you could use this version instead:
<?php
function RetrieveUpline($root_ref_id, &$upline) {
// Make sure $upline is an array (for first iteration)
if (!is_array($upline)) $upline = array();
// Sanitize input
$root_ref_id = (int) $root_ref_id;
// Do query
$query = "
SELECT id, username, referral_ID, total_points_earned, isbanned
FROM members
WHERE id = $root_ref_id
";
$result = mysql_query($query); // What if this query fails? Add error handling here...
// Loop results
while ($feed = mysql_fetch_assoc($result)) {
$upline[] = $feed;
RetrieveUpline($feed['referral_ID'], $upline);
}
// Free mysql result resource
mysql_free_result($result);
}
RetrieveUpline($root_referral_id, $new_upline);
var_dump($new_upline);
Upvotes: 1