Making an inbox with space - php

I'm making an inbox, with space, i've decided that you could have MAX 200 messages, i counted (inbox+outbox) and now im trying to make a "spacemeter"
You'r using 20% of your space
My question now, how could i count this to percent? So it always gets 100% if there is 200 messages?
I have two variables with numbers $got and $sent and then i count them with $total = $got+$sent
I'm sorry for a bad explination if you dont understand i will try to explain better!:)

This is basic math.
$percentage_of_space = ($total / 200) * 100 ;

this can be obtain like this
$total_max_messages = 200; // max limit of your inbox+outbox
$inbox_msg = 10; // inbox msg count
$outbox_msg = 15; // outbox msg count
$total_msg = $inbox_msg + $outbox_msg; // total msg (inbox+outbox)
$used_space = ($total_msg/$total_max_messages) * 100; // in percentage
$left_space = 100 - $used_space; // in percentage

Related

PHP - Changing Value by a small market percentage

First post, please be gentle.
I'm trying to create a simple market script where for example I have a number in my database ie 50.00 and I want to run a cron job php script to increase or decrease this randomly to a minimum of 10.00 and a maximum of 75.00.
I thought a random 0,1 follow by 2 if statements 1 rand(-0.01,0.05) if 2 rand(0.01,0.05) then $sql = "UPDATE price SET oil='RESULT'";
I've tried a few times at the above but I can't get it to run and the other crons in the file work.
<?php
//Get Oil Price from database
$oilchange = rand(1, 2);
if ($oilchange == '1') {
$oilnew = rand(0.01,0.05);
//Oil price from database times oil new.
} else {
$oilnew = rand(-0.01,-0.05);
//Oil price from database times oil new.
}
// Update Price
?>
Rand is for integers (whole numbers)
First up, your use of rand between two decimal values (called floats) won't work, as rand is for integers only. So, you'd first want to have a random function which does output floats, like this:
function randomFloat($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
Then we can safely use it between, say, 1% and 5%:
$percentSwing = randomFloat(0.01, 0.05);
Rand defaults to being 0 or 1. We can use that to randomly invert it, so we also cover -1% to -5%:
$percentSwing *= rand() ? 1 : -1;
The above could also be written like this:
if(rand() == 1){
// Do nothing:
$percentSwing *= 1;
}else{
// Invert it:
$percentSwing *= -1;
}
So, we now know how much we need to swing the number by. Let's say it was $oilPrice:
$oilPrice = 48;
We can just multiply the percent swing by that number to get the amount it's changing by, then add it back on:
$oilPrice += $percentSwing * $oilPrice;
So far so good! Now we need to make sure the price did not go out of our fixed range of 10 to 75. Assuming you want to 'clamp' the number - that means if it goes below 10, it's set at 10 and vice-versa, that's done like this:
if( $oilPrice < 10 ){
// It went below 10 - clamp it:
$oilPrice = 10;
}else if( $oilPrice > 75 ){
// It went above 75 - clamp it:
$oilPrice = 75;
}
The above can also be represented in one line, like this:
$oilPrice = max(10, min(75, $oilPrice));
So, that gives us the whole thing:
function randomFloat($min = 0, $max = 1) {
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
}
// Define the oil price (e.g. pull from your database):
$oilPrice = 48;
// get a random 1% to 5% swing:
$percentSwing = randomFloat(0.01, 0.05);
// Invert it 50% of the time:
$percentSwing *= rand() ? 1 : -1;
// Swing the price now:
$oilPrice += $percentSwing * $oilPrice;
// Clamp it:
$oilPrice = max(10, min(75, $oilPrice));
// Output something!
echo $oilPrice;
As a side note here, money in real financial systems is never stored as a float, because rounding errors can cause major problems.

Struggling with maths... How to reverse calculate a percentage?

I believe this is a language agnostic question and more focused on math, however I prefer PHP. I know how to calculate percentages the normal (forward) way:
$percent = 45.85;
$x = 2000000;
$deduction = ($percent / 100) * $x; // 917,000
$result = $x - $deduction; // 1,083,000
What I would like to do, is be able to reverse the calculation (assuming I only know the $percent and $result), for example...
54.15% of x = 1,083,000
How do I calculate x? I know the answer is 2,000,000, but how do I program it to arrive at that answer?
I found a similar question & solution through Google but I just don't understand how to implement it...
You can do
1,083,000 * 100 / 54.15
In PHP, it will be
$x = $result * 100 / $percent
When you say 54.15% of x = 1083000, you mean 0.5415 * x = 1083000. To solve for x, divide 0.5415 from both sides: x = 1083000 / 0.5415. The PHP is:
$p = 54.15;
$r = 108300;
// First, make p a number, not a percent
$p = $p/100; // I would actually use $p/= 100;
// Now, solve for x
$x = $r/$p;

PHP spin system. The lower the number is, the more rewards you get

Okay I have kind of a spin system, you spin and it generates a random number.
If the number is less than 100, you will win.
But how can I make it so, the lower the number his, the higher coins you will get
Currently i have this:
public function getPrize($number)
{
$prize = $number * 250 / 2;
if ($number < 100)
{
return '<span style="color: green;">You have won lucky <b>'.$prize.'</b> coins!</span>';
}
else
{
return '<span style="color: red;">Sorry but, bad luck. You have won nothing! number: '.$number.'</span>';
}
}
$prize is the prize. Basically now I am multi piling it by 250 and dividing by 2. so if I get the number '1'. i will get an awful prize.
How do I do that?
Solved it with a little of thinking and calculation.
1000 - 250 / 100 = 7.5
$prize = 250 + (750 - ($number * 7.5));
Results:
x(1) = 1000
x(100) = 250
Here is one way. Just takes the opposite of the number using a maximum.
function getNumPrizes($number)
{
$maxPrizes = 100;
// using max so we dont get less than 0
$prizesWon = max(($maxPrizes - $number) + 1, 0);
return $prizesWon;
}
This way you'll end up getting 100 coins for a 1, 99 for a 2, etc.
You could then run $prizesWon through another function to scale it how you wish.
Here is another solution.
function getNumPrizes($number)
{
$maxPrizes = 100;
$multiplier = // number you want to multiply with the result. It may be 125 or something else
// using max so we dont get less than 0
$prizesWon = max(($maxPrizes - $number) + 1, 0)*$multiplier;
return $prizesWon;
}
Maybe you'll try this way:
public function getPrize($number)
{
$quad = $number*$number;
if ($number<100 && $number>0)
{
$prize = 0.0525665*$quad-12.885*$number + 1012.83; //http://www.wolframalpha.com/input/?i=quadratic+fit+%281%2C1000%29%2C%2850%2C500%29%2C%28100%2C250%29
return '<span style="color: green;">You have won lucky <b>'.$prize.'</b> coins!</span>';
}
else
{
return '<span style="color: red;">Sorry but, bad luck. You have won nothing! number: '.$number.'</span>';
}
}
edit.
I've found a function which gives the expected results for the given numbers. Hope it's ok.
Data source: Wolphram Alpha
Final version:
if($number < 100){
$prize = round((99.00 - floatval($number)) * 7.653) + 250;
else{
$prize = 0;
}
This gives 250 for $number = 99 and 1000 for $number = 1 as author desires.
you are trying to define a math function f(x) where f(1) = 250 and f(99) = 1000;
there are lots of possible shapes which will.
i suggest you graph the results of your functions to help you decide what is best for you.
here are some examples.
// linear growth
// this produces a straight line
function prize($number) return (101 - number) * 75 + 250;
// log growth
// this produces a log curve where you have diminishing returns
function prize($number) return (log(101 - number) -1 ) * 750 + 250;
// exp growth
// this returns exponential returns
function prize($number) return (((101-number)^2)/10000) * 750 +250;
the basic operations here are you have a function which generates a values for the series between 1-100.
invert the input (101-number) so that smaller inputs produce bigger results.
map the output to your scale... which is between (0 to 750) by multipling 750 by a ratio.
translate your scaled number to 250 which is your minimum

simple PHP math = height

I'm kind of stuck with this, it SHOULD be very simple, but my brain can't wrap around it (it's Friday... lol)
I've got a thermometer, representing $1,000,000 max. it's 375px tall.
I'm running a DB Query to grab amounts from user submissions (between $1 and $200).
At that math, it's $2,666.66 per pixel to move it up 1 pixel ---
retrieve_amount(); is my DB function that grabs all the amounts - that's simple.
$fill_query = retrieve_amount();
$fill = 0;
$total = 0;
while($fill_query->is_valid() ) : $fill_query->amount();
$amount = get_valid_amount($input, 'amount');
$total = $total + $amount;
endwhile;
$finaltotal = $total; // THIS is the line that grabs the final total from above. Should work?
$fillheight = $SOMETHING +/-* $SOMETHING; // this is the line that i'm less sure of how to get my result
It may be that I'm just not great with math, but my questions are
$finaltotal = $total
should work to receive the total amount retrieved from the DB Query, correct?
And more importantly, how do I translate that to the pixels that I need?
$maxPixels = 375;
$maxAmount = 1000000;
$currentAmount = 1234567;
$currentPixels = round(($currentAmount / $maxAmount) * $maxPixels);
It's basically just like calculating percentages. Except, instead of 100%, your max is now 375 pixels.

PHP looping limit the number

I have to submit all telephone numbers from my database records to URL, but I need to restrict each time send 300 phone numbers only.
I need a php script able to run following scenario:
Retrieve 2,000 records from database.
Loop all rows and save each into a variable or something else. (important)
Count total has 2,000 records.
Loop 300 records each time to write into URL. (very important)
Submit the URL (this part no need to explain)
loop for next 300 records to write into URL, and repeat it until record 2,000.
I believe in this case, 2,000 / 300 = 7 times of looping, which 300 records for first 6 times and final time is sending 200 records only.
As I mentioned above the looping for 300 records is very important, and next looping able to know starting from record 301 until 600, and so on.
EDITED
Below are my original code, but it's reading all phone numbers and dumb them all to my URL:
$smsno = trim($_REQUEST['string_of_phone_number_eg_0123456;0124357;0198723']);
$message = trim($_REQUEST['message']);
$phoneNo = explode(";", $smsno);
// ----------
//
// Need to count total $phoneNo, eg total is 2,000 phone numbers
// Loop 300 times for the phone numbers, eg 001-300, 301-600, 401-900, ..., 1501-1800, 1801-2000
// Every 300 records, eg $phoneStr = '0123456;0124357;0198723;...' total 300 phone numbers in this string
// Write into my URL: $link = "http://smsexample.com/sms.php?destinationnumber=$phoneStr&messagetosms=$message";
//
// ----------
I am seeking solution from here as I have no idea how to loop each 300 records and write into string then throw this string to my URL.
I can make the first 300 records, but how to get next 300 records after first 300 records write into string and throw to my url, and waiting to perform second throw to url.
For example,
first loop for 300 records:
$phoneStr = phoneNumber01;phoneNumber02;phoneNumber03;...;phoneNumber300
$link = "http://smsexample.com/sms.php?destinationnumber=$phoneStr&messagetosms=$message";
second loop for next 300 records
$phoneStr = phoneNumber301;phoneNumber302;phoneNumber303;...;phoneNumber600
$link = "http://smsexample.com/sms.php?destinationnumber=$phoneStr&messagetosms=$message";
and so on.
for ($i = 1; $i <= 2000; $i++)
{
if ($i % 300 == 0 || $i == 2000)
{
//Make URL and send
}
}
// Per-request limit
$limit = 300;
// Get array of numbers
$numbers = explode(';', $_REQUEST['string_of_phone_number_eg_0123456;0124357;0198723']);
// Get message
$message = trim($_REQUEST['message']);
// Loop numbers
while ($numbers) {
// Get a block of numbers
$thisBlock = array_splice($numbers, 0, $limit);
// Build request URL
$url = "http://smsexample.com/sms.php?destinationnumber=".urlencode(implode(';', $thisBlock))."&messagetosms=".urlencode($message);
// Send the request
$response = file_get_contents($url);
}

Categories