Page 1 of 1
Finding the correct percent
Posted: Sat Jun 09, 2012 2:18 pm
by nite4000
Hey guys I have a problem I have a idea how to do it but with no luck
Let me give you an example
i have a table called referrals
with 3 records
1. earn 1 1 50 5%
2. earn 2 51 100 8%
3. earn3 101 500 10%
now what I need to do is once i have a value from my members table from field ref_total then I need to take that number and see which percent they would get
ex if its 6 then they would get 5% or if its 150 then they get 10%
and so on
so far i have it going good but I am stuff on how to get teh correct percentage. any help would be great.
Re: Finding the correct percent
Posted: Sat Jun 09, 2012 2:38 pm
by califdon
I wouldn't use a database to do a simple calculation like that unless the amounts are subject to frequent changes. You could accomplish that with a line or two of simple PHP code:
Code: Select all
$percentage = $earn > 100 ? 10 : 8;
$percentage = $earn > 50 ? 8 : 5;
Re: Finding the correct percent
Posted: Sat Jun 09, 2012 2:44 pm
by nite4000
the values could change for example someone could just do 1-20 then 21-50 - 2% , 5%
I am working with using the BETWEEN call but still no luck.
Re: Finding the correct percent
Posted: Sun Jun 10, 2012 12:52 pm
by califdon
nite4000 wrote:the values could change for example someone could just do 1-20 then 21-50 - 2% , 5%
I am working with using the BETWEEN call but still no luck.
As I showed you, it is extremely simple to do the calculations; what you should be concentrating on is how it will be used. Do you want to allow the user to input the criteria? Do you want to have a flexible application, so that they initially enter the criteria, but then don't change it again? These are the questions you must decide. I can't imagine any situation where it would be appropriate to use a database to store such criteria. You might need a separate web page to input the criteria to a .configuration file, for example.
Re: Finding the correct percent
Posted: Fri Jun 15, 2012 3:55 pm
by nite4000
I am going to try to explain in more detail
I will walk through all the steps then explain what its to do.
Step 1 the admin of the site goes into the admin panel and adds in a referral plan EX Ref1 being the name and it will goes from 1 to 50 and percent is 5%
then the next one would start at 51 and so on for as many and as high as they want.
Now.lets say USER 1 has referred 10 people and a few of them make a deposit of different amounts say $10,$20 and $50
now the deposit page would tehn when it processes the deposit it would chk to see how many referrals he (USER_1) has which would be 10 so then it would see the rate as 5% per deposit of his downline makes so USER_1 would get 5% of the $10,$20 and $50 deposits.
My issue is having it get th percent.
I am going to paste all i have here and you will see i have it where it tells me specific info for testing purposes.
Code: Select all
// Step 1 : add deposit to account balance and processed deposit for the user making deposit
// Step 2 : get upline user id if any
// Step 3 : get referral count from upline of depositing user
// Step 4 : based on count of their upline get percent for commision
// Step 5 : complete deposit and add commision to their upline
//STEP 1:
$amount = 10.00;
//$r=mysql_query("UPDATE accounts SET processed_deposit = '$amount' WHERE username='{$_SESSION['admin_user']}' LIMIT 1")or die(mysql_error());
var_dump("UPDATE accounts SET acct_balance = acct_balance + '$amount',processed_deposit = '$amount' WHERE username='{$_SESSION['admin_user']}' LIMIT 1");
//STEP 2:
$q = mysql_query("SELECT * FROM members WHERE username ='{$_SESSION['admin_user']}'") or die(mysql_error());
$ref_members = mysql_fetch_array($q, MYSQL_ASSOC);
@mysql_free_result($q);
echo "<br/>";
echo "<br/>";
echo "<br/>";
echo "You, "; echo $_SESSION['admin_user']; echo " have just make a $"; echo $amount; echo " deposit";
//echo $ref_members['username'];
echo "<br/>";
echo "<br/>";
$q = mysql_query("SELECT * FROM members WHERE id ='{$ref_members['ref_userid']}'") or die(mysql_error());
$ref_members2 = mysql_fetch_array($q, MYSQL_ASSOC);
@mysql_free_result($q);
echo "You were referred by user id "; echo $ref_members2['id']; echo " also known as "; echo $ref_members2['username'];
echo "<br/>";
echo "<br/>";
$reftotal = $ref_members2['ref_total'];
echo $ref_members2['username']; echo " has "; echo $reftotal; echo" referrals";
echo "<br/>";
echo "<br/>";
echo "<br/>";
echo "<br/>";
echo "<br/>";
as you see i have a fake amount entered up above.
all is working but i am unsure how to have it select based on the ref count to get teh percent I need it to tell me what rate its getting once this is done then i will be able to complete it but i am lost.
Thanks for any help
Re: Finding the correct percent
Posted: Fri Jun 15, 2012 4:59 pm
by califdon
So the `ref_total` field in your table is the number that the percentages are based on?? How will that value get updated? Where are you going to store the values of percentage, based on a range of referrals someone has? In a separate table? Will they apply to all transactions for that admin? Have you designed that table yet? Or are you asking how to design that table? To answer that last question, I would have to understand the payment system, since I have no knowledge at all of "downlines", etc. I would probably design such an application with a totally different approach, but it's hard to say because I have no idea how those payment systems work and I don't want to learn.
I will mention that your script has some unusual and generally inefficient parts that make it very difficult to read and understand. They all should work, however, so I don't want to confuse the issue that you are seeking help on. But, for example, you have about 7 more echo commands than you need, since there is no point in writing
Code: Select all
echo "<br/>";
echo "<br/>";
echo "<br/>";
All you need is:
Re: Finding the correct percent
Posted: Fri Jun 15, 2012 5:03 pm
by nite4000
the is updated each time that user referrs someone the percentages are in a referrals table alone with the min and max aka from and to values for each percentage. as i states this is just a demo of me working on it so I can then add it in and test it right this is just a fake test with a fake deposit as if it was real. I do that so i can be sure i am doing it right.
Thanks for the help. maybe someone else will have a idea
ref_total is in members and the to_value and from_value are in referrals so based on ref_total that is how it gets the correct percentage
Re: Finding the correct percent
Posted: Fri Jun 15, 2012 5:28 pm
by califdon
nite4000 wrote:ref_total is in members and the to_value and from_value are in referrals so based on ref_total that is how it gets the correct percentage
Well that won't work, if I understand what you are doing. Relational databases should be designed so that each table represents an entity and all fields in each table must be dependent entirely on the primary key value of each row. Table `members` represents the entity "members", so it is correct that the ref_total is a field in that table, since it is entirely dependent on the member's primary key (`id`). But if table `referrals` represents the referral transactions (whatever they are), then the `from_value` and `to_value` do NOT belong in that table, since those values are NOT dependent on the the referral transaction primary key, but on the decision of the site admin, which probably indicates that they belong in another table, `admins` or wherever you store things like the admin's id and contact information.
This is why you are having trouble with how to get the percentage. You don't have an understanding of what a relational database is and how it must be designed. It might help for you to study a few tutorials about relational database design. Use a search engine and look up that phrase, to find some tutorials. But the range criteria for the percentages surely belongs in the same table as the admin's info, since they are dependent on which admin establishes them, as I understand your earlier explanation.