好吧,我不明白为什么这不起作用,也许你们中的一个先生可以帮助我。
$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;
}
}
函数内部的var_dump按预期工作。返回的内容只返回任何内容,即使我将其设置为原始文本也是如此。我知道这可能是一件容易的事情,但我现在已经筋疲力尽了。
试试这个版本:
<?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);
您需要通过引用传递 $upline
参数,或者从任一选项返回结果 - 无论是否有结果。我会选择返回每个结果选项,因为这意味着在调用函数之前不需要初始化结果数组。这种方法的缺点是它会占用更多的内存,因此您可以改用此版本:
<?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);
我知道这不是
问题,但是,也许您应该阅读有关获取"分层数据"并使用它的内容。你现在的做法非常...Mh让我说,"慢"。
- 关于它的文章在网站点
- 维基百科上的树遍历