php mysql whlie loop

Newbie in the house. Let me quickly tell you what I am trying to attempt and then hopefully you can help me clear this up. It may be a logic issue, I simply don’t know as there is no error. It just does not work.

I have a database full of users. Each user is referred by someone else. Members have the ability to become vip. If the member above you is not a vip, this script is meant to “walk” the records until it finds the first vip in the chain of users that are directly linked to you. Entity relationship if you will. :slight_smile:

I am passing in the referralid of the user that referred the user that is logged in. The script will pull the user record that belongs to the person who referred the logged in user. If the referrer is not a vip, it will then identify who referred the referrer and check for vip status and so on and so forth. the referral_id will always be the username of the person that referred you.

[php]function getVIP($referralid=NULL)
{
global $db,$db_table_prefix;
$vip = 0;

while ($vip = 0)
{
if($referralid!=NULL)
{
$sql = “SELECT * FROM “.$db_table_prefix.“Users
WHERE
username = '”.$db->sql_escape(sanitize($referralid)).”’
LIMIT
1”;
}

$result = $db->sql_query($sql);

$row = $db->sql_fetchrow($result);

if($row[“vip”] == 0)
{
$referralid = $row[“referral_id”];
$vip = $row[“vip”];
}
else
{
$vip = $row[“vip”];
}

}

return ($row);
}
[/php]

I’m also new but i’ll try and help while I wait for some one to help me. I’m guessing you know how to connect to the a SQL database and you’ve done that before the function? If so try doing it in the function also/instead make sure you close other connection also. And you might be less ‘Noobish’ than me but this:
[php]
$sql = “SELECT * FROM “.$db_table_prefix.“Users
WHERE
username = '”.$db->sql_escape(sanitize($referralid)).”’
LIMIT
1”;[/php]
I don’t understand maybe its more advanced than me but i would use this:
[php]
$sql = mysql_query("SELECT ‘USER’ , USER_REFID, ‘VIP’ FROM USERS WHERE ‘USER’ =$USERAND USER_REFIDIS NOT NULL LIMIT 0, 1 ");
[/php]

I think that would get you the referalID from that user then just loop this changing the $USER to the referal user and repeat until VIP is set

That might have been useless but hey I tried :slight_smile:

Yes, something like that. I would do it in this format. (Just general terms…

Query and pull the original ID and ReferrerID
ref=ReferrerID
vip=0
while vip=0
Query ReferrerID pulling THEIR ReferrerID
if new ReferrerID =vip {
set vip=1
}else{
ReferrerID = THEIR ReferrerID
}

So, basically, set a variable for the current referrer ID and set VIP to zero.
Loop until VIP is set to 1.
During each look run a new query using the current referrer ID pulling their referrer.
check if the new-current referrer is a vip.
if so, set vip to one and that will cause the while to exit out
if not, set the current referrer to the new one just pulled from the query
loop back and do it again…

Once out of the loop, VIP=1 and current referrer is pointing to an ID that is the VIP.
(You might want to also pull the name or whatever else you need in that query so when the loop
is done, you do not have to requery to get the rest of the info needed.)

Hope that outlines it good for you. Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service