I am trying to switch phpmailer information to shift email server loads every 1000 emails sent. For example every 1000 emails sent, I want to use a different server.
1-1000 emails use server 1
1001-2000 use server 2
2001-3000 use sever 1 and so on
So far I am using this:
$x =1;
foreach($data as $value) {
if ($x <= 1000) {
//use server 1
$x++;
} else {
//use server 2
if ($x == 2000) {
$x = 1;
}
}
Is there a better or more standardized way to achieve this?
Maybe a bit more elegant than if/else:
$phpMailerInstances = ['server1' => $yourInstance, 'server2' => $yourInstance2]
function getServer($currentServer){
if($currentServer == 'server1')
{
return 'server2';
}
return 'server1';
}
$currentServer = 'server1';
for($i = 0; $i < 10000; $i++)
{
echo $i.PHP_EOL;
if($i % 1000 == 0)
{
$currentServer = getServer($currentServer);
echo '>>>>>'.$currentServer.PHP_EOL;
}
$phpMailerInstances[$currentServer]->send()
}
?>
Related
How can i make a level system where i have 2 table, one for the level and another for amount of exp earned?
I want to be able to manage the different exp requierments myself, like level 2 will need 340exp and level 3 need 450exp. I dont want to set one exp amount and then multiply it. I want to manage the whole system.
I also want to set default level and max level, and give exp directly to the database column without too much problem (for forum posts etc).
I have seen a few questions here but i find them outdated or just not what im looking for.
PS: sorry for bad english and bad explenation.
I found a realy good solution and was able to rewrite it to work with my setup. if anyone is interested i will leave the original link and my edit of it bellow.
my edit:
<?php
// Connect to your database like you normally do, then get any value into the $count variable
$count = $exp;
if($level == 0){
$lvl = 1;
}else{
$lvl = $level;
}
if ($count >= 12800) { $lvl = 10; }
else if ($count >= 6400) { $lvl = 9; }
else if ($count >= 3200) { $lvl = 8; }
else if ($count >= 1600) { $lvl = 7; }
else if ($count >= 800) { $lvl = 6; }
else if ($count >= 400) { $lvl = 5; }
else if ($count >= 200) { $lvl = 4; }
else if ($count >= 100) { $lvl = 3; }
else if ($count >= 50) { $lvl = 2; }
// Render stars or any graphics/images according to dynamic $lvl number
$stars = "";
$i = 1;
while ($i <= $lvl) {
$stars .= "★";
$i++;
}
echo "level $lvl";
?>
Original link:
https://www.developphp.com/video/PHP/Experience-Level-Evaluation-Programming-Tutorial
I am trying to build up a script that sends emails to users and pauses 1 hour after x number of emails. But I get 'Page is not redirecting properly', despite the fact when I access the query string manually it works:
cron-wordpress.php?page=1 // works manually
cron-wordpress.php?page=2 // works manually
cron-wordpress.php?page=3 // works manually
Here is the meat of the script:
EDIT SIMPLIFIED THE PROBLEM
cron-wordpress.php
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$divide = 10;
$start = $divide*$page;
$emails = array();
for ($i = 0; $i <= 300; $i++) { // 300 emails in the dbs that means 30 pages $divide = 10
array_push($emails, $i.'#email.com');
}
$emails = array_slice($emails, $start, $divide);
$url = "http://".$_SERVER['SERVER_NAME'].'/wordpress/blog/cron_test.php';
$count = 0;
foreach ($emails as $email) {
echo 'Mail send to '.$email.'<br>';
$count++;
if (($count % $divide == 0) !== false && $count < 2000) {
sleep(2);
$page_nr = $count/$divide + 1;
header('Location: '.$url.'?page='.$page_nr);
//die();
//exit;
}
}
I am using multiple IF statements and like to know how i would put these into an array.
the IF Statements
if ($totalEXP < $l1) echo "1";
if ($totalEXP < $l2) echo "2";
As you can see there the same apart from $l1 and the final echo
i would be using around 100 IF statements, Is there a way to put this in an array to save alot of coding?
I know i should be using the ELSEIF statement
<?php
if ($totalEXP < $l1) {
echo "1";
} elseif ($totalEXP < $l2) {
echo "2";
} else {
echo "MAX";
}
?>
but for simplicity reasons i decided the route i had taken.
Sadly i havent tried much to get this working because i dont know if its even possible.
Yes, of course.
You put your level data in an array
$levels = array(
1 => $l1,
2 => $l2,
...
);
And then loop
$pleyer_level = 1;
foreach($levels as $level => $xp)
if($totalEXP > $xp)
$player_level = $level;
It is also smart to put a break in loops when you know their job is done so you don't iterate pointlessly
$pleyer_level = 1;
foreach($levels as $level => $xp)
if($totalEXP > $xp)
$player_level = $level;
else
break;
Not sure this is the best approach, but you could run the if statements in a for loop.
for ($i = 1; $i <= 100; $i++) // i increases every new loop
{
if ($totalEXP < ${l$i}) // ${l$i} corresponds to $l1, $l2, $l3 and so on
{
// do stuff
}
}
I got the answer fine, but when I run the following code,
$total = 0;
$x = 0;
for ($i = 1;; $i++)
{
$x = fib($i);
if ($x >= 4000000)
break;
else if ($x % 2 == 0)
$total += $x;
print("fib($i) = ");
print($x);
print(", total = $total");
}
function fib($n)
{
if ($n == 0)
return 0;
else if ($n == 1)
return 1;
else
return fib($n-1) + fib($n-2);
}
I get the warning that I have exceeded the maximum execution time of 30 seconds. Could you give me some pointers on how to improve this algorithm, or pointers on the code itself? The problem is presented here, by the way.
Let's say $i equal to 13. Then $x = fib(13)
Now in the next iteration, $i is equal to 14, and $x = fib(14)
Now, in the next iteration, $i = 15, so we must calculate $x. And $x must be equal to fib(15). Now, wat would be the cheapest way to calculate $x?
(I'm trying not to give the answer away, since that would ruin the puzzle)
Try this, add caching in fib
<?
$total = 0;
$x = 0;
for ($i = 1;; $i++) {
$x = fib($i);
if ($x >= 4000000) break;
else if ($x % 2 == 0) $total += $x;
print("fib($i) = ");
print($x);
print(", total = $total\n");
}
function fib($n) {
static $cache = array();
if (isset($cache[$n])) return $cache[$n];
if ($n == 0) return 0;
else if ($n == 1) return 1;
else {
$ret = fib($n-1) + fib($n-2);
$cache[$n] = $ret;
return $ret;
}
}
Time:
real 0m0.049s
user 0m0.027s
sys 0m0.013s
You'd be better served storing the running total and printing it at the end of your algorithm.
You could also streamline your fib($n) function like this:
function fib($n)
{
if($n>1)
return fib($n-1) + fib($n-2);
else
return 0;
}
That would reduce the number of conditions you'd need to go through considerably.
** Edited now that I re-read the question **
If you really want to print as you go, use the output buffer. at the start use:
ob_start();
and after all execution, use
ob_flush();
flush();
also you can increase your timeout with
set_time_limit(300); //the value is seconds... so this is 5 minutes.
I know I can do this easily by converting the IP addresses to decimal notation first using PHP built in functions like up2long and long2ip. I just want to be able to do the same using the standard IP address notation as an exercise.
The problem I am thinking goes like this: Given an starting IP address, say 192.168.1.100, and an ending IP address, say 201.130.22.10. Make the program that prints all the address numbers in that range (192.168.1.100, 192.168.1.101, … , 201.130.22.9, 201.130.22.10).
I was thinking that maybe the way to go would be to make a nested for loop inside a while condition until the first octet of the starting address matches the first octet of the ending address. Then execute the same block of code for the second octet and so on until the program reaches the ending address and finished.
I just started learning to program recently so it is quite possible that my of thinking and or writing code is far from elegant. If you were to this, how would you do it?
Something like this:
<?php
// works only for valid range
$start_ip = '10.0.0.1';
$end_ip = '10.0.20.1';
$start_arr = explode('.',$start_ip);
$end_arr = explode('.',$end_ip);
while($start_arr <= $end_arr)
{
echo implode('.',$start_arr) . '<br>';
$start_arr[3]++;
if($start_arr[3] == 256)
{
$start_arr[3] = 0;
$start_arr[2]++;
if($start_arr[2] == 256)
{
$start_arr[2] = 0;
$start_arr[1]++;
if($start_arr[1] == 256)
{
$start_arr[1] = 0;
$start_arr[0]++;
}
}
}
}
?>
This is much less complicated:
<?php
// works only for valid range
$start_ip = ip2long('10.0.0.1');
$end_ip = ip2long('10.0.20.1');
while($start_ip <= $end_ip){
echo long2ip($start_ip).'<br>';
$start_ip++;
}
?>
function getInBetweenIPs($startIP,$endIP){
$subIPS = array();
$start_ip = ip2long($startIP);
$end_ip = ip2long($endIP);
while($start_ip <= $end_ip){
$subIPS[]=long2ip($start_ip);
$start_ip++;
}
return $subIPS;
}
Increment (Add to):
<?php
function ipinc($i): string {
$i = explode(".", $i);
$a = $i[0];
$b = $i[1];
$c = $i[2];
$d = $i[3];
$d++;
if ($d > 255) {
$d = 0;
$c++;
}
if ($c > 255) {
$c = 0;
$b++;
}
if ($b > 255) {
$b = 0;
$a++;
}
if ($a > 255) {
die("IPv4 Range Exceeded");
}
return "$a.$b.$c.$d";
}
?>
Decrement (Take from):
<?php
function ipdec($i) {
$i = explode(".", $i);
$a = $i[0];
$b = $i[1];
$c = $i[2];
$d = $i[3];
$d--;
if ($d < 0) {
$d = 255;
$c--;
}
if ($c < 0) {
$c = 255;
$b--;
}
if ($b < 0) {
$b = 255;
$a--;
}
if ($a < 0) {
die("IPv4 Range Exceeded");
}
return "$a.$b.$c.$d";
}
?>
To test both functions, you can write a for loop to generate approximately 16 million IP addresses back and forth, you can pipe the output to a file and store the results that way.
<?php
require 'function.ipinc.php';
require 'function.ipdec.php';
print("Increment:\n");
for ($i = 0, $ip = "100.0.0.0"; $i <= 16777215; $i++) {
print("$ip\n");
$ip = ipinc($ip);
}
print("----------\n");
print("Decrement:\n");
for ($i = 0, $ip = "100.255.255.255"; $i <= 16777215; $i++) {
print("$ip\n");
$ip = ipdec($ip);
}
print("----------\n");
die("Finished!\n");
?>
If you don't like assigning values through variable declarations, modify the functions so you can use pass by reference instead.