I'm hoping that another set of eyes will see something that I'm not.

I'm having an issue with a script that I just added a little routine to, so I know the issue is in the routine, because the sript as a whole was working before adding this...

the routine separated into a stand alone script gives me the same results (runs off in space and never stops processing..)

<?php
include "all_inc/config_site.php";
include "all_inc/pghead.php";
$links='y'; // indicates that menu should be displayed
include "all_inc/pgtop.php";

    $ref_id='10000011';// set to a known good member record as initial referrer
    $ref_stat='';
    $cur_ref=$ref_id;
    while($ref_stat!='D'){
      $sql_stat = "
        SELECT mem_status, ref_id
        FROM members
        WHERE mem_id = '$cur_ref'
      ";
      $stat_result=mysql_query($sql_stat);
      $stat_row=mysql_fetch_array($stat_result);
      $ref_stat=$stat_row[0]; // holds the mem_status of cur_ref
      if ($ref_stat !='D'){ // not an IR, so set cur_ref to next upline referrer
        $cur_ref=$stat_row[1];
      } //  if cur_stat is a D then it will drop out of while loop
    }
    print"<br>When dropping out of while loop:<br>cur_ref".$cur_ref;

    include "all_inc/pgbottom.php" ;
?>

Abbreviated version of members table:

mem_id mem_status ref_id

10000005 D 10000002
10000011 F 10000005

The way it should work is that the initial ref_id is 10000011
and when the query is run it gets the mem_status of that member along with the ref_id in their record, which is their referrer
In this case 10000005 referred 10000011
So the first returned result should be ref_stat is 'F'
and since it is not 'D' then it should reassign cur_ref to with the value of 10000005

Then go to the next iteration of the while loop.
and when the query is run it gets the mem_status of that member along with the ref_id in their record, which is their referrer
In the second iteration 10000002 referred 10000005
So the first returned result would set the ref_stat to 'D'
And since it is 'D' then it doesn't need to reset the cur_ref, and it should drop out of the while loop

and simply print what the cur_ref value is

Should be super simple, but for some reason, it never stops processing, and I can't get it to display anything that would give me a clue as to what is going wrong...

Any suggestions would be greatly appreciated.

Thanks
Douglas

Member Avatar for diafol

It sounds as though you've over-egged it. Sounds complicated as hell, although I assume what you're trying to do is quite straightforward.

So you want to follow a thread back to its root? Denoted by 'D' ? Something like that?

Yes diafol, I guess that is one way to put it.. never thought of it quite like that.

it is basically following a line of sponsorship to find the first upline member that has become active (D), as opposed to being a free (F) member.

The intent was to gather the current mem_status of the first upline along with the mem_id of their referrer at the same time, in case I needed to continue moving up to find the first D. Then each time I moved up one level, do the same. Seems to me that it is a pretty simple concept, and I'm still baffled as to why that code didn't do the job.

But, to be honest, I have completely rewritten it coming from a different angle and made it work, doing the same thing only in a different way.

Basically I just broke it into 2 separate queries. One to get the ref_id and the second to check the mem_status of that member (ref_id).

<?php
include "all_inc/config_site.php";
include "all_inc/pghead.php";
$links='y'; // indicates that menu should be displayed
include "all_inc/pgtop.php";

    $mem_id='10000013';// set to the id of the upgrading member
    $ref_stat='';
    $cur_mem=$mem_id;
   while ($ref_stat != 'D'){ // iterate upline by line of sponsorship to next
                          // Active IR to pay fsb to
      $sql_ref = "SELECT ref_id FROM members WHERE mem_id=$cur_mem";
      $res_ref=mysql_query($sql_ref);
      $row_ref=mysql_fetch_array($res_ref);
      $check_up=$row_ref[0]; // get next upline ref_id starting with members ref
      if ($check_up<$default_id){
        break;  // breaks out of while ($ref_stat != 'D')
      }
      $sqlpay = "SELECT mem_status FROM members WHERE mem_id = '$check_up' LIMIT 1";
      $respay=mysql_query($sqlpay);
      $rowpay=mysql_fetch_array($respay);
      $ref_stat=$rowpay[0]; // capture uplines mem_status checking for 'D'
        // when upline ref is 'D' will drop out of loop
      if($ref_stat != 'D'){$cur_mem=$check_up;}//if not D - set next search
    } //  when it drops out $check_up will hold mem_id to pay a fsb to
    print"<br>When dropping out of while loop:<br>check_up is ".$check_up;

    include "all_inc/pgbottom.php" ;
?>

The results are exactly as expected, giving me the first upline member that has a mem_status of D.

And here is the processing output I sent to the screen to see what the processing looked like.

sql_ref is SELECT ref_id FROM members WHERE mem_id=10000013
line15 - check_up is : 10000011
sqlpay is SELECT mem_status FROM members WHERE mem_id = '10000011' LIMIT 1
line22 - ref_stat is : F
sql_ref is SELECT ref_id FROM members WHERE mem_id=10000011
line15 - check_up is : 10000005
sqlpay is SELECT mem_status FROM members WHERE mem_id = '10000005' LIMIT 1
line22 - ref_stat is : D
line26 - When dropping out of while loop:
check_up is 10000005

I just don't understand why that can't be done in the single query as opposed to 2 separate ones.

But, either way, I got it working. Would still like to consolidate.

Thanks for your response
Douglas

Member Avatar for diafol

Perhaps if you had an extra field in your table as "first_id" so when you add new referred members, they take on the "first_id" of the referrer (or id if the first_id of the referrer is NULL/0)

commented: Consistently there with helpful responses and suggestions. Thanks +2

that is what the ref_id is in each members record...
mem_id is the members id
ref_id is the mem_id from their referrers record.

it basically creates a chain through the line of sponsorship/referral

which is why I thought if I was getting the mem_status of the person that referred you, I could also get their ref_id at the same time, which would point to the member that referred them. Then I could easily walk up the chain one referrer at a time, until finding the first one with the mem_status I was looking for.

I don't know why it didn't work, but I know that there has to be a simple explanation. Once we get launched (this week), and I have time to breath, I'll revisit that query, and take the time to determine why it didn't work..

Thanks again for your ongoing assistance with all of my questions over time.

Douglas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.