
imgrooot
-
Posts
383 -
Joined
-
Last visited
-
Days Won
1
Posts posted by imgrooot
-
-
I have a table like this.
investment_id | investor_id | deposit | Date | Status
I will be depositing funds into this table. For eg. If I am paying out 1000 investors at a time, 1000 rows will be added to this table. I might do this 100 times a day. That's 100,000 rows added in a day. Each deposit I do will be unique, even though it might be going to the same person multiple times.
This could end up with million of rows. I am wondering if this is the correct way to do this or is there a better method?
-
That doesn't work. Your supposed results of the second & third calculations are not correct. For the 30% users the result would be $0.60 and for the 10% users it would be $0.50. In both instances you used 250 as the $total_members from the first calculation. So, with those correct results, a person with a 10% (and only 20 members) would get more than a person with a 60% (and 250 members).
The main issue with the problem is the use of the term "percentage". Based on your initial description the "percentage" should really be considered as the number of "shares" the people have. This really sounds like a stock problem - not a commission problem. To determine a persons value in a stock, the formula is as follows:
[total value] / [total shares of stock] * [user's shares of stocks]
The part in blue in the value of a single share.
Example
//Define the total amount $total_amount = 100; //Define the members and thier shares $member_shares = array( 60 => 250, //250 members with 60 shares each 30 => 50, // 50 members with 30 shares each 10 => 20 // 20 members with 10 shares each ); //Calculate the total shares $total_shares = 0; foreach($member_shares as $shares => $members) { $total_shares += $shares * $members; } //Calculate the value of a single share $share_value = $total_amount / $total_shares; //Calculate the value of each unique share amount foreach($member_shares as $shares => $members) { $value = $share_value * $shares; //Round/format as needed echo "Shares {$shares}: {$value}<br>\n"; }
Your right. The results for my second and third calculations would be what you wrote. My mistake what I wrote in the comment part of it. But the math works out with the code itself.
Your example is an interesting way to solve this problem. And yes now that you mention it, it is more like shares than commissions. I will try it out as well.
-
Alright so I think I have it figured out. It's rather simple.
$total_amount = 100; // 100 dollars $total_members = 250; // 250 members $total_commission = 60; // 60% commission $get_percent = ($total_commission / 100) * $total_amount; $new_total = $get_percent / $total_members; // $60 / 250 = 0.24 per user. This is what I was missing before. // Since above is based on 60%, I still have 30% and 10% left. Below are these examples. $total_amount = 100; // 100 dollars $total_members = 50; // 50 members for this example, just to vary it up. $total_commission = 30; // 30% commission $get_percent = ($total_commission / 100) * $total_amount; $new_total = $get_percent / $total_members; // $30 / 250 = 0.12 per user. $total_amount = 100; // 100 dollars $total_members = 20; // 20 members for this example, just to vary it up. $total_commission = 10; // 10% commission $get_percent = ($total_commission / 100) * $total_amount; $new_total = $get_percent / $total_members; // $10 / 250 = 0.04 per user.
$60(60%), $30(30%), $10(10%) sum up to $100. No matter how many users are in each percentage pool, it'll always sum up to that exact total.
-
If you had 250 users then each would have 1/250th of the pool, or
0.004
. If that 250 were only eligible for 10% of the main pool then it'd be0.1 * 0.004 = 0.0004
Alright so based on what you said, here is the new example.
$total_amount = 100; // 100 dollars $total_members = 250; // 250 members $total_commission = 60; // 60% commission $new_commission = $total_commission / 100; // this would return 0.6 $new_members = 1 / $total_members; // this would return 0.004 $userPercentage = $new_commission * $new_members; // 0.6 * 0.004 = 0.0024 $userShare = $userPercentage * $total_amount; // 0.0024 * 100 = 0.24 $new_total = $userShare * $total_members; // 0.24 x 250 = 60 That returns a total of 60 for each user.
That is technically correct. But remember there are total of 250 users. That's $60 x 250 = $15,000. That not what I am looking for. I am looking for all 250 users' amount to sum up to $100 in total. Normally I can 100/250 and it'll return $0.4 for each user. But since each user has a different percentage, each of them is going to have different values that add up to $100.
Please let me know if I am making sense.
-
I should also mention that the example I share and the example you edited have are for 3 users only that equals to $100. I will be using this formula in a foreach loop. So if I have more the 4 users, which I do, their total sum is greater than $100, which is not what I want.
For eg. Here's the for each loop.
$total_members = 250; foreach($result as $row) { $commission = $row['commission']; // This commission could be 10%, 30%, 60%; $amount = $row['amount']; $new_commission = $commission / 100; $userPercentage = 0.1 * $new_commission; $total = $amount; $userShare = $userPercentage * $total; $new_total = $userShare * $total_members; echo $new_total; }
-
You did not account for the 1/10th of each pool allocated, as Kicken did in his example. His was pre-computed for you in his example (ie. .05).
In plain english a single user's share is "1/10th of pool% of total"
$userPercentage = 0.1 * 0.1; // 10% $total = 100; $userShare = $userPercentage * $total; // 0.05 * 100 = 10 $new_total = $userShare * 10; // 10 being the amount of users. This totals $100. $userPercentage = 0.1 * 0.3; // 30% $total = 100; $userShare = $userPercentage * $total; // 0.3 * 100 = 30 $new_total = $userShare * 10; // 10 being the amount of users. This totals $300. $userPercentage = 0.1 * 0.6; // 60% $total = 100; $userShare = $userPercentage * $total; // 0.6 * 100 = 60 $new_total = $userShare * 10; // 10 being the amount of users. This totals $600.
Here's your code with some variable echoing to show you that this works: https://3v4l.org/p5cVXSo what if I have 250 users? What would 0.1 look like in this line? Is it 1/10th of 250, which is 25?
$userPercentage = 0.1 * 0.6;
-
Actually I realized something. Your method would normally work but not with what I am trying to do.
For eg.
Say I have 30 users.
10 users get 10%
10 users get 30%
10 users get 60%
Total of 30 users with their own percentages. Using your method, it would look like this.
$userPercentage = 0.1; // 10% $total = 100; $userShare = $userPercentage * $total; // 0.05 * 100 = 10 $new_total = $userShare * 10; // 10 being the amount of users. This totals $100. $userPercentage = 0.3; // 30% $total = 100; $userShare = $userPercentage * $total; // 0.3 * 100 = 30 $new_total = $userShare * 10; // 10 being the amount of users. This totals $300. $userPercentage = 0.6; // 60% $total = 100; $userShare = $userPercentage * $total; // 0.6 * 100 = 60 $new_total = $userShare * 10; // 10 being the amount of users. This totals $600. All totaling to $1,000 for 30 users.
That's not what I want. I want to have all 30 users' total come to the original amount, which is $100, not $1,000.
-
If you have a percentage for each user then you'd just multiple the percentage (in decimal form) against the total to get their share.
$userPercentage = 0.05; $total = 100; $userShare = $userPercentage * $total; // 0.05 * 100 = 5
You'll have to decide what you want to do as far as rounding though if necessary. For example with USD you could end up with values less than a cent depending on the breakdown. If you had 1000 users but only $5 to distribute equal distribution would be 0.5% each or 1/2 of a cent. Since you can't give someone half a cent you'd have to find a solution. Possible solutions would be:- Add the 1/2 cent to a running total of what is due then distribute when they reach a minimum amount
- Round to 0 and give them nothing (users would probably hate you)
- Round to one cent (users would like you, but you'd loose money)
That makes sense. Thank you.
-
Say I have 5 users. Each user will receive their payment based on their individual percentage.
For eg.
User 1: 5%
User 2: 10%
User 3: 15%
User 4: 15%
User 5: 5%
Now say I have a total of $100. I want to divide the $100 among the 5 users. But the users with higher % will receive a higher amount than the the ones with lower %.
How would I calculate this?
My question may be a little confusing so I will give you another example.
Above I gave you an example of 5 users only. This could easily be 100 or 1000 users. So If I have 100 users with individual percentage of 5%,10%,15% and the total give away amount is $100, how would you divide the $100 among the 100 users?
-
Say I have 5 users. Each user will receive their payment based on their individual percentage.
For eg.
User 1: 5%
User 2: 10%
User 3: 15%
User 4: 15%
User 5: 5%
Now say I have a total of $100. I want to divide the $100 among the 5 users. But the users with higher % will receive a higher amount than the the ones with lower %.
How would I calculate this?
-
My code works but so does your code. And I must admit your code is far more simpler and cleaner. I will use your example. Thanks.
-
Well since no one else has anything to add. Here's my full answer to the original problem.
$api_amount = array(); $api_recp = array(); $api_trans_id = array(); $api_sender_id = array(); foreach($get_address->data->txs as $tx){ $api_amount[] = $tx->amounts_received[0]->amount; $api_recp[] = $tx->amounts_received[0]->recipient; $api_trans_id[] = $tx->txid; $api_sender_id[] = $tx->senders[0]; } $post_transid = $_POST['trans-id']; // example 1 if(in_array($post_transid, $api_trans_id)) { echo 'yes'; } else { echo 'no'; } // example 2 if(in_array('0.00400000', $api_amount)) { echo 'yes'; } else { echo 'no'; }
-
I tried it as
$api_sender_id[] = $tx->senders[0];
And it seems to work. So far so good.
Please let me know if anything I've said is incorrect.
-
Alright so I think I figured it out. Using "kicken"'s foreach loop, I need to turn the loop variables into an array and access them outside the loop like this.
$api_amount = array();$api_recp = array();$api_trans_id = array();$api_sender_id = array();foreach($get_address->data->txs as $tx){$api_amount[] = $tx->amounts_received[0]->amount;$api_recp[] = $tx->amounts_received[0]->recipient;$api_trans_id[] = $tx->txid;$api_sender_id[] = $tx->senders;}$post_transid = $_POST['trans-id'];
if(in_array($post_transid, $api_trans_id)) { echo 'yes'; } else { echo 'no'; }
if(in_array('0.00400000', $api_amount)) {
echo 'yes';
} else {
echo 'no';
}The above works.
One thing that doesn't work is the "$api_sender_id". I realized why. This is how it looks in json. If you look closley you will notice the value of "senders" is directly after the bracket ([). The others don't have that. So how do I access this "senders" value?
status" : "success","data" : {"network" : "BTC","txs" : [{"txid" : "71d9afcc73da63c45674088411460116cfefe324cc7a6f5fcb267d9f584adf73","from_green_address" : false,"time" : 1496877627,"confirmations" : 3080,"amounts_received" : [{"recipient" : "4QFaAYB4r8sk7MG3beb7yY4R21t9JrnMwK","amount" : "0.00200000"}],"senders" : ["6EqvPUTwjUuon6c2a6YxBreJtBkkZD5fsp"],"confidence" : 1.0,"propagated_by_nodes" : null}]}Edit: Sorry for the shit code paste. It shows in fine in the code editor but it for some reason it shows outside of it after I save it. -
Just loop through the txs array and check each transaction.
foreach ($get_address->data->txs as $tx){ $api_amount = $tx->amounts_received[0]->amount; $api_recp = $tx->amounts_received[0]->recipient; $api_trans_id = $tx->txid; $api_sender_id = $tx->senders; //... }
I'm assuming you mean check if it's true or not inside the foreach loop like this?
foreach ($get_address->data->txs as $tx){ $api_amount = $tx->amounts_received[0]->amount; $api_recp = $tx->amounts_received[0]->recipient; $api_trans_id = $tx->txid; $api_sender_id = $tx->senders; if($api_trans_id == $_POST['trans-id']) { echo 'Success'; } else { echo 'Fail.'; } }
If so then I get the "Fail" error. It's shown repeated multiple times.
-
I asked this question before and I did receive the answer I was looking for. But now I realize something. It only works for the first result. Let me explain.
I am using this API. https://block.io/api/simple/php
$get_address = $block_io->get_transactions(array('type' => 'received', 'addresses' => $my_wallet)); $api_amount = $get_address->data->txs[0]->amounts_received[0]->amount; $api_recp = $get_address->data->txs[0]->amounts_received[0]->recipient; $api_trans_id = $get_address->data->txs[0]->txid; $api_sender_id = $get_address->data->txs[0]->senders;
These are variables above i am using to check if something is true or not. For eg. The form below. I want to check if the $api_trans_id matches the the post id I submit below.
if($api_trans_id == $_POST['trans-id']) { echo 'Success'; } else { echo 'Fail.'; } <form action="" method="post"> <input type="text" name="trans-id" value="" maxlength="1084" /> <input type="submit" name="submit" value="Submit" /> </form>
The above works if the first result in the json array below has the same post id or else it fails. I want to loop through ALL the json arrays and try to match the post id($_POST['trans-id']).
Below are the 3 array results to give you an example. The first txid is "41d9afcc73da63c45674088411460116cfefe324cc7a6f5fcb267d9f584adf73". If I submit this same id as the post id in the form, it'll be success full. But if I use the 2nd or 3rd txid below, it will fail.
So how can I change this "$api_trans_id = $get_address->data->txs[0]->txid;" so that it looks for all three txid instead of only the first one?
{ "status" : "success", "data" : { "network" : "BTC", "txs" : [ { "txid" : "41d9afcc73da63c45674088411460116cfefe324cc7a6f5fcb267d9f584adf73", "from_green_address" : false, "time" : 1496877627, "confirmations" : 3080, "amounts_received" : [ { "recipient" : "1QFaAYB4r8sk7MG3beb7yY4R21t9JrnMwK", "amount" : "0.00200000" } ], "senders" : [ "2EqvPUTwjUuon6c2a6YxBreJtBkkZD5fsp" ], "confidence" : 1.0, "propagated_by_nodes" : null }, { "txid" : "2f72d4dd97234937ddf6e8174b17a9a421ba35bbfb6fa40cfeb6ac92db6ce032", "from_green_address" : false, "time" : 1491675979, "confirmations" : 12338, "amounts_received" : [ { "recipient" : "1QFaAYB4r8sk7MG3beb7yY4R21t9JrnMwK", "amount" : "0.00010000" } ], "senders" : [ "3L98MQZKvwNo21fFpY7RJ9HYfYdv62XpYz" ], "confidence" : 1.0, "propagated_by_nodes" : null }, { "txid" : "fce58f0cb1d32c1ed7c27825c41753ce98323771bd931f0a0f99b6294d9ee642", "from_green_address" : false, "time" : 1491672301, "confirmations" : 12341, "amounts_received" : [ { "recipient" : "1QFaAYB4r8sk7MG3beb7yY4R21t9JrnMwK", "amount" : "0.00010000" } ], "senders" : [ "4L98MQZKvwNo21fFpY7RJ9HYfYdv62XpYz" ], "confidence" : 1.0, "propagated_by_nodes" : null }, ] } }
-
Nothing happens. I don't get any errors. I just never receive the email in the inbox.
-
I am using a phpmailer to send emails to users. I noticed that it only sends email if I am calling it from a page in a root directory. Say I am sending an email from contact.php and it's inside a sub folder/directory instead of the root, it won't email out.
Setup is like this.
include_once '../phpmailer/class.phpmailer.php'; $mail = new PHPMailer; $mail->IsSMTP(); // Set mailer to use SMTP $mail->Host = '127.0.01'; // Specify main and backup server $mail->From = '[email protected]'; $mail->FromName = 'Myname'; $mail->AddAddress($user_email); // Name is optional $mail->WordWrap = 50; // Set word wrap to 50 characters $mail->IsHTML(true); // Set email format to HTML $mail->Subject = 'Your account has been deleted!'; $mail->Body = "Hello {$username},<br><br> We are letting you know that your account has been deleted";
if(!$mail->Send()) {$errors[] = $mail->ErrorInfo;} else {if(empty($errors)) {$db->commit();$success = 'Email sent.';} else {$db->rollBack();}} -
Say I have directory setup like this.
core/
includes/
snippets/
template.php
js/
index.php
Say in the index.php file, I have a file path like this " include 'snippets/template.php'; ". It works.
Or say If I am in template.php and I have a file path like this " include '../index.php'; ". It works as well.
I am wondering if there is a more global method to this so I don't have to worry about when to use "../" or not. It becomes hectic if there are a lot of includes and/or links.
-
And that's dangerous nonsense which you a) need to realize and b) explain to him in a diplomatic way.
Again: Running a public username through a public algorithm with no additional input doesn't magically turn it into a “complex password”. That's mathematically impossible. It cannot be done. Not even Chuck Norris can do it. What you end up with is a bunch of entirely unprotected social media accounts, and nobody today can afford that.
Let's imagine this forum generated our passwords by running our username through, say, SHA-256. Can you see the problem with that? Do you see how somebody else might be able to calculate your password? And that's exactly the problem.
If you want to generate passwords, you need secret input, be it pure randomness (the optimum), a user-chosen string (acceptable) or a server-side secret (the weakest option). There is no other way.
I see your point. I will sure to relay your concerns to the client.
-
Usually - and I am not a security expert - the request you are detailing is for the INPUT value, not the encrypted result. Whatever the result of your encryption process becomes a password (that's what it is). I really can't see you even attempting to create your own encryption method when they are so difficult to write and when there are several of them already out there. If your client is really making this kind of demand, then they should be referred to some good security documents/guidelines to see how out of line they are in this spec.
My $.02.
One last thing - I notice you did not mention any kind of decryption and that's a good thing. If there was a decryption method then you have just made your supposedly-secure token much less secure. One creates a plain-text password (following the above-mentioned guidelines) and it is used to create an encrypted token that gets saved in a secure table usually. Any authorization attempt then uses the user's password to generate the token using the same method again and compares the stored encrypted value against the just-generated value and if they match the authorization is said to succeed. The user (and hopefull nobody else) ever sees the encrypted value.
You're second statement could be one of the ways to achieve this goal.
-
For the third time: What is the client trying to achieve? Forget about the “encryption” stuff. As you've been told multiple times, your client doesn't know what he's talking about, so taking him literally will likely end in a disaster for both of you. You need to figure out what he really wants, not what he says he wants.
So users enter what? Their username? And the output is what? Their password? First off, what on earth is the point of that? Secondly, this is a horrible idea. The “password” is now calculated from public input using a public algorithm, which means anybody can do the same calculations to obtain it. The output may look strong to a layman, but this is all fake. In reality, you're effectively using the name as the password.
And again, what is the whole point? What is the use case? We can talk about the tech stuff later. First we need to understand what this is even about.
That's all he's trying to achieve. The output is the encrypted password. He basically wants users to have a complex password that they can use for their social media accounts. For eg. User inputs "johnsmith", the output will be "Rju9f3j5?10w3j81". The user can use this output password to register for say Facebook. The user also knows that if they ever forget the output password but knows the original input, he can go back to the site, input "johnsmith" again and receive the exact same output as before. That's his goal.
-
Alright so here's the new info.
The new encryption will be like a password. User inputs text into a textfield and hits submit. It will return a string of 16 digits with uppercase letter, lowercase letter, a number and a special character. There will be no decryption or saving it in the database. Also the client wants it so that it's not a random string of numbers for the same word. So if the input is "johnsmith", then it will return the same string no matter how many times it has been submitted.
Can that be done?
-
I don't think you understand what encryption means. Of course you can arbitrarily map strings to other strings, but this has nothing whatsoever to do with encryption. So you obviously mean something very different.
Again: What are you, or what is your client trying to achieve? Give us the full picture, then I'm sure we can come up with an idea.
I'm getting the details tomorrow. I'll let you know.
Is there a better solution to storing this data in mysql table?
in PHP Coding Help
Posted · Edited by imgrooot
I didn't know a database could hold billions of rows and still work fine. Good to know. That answers my question. Of course I was giving an extreme example when I said millions of rows. But if it can handle that, then great. I was a little hesitant before because I never had that many records stored in a table before.