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);
}
Related
I'm building an application to help customer calculate various product prices.
Right now I'm building a feature where user enters a single number to the application and submits a form. Based on that number, I would like to define another variables value.
What I'd like to achieve
If user input is number between 1-10, set variable number to 200.
If user input is number between 11-20, set variable number to 400.
If user input is number between 21-30, set variable number to 600.
If user input is number between 31-40, set variable number to 800.
If user input is number between 41-50, set variable number to 1000.
And so on... So basically increasing by 200 every tenth.
Of course, I could do something like this:
$userInput = 11;
$result;
if($userInput => 1 && $userInput =< 10)
$result = 200;
if($userInput => 11 && $userInput =< 20)
$result = 400;
if($userInput => 21 && $userInput =< 30)
$result = 600;
But it isn't really a great solution, because it takes lot of code and if user sets number out of the determined range it doesn't work..
How can I implement this with as little amount of code as possible?
If I have the math right, you just need to divide the number by 10, and use ceil to round the fraction up. From there, multiply it by 200;
function getVariable($num) {
$divisor = ceil($num / 10);
return $divisor * 200;
}
echo getVariable(1)."\n"; // 200
echo getVariable(6)."\n"; // 200
echo getVariable(13)."\n"; // 400
echo getVariable(27)."\n"; // 600
echo getVariable(48)."\n"; // 1000
echo getVariable(50)."\n"; // 1000
echo getVariable(88)."\n"; // 1800
echo getVariable(100)."\n"; // 2000
So I have an array of value
if(isset($wholeprices) && !empty($wholeprices)){
foreach($wholeprices as $wholeprice){
$get_wholeUnits[] = $wholeprice->Units;
$get_wholePrices[] = $wholeprice->UnitsPrice;
}
this $get_wholeUnits[] has values such as the following
array(
0=>200,
1=>150
);
this $get_wholePrices[] has values such as the following
array(
0=>50,
1>70
);
for($get_whol_price=0;$get_whol_price<count($get_wholeUnits);$get_whol_price++){
if(350 >= $get_wholeUnits[$get_whol_price]){
$wholesale_price_Set = $get_wholeUnits[$get_whol_price];
$gross_price = 350 * $get_wholePrices[$get_whol_price];
}
}
Now I have a number 350, I want it to be calculated only with 200 as 200 closest to 350. But if I have number as 190 , than it should calculate with 150 , as it is closest.
Since my code (for 350) has 200 at the first index, and the first if condition to true, it will result me the value, but at the same time, at the second index, again the if condition executes.
I just want a simple behavior that, whatever the number is, it should identify the closest number irrespective of the index they fall in.
You have to process the whole array in order to find the closest value.
You need to do something like this
$valueToCompare = 300;
$arrayOfvalues = [/*Some different values here*/];
$closestValue = arrayOfvalues[0];
for($i=1; $i<count($arrayOfvalues);$i++){
if(abs($valueToCompare - $closestValue) > abs($valueToCompare - $arrayOfvalues[$i]))
$closestValue = $arrayOfvalues[$i];
}
At last, you will have the nearest value from the array inside the $closestValue variable.
I'm not 100% if i get what you are asking but if i'm not wrong this would work.
for($get_whol_price=0;$get_whol_price<count($get_wholeUnits);$get_whol_price++){
if(350 >= $get_wholeUnits[$get_whol_price]){
$wholesale_price_Set = $get_wholeUnits[$get_whol_price];
$gross_price = 350 * $get_wholePrices[$get_whol_price];
break; //This will exit from for loop since you have the value that you wants.
}
}
}
Okay, so i don't really know how I go about this.
I'm currently working on a lottery system for a game.
I have a table with virtual items which I want to randomly select by a likely chance.
Table examples:
ID = 1, item_name = Sword, likely_chance = 75
ID = 2, Item_name = 10,000, likely_chance = 20
For id 2, 10,000 represents 10,000 coins.
I want to come up with an algorithm which will select a item with a higher chance of selecting a higher likely chance but also still be able to win a item with a lower likely chance rarely.
If you have items with "likely chances" of C1, C2, C3...Cn, then you can calculate the sum Ctotal.
Then, you can get a random value between 0 and Ctotal, and walk through your array (order is irrelevant) until the sum of "skipped" items exceeds this random value.
For example, in your case, Ctotal = 75 + 20 = 95. Get a random number between 0 and 95, and if it is less than 75 - give a sword; else - give 10000 coins. It will provide a fair winnings distribution according to your likely chances - 78.95% and 21.05%, respectively.
$items = ['Sword', '10000 coins'];
$chances = [70, 25];
$ctotal = array_sum($chances); echo "Total is $ctotal\n";
$rand = rand(0, $ctotal); echo "Rand is $rand\n";
$i = 0;
$currentSum = 0;
while (true)
{
$currentSum += $chances[$i];
if ($currentSum >= $rand)
{
echo "You win: ".$items[$i];
break;
}
$i++;
}
Here is the working Demo. Note that IDEOne remembers the last output and doesn't run this program again every time. The output will appear to be the same, but it is not.
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
Not sure if this is possible with just php or if I'd need something else too. Basically I'd like to have an array with a bunch of entries, such as:
(1234,5432,5678,5899,3245)
And I'd like to have the php randomly select one entry from the array AND stick to that entry it selected for 3 days. So for example, on a given date, it would pull "5432" from the array, then it would hold that entry for 3 days, then after 3 days it would pick another entry and hold it for 3 days, etc.
Any ideas how this could be done? Can it be done with just php? Thanks so much for any help.
Assuming you're running a PHP program (command line), and not running a script on a web server you can run this script to stay on all the time and wait to draw.
$entries = array( 34534, 435, 345 );
while(1) {
// subtract 1 from total number of entries because arrays start at an index of 0.
$totalNumberOfEntries = sizeof( $entries ) - 1;
// if no entries left, quit the program
if ($totalNumberOfEntries <= 0) break;
// grab a random index from your array using `mt_rand()` function
$entry = $entries[ mt_rand(0, $totalNumberOfEntries - 1) ];
// write the entry to a file
file_put_contents( 'entry.txt', $entry, FILE_APPEND );
// wait 3 days to draw again
sleep( 3600 * 24 * 3 );
}