I was able to opt to out of a hard coded select of form values from 50,000 to 75,000,000 in increments of 100000.
<select name="<?php echo $search_parameter; ?>" id="<?php echo $search_parameter; ?>" class="form-control">
<option value=""><?php echo __( $search_labels[$i], 'tt' ); ?></option>
<?php for( $j = 50000; $j <= 75000000; $j += 50000 ) : ?>
<option value="<?php echo $j; ?>"><?php echo number_format( $j ); ?></option>
<?php endfor; ?>
</select>
This saved alot of time. I'd like to further customize this but really have no where to start outside of hard-coding this which seems like a step backwards?
How would I have PHP automate different incrementals based on value of $j thresholds? Is this even possible or am I asking too much?
50000 - 500000 - 50k increments
500001 - 1000000 - 100k increments
1000001 - 5000000 - 500k increments
5000001 - 10000000 - 1 million increments
10m+ 10 million increments
This is a very simple approach, that nevertheless should fit for you:
<?php
$inc = 50000;
for( $j = 50000; $j <= 75000000; $j += $inc) : ?>
<option value="<?php echo $j; ?>"><?php echo number_format( $j ); ?></option>
<?php
if ($j>10000000) {$inc=10000000;}
elseif ($j>5000000) {$inc=1000000;}
elseif //and so on...
endfor; ?>
Note that we're comparing "from the top", so that only a small number of comparisons/assignments should be needed.
Another possibility would be to define the thresholds and increments in an additional array and evaluate them in each loop.
Edit: Added two missing ;s.
Related
I am looking for a way to populate an option value.
<?php for ($i = 1; $i <= 300; $i++) : ?>
<option value="<?php echo $i; ?>">1.<?php print $i; ?> AVAX</option>
<?php endfor; ?>
I want for the above code to work something like this. 1.00 - 1.99 after reaching 1.99 I want it to navigate to 2.00 - 2.99 and so forth and onward until the numbers run out. How can I achieve that? The above result returns only 1.300
You can set the increment to whatever you want
<?php for ($i = 1; $i <= 300; $i+=0.01) : ?>
<option value="<?php echo number_format($i, 2, '.', ''); ?>"><?php echo number_format($i, 2, '.', ''); ?> AVAX</option>
<?php endfor; ?>
demo
What do you mean by until numbers run out?
You do realize that numbers are infinite.
Therefore, I can only assume that you want the options to have the series:
1.01, 1.02.... 1.99, 2.00, 2.01, 2.02, .... until 10.00.
If 10 is not the highest number you want to reach, then you can change the end limit of $i in the following code:
<?php
for ($i = 1; $i <= 10; $i++) {
for ($j = 0; $j <= 99; $j++) {
$num = "$i." . sprintf('%02d',$j); // 1.01, 1.02, ...
echo '<option value="' . $num . '">AVAX</option>';
}
}
?>
I have a weather json from where I get a wether forecast for 8 days. My problem is that I want to show the result in a table of 2 rows of 4 days each.
I managed to do it but in a very odd way that was pure begginer's luck during a trial-error-trial attempt :-) I don't even understand quite well why it is splitting the result in 2 rows... but it works!!! The only thing I could not do was to include a "hr" line between the 2 rows, to make the table easier to read.
You can see the result here http://www.meteocaldas.com/previsao_ds.php
With current code I am displaying each day forecasted values inside the same "td" in different lines using "br". I have been reading that it is not correct to use "br" inside "td" so I am not sure that I am doing the righ thing. Wouldn't it be better to use a table with 4 columns (one for each day) and have different rows for each of the values?
Is there any way to rewrite this code to make it more efficient and look less "childish"? :-) Thanks in advance!
<?php
(...)
$decoded = json_decode($rawData, true);
?>
<table>
<?php for($k=0;$k<8;$k++){
$dailyvalue = $decoded['daily']['data'][$k];
$dailyTime = $dailyvalue['time'];
$dailyIcon = $dailyvalue['icon'];
$dailyTempMax = round($dailyvalue['temperatureMax'],0);
$dailyTempMin = round($dailyvalue['temperatureMin'],0);
(...)
?>
<!-- table for 8 day's forecast (2 rows/4 days each) -->
<td>
<?php echo strftime("%a %d",$dailyTime) ?>
<br>
<?php echo '<img src="path/'.$dailyIcon.'.png">' ?>
<br>
<?php echo $dailyTempMin.'º' ?> </span>
<br>
<?php echo $dailyTempMax.'º' ?></span>
<br>
(...)
<?php if ($k == 3) {
echo '</td></tr>';
} ?>
<?php
}
?>
</td></tr><table>
As per your comment-request, here's some sample-pseudo code, based on your code above:
<?php
(...)
$decoded = json_decode($rawData, true);
for($k=0;$k<8;$k++){
$dailyvalue = $decoded['daily']['data'][$k];
$dailyTime[$k] = $dailyvalue['time'];
$dailyIcon[$k] = $dailyvalue['icon'];
$dailyTempMax[$k] = round($dailyvalue['temperatureMax'],0);
$dailyTempMin[$k] = round($dailyvalue['temperatureMin'],0);
}
?>
<table>
<?php
echo "<tr>";
for($k = 0; $k <= 3; $k++){
echo "<td>".strftime("%a %d",$dailyTime[$k])."</td>"; // this will make the "datetime" row
}
echo "</tr><tr>";
for($k = 0; $k <= 3; $k++){
echo "<td><img src=path/".$dailyIcon[$k].".png></td>"; // this will make the "icon" row
}
echo "</tr><tr>";
for($k = 0; $k <= 3; $k++){
echo "<td>".$dailyTempMin[$k]."º</td>"; // this will make the "MinTemp" row
}
echo "</tr><tr>";
for($k = 0; $k <= 3; $k++){
echo "<td>".$dailyTempMax[$k]."º</td>"; // this will make the "MaxTemp" row
}
echo "</tr>";
// put stuff you want between the tables here
echo "<tr>";
for($k = 4; $k <= 7; $k++){
// proceed to do the same as above
Mind you, there are further ways you can reduce the screen clutter (like moving the table-drawing for loops into a function), but this is the general gist of it
Why don't you simply stack two different table on each other ?
for ($x = 0.01;
$x <= 0.99;
$x++) {?>
<option value="<?php echo $x;?>"><?php echo $x;?></option>
<?php
} ?>
This does not work.
I am trying to get a decimal loop from .01 to .99
The amount of time I have spent on this I could have just typed it out manually :)
You just have to change this:
$x++ //Increments the value by 1
to this:
$x = $x + 0.01 //Increments the value by 0.01
Modify your code this way,
for ($x = 0.01; $x <= 0.99; $x = $x + 0.01) {?>
<option value="<?php echo $x;?>"><?php echo $x;?></option>
<?php
} ?>
You can define the incremental value as shown above
Try this:
for ($x = 0.01; $x <= 0.99; $x++0.1)
Initially, x = 0.01.
While incrementing (x++), x = 1.01.
So your condition fails second time, since 1.01 is greater than 0.99.
Hope you can figure it on your own on how to fix.
This code will divide the score until it reaches the number 5.
The $rows[score] is equal to 6600 in the database.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 5) {
echo $i = $i /2;
echo "<br>";
}
?>
This is what my browser outputs:
3300
1650
825
412.5
206.25
103.125
51.5625
25.78125
12.890625
6.4453125
3.22265625
I don't understand why the browser output the last 3.22 - how do I stop the loop from echo out the last one that is less than 5??
Nothing wrong here the last value you get is from 6.4453125 / 2 = 3.22265625 since 6.4453125 still greater than 5
because 6 is higher than 5? so it does one more loop making $i 3.2 where the loop stops
If ($i<5) it wont go into the loop but there is no way to know until you check. $i = 6.4453125 the last time it checks, so it goes into the loop and it divides it by 2, which makes it less than 5 so it doesn't go into the loop again and stops.
I found the way to answer my own question so 3,22 will not be viewed on the page.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 5) {
$i = $i /2;
if($i >= 5) {
echo $i;
echo "<br>";
}
}
?>
Since you're dividing by two immediately before displaying the result, you want to stop your loop when $i >= (5*2) i.e. $i >= 10, not 5.
<?php
$i = $rows[score]; //score is 6600 in the database
while ($i >= 10) {
echo $i = $i /2;
echo "<br>";
}
?>
This gives:
3300
1650
825
412.5
206.25
103.125
51.5625
25.78125
12.890625
6.4453125
I have a standard form array in PHP like this. I need to account for up to 10 hours of labor in 15 minute increments with 10 as a value for every 15 min or 40 per hour. Is it possible to automate this into some sort of array with PHP instead of hardcoding each of these values? It just seems like there should be a better way and I have no idea how to start?
<select size="1" name="labor" id="labor">
<option value="80">2 Hours</option>
<option value="70">1 Hour 45 min.</option>
<option value="60">1 Hour 30 min.</option>
<option value="50">1 Hour 15 min.</option>
<option value="40">1 Hour</option>
<option value="30">45 Minutes</option>
<option value="20">30 Minutes</option>
<option value="10">15 Minutes</option>
</select>
Probably easiest to hardcode the solution. I think this is one of those situations where it is OK as #Wrikken mentioned, as it makes the code very clean and easy to maintain (Imagine coming back to this in a year or two). In addition this situation can also be handled very well with a database table.
First use an array to store you values and descriptions:
$list = array();
$list['10'] = '15 Minutes';
....
Then loop through the entire array to generate your dropdown:
<select size="1" name="labor" id="labor">
<?php
foreach($list as $value => $desc){
$option = "<option value=$value>" . $desc . "</option>";
echo $option;
}
?>
</select>
You need two values, the 10-units increment and the user-displayed value. Run a for loop over $i (or some other variable) from 1 to 40 and calculate your values inside the loop:
10-increment is easy, just multiply $i by ten.
For the displayed value, multiply by 15 to get the total amount of minutes. Then transform that into hours and minutes using the modulo operator (which gives you the minutes) and standard divison (to get the hours).
This should do it. Change the start number of $i to how many total minutes the dropdown should contain. It´s now set to 750 minutes.
echo '<select>';
for ($i = 750; $i >= 15; $i -= 15) {
$hours = $i / 60;
$min = $i % 60;
echo '<option>';
if ($hours >= 1)
echo floor($hours)." Hours ";
if ($min > 1)
echo $min." Minutes";
echo '</option>';
}
echo '</select>';
NOTE: This code is not perfect, the start number needs to be evenly divided with 15 to generate your desired result, (however, it works).
Something like:
<select size="1" name="labor" id="labor">
<?php
for ($x = 1; $x < 11; $x++) {
echo '<option value="';
echo $x*10;
echo '">';
echo $x*15;
echo " Minutes</option>";
}
?>
</select>
Throw in an if statement there to seperate it so that once (x/10*15)>60 it starts carrying over into hours instead of having 75/90/105/120 minutes.