Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm new to PHP and would like to create a drop down box which displays every year from xxxx to the present year, I can handle the implementation into the drop down box but would like to know if I can have php automatically create an array with all of these years in to save typing them in manually.
You can use the function range (see the range docs) to accomplish this
$years = range(2013, 2050);
The optional third parameter is the step (standard is 1).
If you want to use an array:
$array_years = range($start_year, $end_year, $steps); //$steps is optional
foreach($array_years AS $years) {
//output options
}
Documentation here: http://php.net/manual/en/function.range.php
Alternative: use a for loop (see other answers for example code)
You can also use simple for loop to generate it.
echo "<select>";
for($i = from; $i <= to; $i++){
echo "<option>" . $i . "</option>;
}
echo "</select>";
<select>
<?php
$startYear = "";
$endYear = date("Y");
while($startYear != $endYear){
echo '<option>'.$startYear.'</option>';
$startYear++;
}
?>
</select>
should do it
<select name="year">
<?php
$start = 1960;
for($i=$start; $i < date("Y"); $i++) {
echo '<option>' . $i . '</option>';
}
?>
</select>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
In my html/php file I have the following tag:
<select name="seitJ">
<option value=20 <?php if ( $_GET["bisJ"]==20 ) echo("selected"); ?> >2020</option>
<option value=19 <?php if ( $_GET["bisJ"]==19 ) echo("selected"); ?> >2019</option>
<option value=18 <?php if ( $_GET["bisJ"]==18 ) echo("selected"); ?> >2018</option>
</select>
What I want is a php code which updates this code every beginning of a new year automatically. My problem are NOT the date- functions but how to insert the php code contained in the tags. I think I would have to include php code inside php code which I know doesn’t work.
This should do what you want:
Essentially I am setting the year to the current year (2020), setting the target year to the current year minus 4 years (2016) and I am looping through, reducing the year by 1 each iteration until we reach our target year.
<select name="seitJ">
<?php
$year = date('Y');
$target = $year - 4;
for($i = $year; $i > $target; $i--) {
$digits = substr($i, 2);
$selected = ($_GET['bisJ'] == $digits ? 'selected' : '');
echo '<option value="' . $digits . '" ' . $selected . '>' . $i . '</option>';
}
?>
</select>
Resources:
substr()
for()
You can try this
<select name="seitJ">
<?php
for($i = 2000; $i < date('Y'); $i++){
echo "<option value=<?php echo $i; (date('Y') == $i) ? echo selected : ''?> >$i</option>";
}
?>
</select>
The date('Y') will get you the current year so it will update automaticly.
And keep in mind that it's a good practice to create a loop instead of writing the values one by one.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to loop through $i which starts from 790000000 and echo every 100000 up to 799999999:
790000000
790100000
790200000
790300000
790400000
...
I tried this code but it didn't work:
for ($i=790000000; $i<=800000000; $i+100000) {
echo $i . '<br>';
}
For loop wrong code, you are missing actual code update as $i+100000 does not update variable. Use $i += 100000 instead.
// Here is problem in your code
for ($i=790000000; $i<=800000000; $i += 100000) {
echo $i . '<br>';
}
Update your loop using += instead of i.
Only using + will not increment your $i variable value.
for ($i=790000000; $i<=800000000; $i+=100000) {
echo $i . '<br>';
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have using this code between echo "";
<?php
for ($i=0; $i<$tot; $i++)
{
$rt = date ( "d-m-Y", $y[$i] ) ;
}
?>
Please specify how to implement this code between echo "";
<?php
$output = 'for ($i=0; $i<$tot; $i++){ ';
$output .= '$rt = date ( \'d-m-Y\', $y[$i] );';
$output .= '}';
echo $output;
?>
For readability I separated it into 4 lines, but you can see how it could easily be refactored into one echo statement.
The key idea here is that by declaring strings with ' instead of ", php will not interpret the $ signs as variables. Additionally, in order to include single quotes within the string itself, a backslash escape character is required as in \'d-m-Y\'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What's the easiest way to find the first zero bit with PHP?
For example, say I have an integer 47 which is 111101, how can I find out that the 5th bit is the first unset bit? This needs to work to cater for different integers.
$value = 47;
$i = $j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
$i++;
}
echo "bit $i is 0";
If you want to eliminate the use of $i as a counter, you can do a little bit of extra math:
$value = 47;
$j = 1;
while (true) {
if (($value & $j) == 0) {
break;
}
$j = $j << 1;
}
echo "bit ", (log($j) / log(2) + 1), " is 0", PHP_EOL;
The +1 is necessary because you're starting your binary as bit 1 rather than as bit 0
Use decbin to return a string of 0 and 1.
Then, use strpos to find the first 0 caracters.
$str = decbin(47);
$result = strpos($str, '0');
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I store the number 1 2 3 4 as the answer to multiple choice questions in MySQL database,
But I want to display them in a b c d on web page.
I'm using PHP, how can I transform the numbers to letters using PHP?
echo "
<h2>question</h2>
<p>".$row[0]."</p>
<h2>answer</h2>
<p>".$row[1]."</p>";
The row[1] is {1,2,3,4} now, I want it turn to {A,B,C,D}.
Can any one please have a look and tell me how to do it?
It is very easy:
<?php
$row = array(1, 2, 3, 4);
echo "
<h2>question</h2>
<p>".chr(64 + $row[0])."</p>
<h2>answer</h2>
<p>".chr(64 + $row[1])."</p>";
Test it: http://3v4l.org/uojsK
you can define an array first:
$letters = array_combine(range(1,26), range('a', 'z'));
//now just use:
echo $letters[$row[1]];
Create the alphabet array and use the numbers to get the right letter by index (num-1)
$letters = range('a','z');
$num = 3;
var_dump($letters[$num-1]);//"c"
i.e.
$letters = range('a','z');
echo "
<h2>question</h2>
<p>".$letters[$row[0]-1]."</p>
<h2>answer</h2>
<p>".$letters[$row[1]-1]."</p>";
<?php
/// the $row is the array you can get from db
$row = array(1,2,3,4,5);
// Here you can add your option up-to z
$alpha = range('A','E');
$i = 0;
/// Instead of foreach loop you can run while loop
foreach($row as $rows){
echo $alpha[($row[$i]-1)].'<br/>';
$i++;
}
?>