I have set to constants, a start year and an end year,
so i have built a while loop on the condition that
if the start year is < than the current year increment until true.
the problem i have is that instead of it going up like this:
1999,2000,2001,2002,2003,2004
it goes like:
1999,2001,2003,2005,2007,2009
Here is my code:
function yearCount()
{
$yearBegin = START_YEAR;
$yearCurrent = CURRENT_YEAR;
while($yearBegin < $yearCurrent){
echo "<option value=\"".$yearBegin++."\">".$yearBegin++."</option>";
}
}
any ideas would be highly appreciated.
You are incrementing the value twice:
echo "<option value=\"".$yearBegin++."\">".$yearBegin++."</option>";
Each $yearBegin++ increments it by one.
Use a for loop instead:
for ($yearBegin = START_YEAR; $yearBegin < CURRENT_YEAR; $yearBegin++)
{
echo "<option value=\"".$yearBegin."\">".$yearBegin."</option>";
}
Using a for loop is usually the way to do this,
for($year=START_YEAR;$year<=CURRENT_YEAR;$year++)
{
//User the $year here
}
your problem with the code is that your calling $yearBegin++ 2 times within the while loop, causing it to increment twice.
using the for loop is much cleaner then as incrementing is done within the expression for you
function yearCount()
{
$yearBegin = START_YEAR;
$yearCurrent = CURRENT_YEAR;
while($yearBegin < $yearCurrent){
$this_year = $yearBegin++;
echo "<option value=\"".$this_year."\">".$this_year."</option>";
}
}
use only one time ++ , increment
echo "<option value=\"".$yearBegin."\">".$yearBegin++."</option>";
You increment $yearBegin twice, one time in the value part, one time in the display part ...
You need to change it so it only increments once
You increment it twice, once setting it as a value, and the second time displaying it in option tag
Related
My code is:
$count=0;
foreach( $List AS $email){
$data = my_function($url);
$count++;
if(filter_var($email , FILTER_VALIDATE_EMAIL)){
...............
my_function runs a simple cURL for an url and returns session_id and api_identity as results. If I run it in the loop for each email in the list, my code is very slow and takes time to show results.
now
$data = my_function($url);
Is applied for each email.
My question:
Is there any way to apply $data = my_function($url); for every 10th mail in the whole list?
If I'm not misunderstanding your question, you want to do something every nth iteration, which can be accomplished using modulo operators:
$count = 1;
foreach ($List as $email)
{
if ($count % 10 == 0) $data = my_function($url);
$count++;
...
}
If you don't know what x % y does, it simply returns the remainder of the expression x/y. A simple trick is therefore to use the fact that when evaluating x % n for different values x = 1, 2, 3..., the same result will repeat every nth time.
Note that depending on what your counter starts at, you will get a different start-email. If you keep it 0 as you have now, the function will be called for the 1st, 11th, 21st, .... emails. If you change it to 1 as I did, the first email for which the function will be called is the 10th one.
You can use https://stackoverflow.com/a/44883380/7095134 this answer or by using counter variable you can achieve that.
e.g:
counter=0;
for(i=0; i<=100; i++)
{
if(counter==10){ counter=0; call_your_function(); }
else { do the rest of work; counter++ ;}
}
Hi this is a table converting celsius to kelvin and fahrenheit, I am just wondering why my code does not loop :( it only displays the first two lines and stops. Thank you!
<?php
$celsius = 100;
$stop_kelvin = 0;
print '<table>';
print '<tr><th>Degrees Celsius(C)</th><th>Kelvin(K)</th><th>Degrees Fahrenheit(F)</th></tr>';
while ($kelvin <= $stop_kelvin) {
$fahr = ($celsius*1.8) + 32;
$kelvin = $celsius + 273;
print"<tr><td>$celsius</td><td>$kelvin</td><td>$fahr</td></tr>";
$fahr += 1;
}
print '</table>';
?>
In your code, $kelvin variable is not initialized. Also, please re-think your loop logic.
First of all you havent initialized $kelvin; because of which $kelvin is taking some random values.
After first loop the value of $kelvin becomes 373 and in your loop it is $kelvin <= $stop_kelvin means the condition is false and it jumps out of the loop
$files = glob("../../users/$username/posts/*");
foreach( $files as $eachfile )
{
echo basename($eachfile); //this is current basename
echo "?"; //previous basename how will be?
echo "?"; //next basename how will be?
}
How I can echo the next and the previous basename of the loop, on the same loop, i mean without the php do again the foreach as you can see in the example.
maybe you could do something like:
$files = glob("../../users/$username/posts/*");
for($i = 1; $i < count($files) - 1;$i++)
{
echo basename($files[$i]); //this is current basename
echo basename($files[$i - 1]);
echo basename($files[$i + 1]);
}
Simplify first
To get the loop right, it could be easier to see the three values as looking back only, not ahead:
Instead of "need the previous and next basename", you can simplify the problem as "need the previous two basenames".
Save old values
Now, you can save the previous two basenames in two variables.
The current basename can be saved at the end of the loop as the previous one for the next loop.
The previous basename will be the second previous one in next iteration.
Rename to make next part easier
The simplification was useful for this part, but it may be easier for the later parts of the algorithm to have previous and next basename. So, if that helps, you could now rename the variables curent, previous and previous2 to next, curent, and previous.
In pseudocode (I don't know php):
prev = files[0]
curr = files[1]
next = files[2]
for (i = 2...files.length) {
next = files[i]
echo prev
echo curr
echo next
prev = curr
curr = next
}
I know I have done this in Javascript once, but how can I make it in PHP?
Basically I want to do this:
if (empty($counter)){
$counter = 1;
}else{
"plus one to $counter" ($counter++?)
}
But it didn't work when I tried. How would I go about doing this?
Thank you :)
EDIT: This is so I can do:
if ($counter == 10){
echo("Counter is 10!");
}
EDIT:
This is all in a "while()" so that I can count how many times it goes on, because LIMIT will not work for the query I'm currently doing.
why the extra if into the while?
i would do this:
$counter = 0;
while(...)
{
(...)
$counter++;
}
echo $counter;
To increment a given value, attach the increment operator ++ to your integer variable and place it within your while loop directly, without using a conditional expression to check if the variable is set or not.
$counter = 1;
while(...){
echo "plus one to $counter";
$counter++;
}
If your counter is used to determine how many times your code is to be executed then you can place the condtion within your while() expression:
while($counter < 10){
echo "plus one to $counter";
$counter++;
}
echo("Counter is $counter!"); // Outputs: Counter is 10!
You're going to have to learn the basics of how PHP outputs to the screen and the other controls along with it.
if (empty($counter)){
$counter = 1;
}else{
echo 'plus one to $counter';
$counter++;
}
Something along those lines will work for you.
PHP is pretty flexible with what you throw at it. Just remember, statements need a semicolon at the end, and if you want to output to the screen, (in the beginning) you'll be relying on echo statements.
Also, when dealing with echo statements, notice the difference between single quotes and double quotes. Double quotes will process any contained variables:
$counter = 3;
echo "plus one to $counter"; // output: plus one to 3
echo 'plus one to $counter'; // output: plus one to $counter
I have a while loop that contains an if statement. The while loop works fine but when I run the following if statement for each value passed through the while loop, and the if statement returns true, the script hangs up and I get the 30 second maximum execution time error.
I am not sure if it is creating an infinite loop or what. Can anyone spot the problem?
$size = count($_POST['itemname']);
// start a loop in order to update each record
$i = 0;
while ($i < $size) {
// define each variable
$itemname= $_POST['itemname'][$i];
$id = $_POST["id"][$i];
if(preg_match('/[A-Za-z]/',$itemname)) {
echo("has words");
} else {
//update code here
}
}
You never increment $i, that is what is hanging it up as it will always be < $size
while ($i < $size) { // changed this to >
// define each variable
$itemname= $_POST['itemname'][$i];
$id = $_POST["id"][$i];
$i++; // increment $i
You never increment $i. Try a for loop instead; they're a little more explicit.
You need to increment $i somewhere outside of the if statement.
you never change $i in the while loop
you have to increment $i for each loop or else if its true once it will always pass
add $i++ between the last 2
}
}
so it looks
}
$i++;
}