Simple PHP Question

LPL

LPL

Permabanned
Joined
20 Jul 2008
Posts
439
PHP:
<select class="input" name="day">
  <?php
    $day = 1;
    while ($day <= 31) {
    echo "<option value=" . $day . ">" . $day . "</option>";
    $day = $day + 1;
    }
  ?>
</select>

How do I set a default value for the option?
ie when the page loads it doesn't start at 1 but whatever number I set.

I tried option selected or value selected and both just set the default displayed number to 31
 
Code:
selected="selected"

is the html. so you just need to implement that in your php. :)

PHP:
echo '<option value="' . $day . '"';
if($day == 5) echo ' selected="selected"'; //edit this to whatever you want
echo '>' . $day . '</option>';
 
I want the counter to go from 1-31 and then have the default value of something from a database.

I tried the above last night and still it defaults to 31
 
works fine here....

http://www.marc2003.ukfsn.org/test.php

full code....

PHP:
<?php
echo '<select name="day">';
$day = 1;
while ($day <= 31) {
	echo '<option value="' . $day . '"';
	if($day == 5) echo ' selected="selected"'; //edit this to whatever you want
	echo '>' . $day . '</option>';
	$day++;
}
echo '</select>';
?>
 
why are you not using a for loop???

PHP:
<select name="day">
<?php
for($i = 1; $i <= 31; $i++)
    echo '<option value="'.$day.'"'.($day == 5 ? ' selected="selected"' : '').'>'.$day.'</option>';
?>
</select>
 
why are you not using a for loop???

PHP:
<select name="day">
<?php
for($i = 1; $i <= 31; $i++)
    echo '<option value="'.$day.'"'.($day == 5 ? ' selected="selected"' : '').'>'.$day.'</option>';
?>
</select>

does it matter? both get same result. it sounds like he is a novice and for loops take a little bit more understanding
 
a for loop is hardly rocket science.

understanding a for loop will lead to understanding a foreach loop.

using less code across a project means that it will be easier to see what is doing what.

Not sure that being a novice means you can't learn stuff or use good practice.
 
it's personal preference, and is nit picking.

Code:
foreach (range(1, 31) as $day) {
  echo '<option value="'.$day.'"'.($day == 5 ? ' selected="selected"' : '').'>'.$day.'</option>';
}
 
a for loop is hardly rocket science.

understanding a for loop will lead to understanding a foreach loop.

using less code across a project means that it will be easier to see what is doing what.

Not sure that being a novice means you can't learn stuff or use good practice.

agreed.
 
a for loop is hardly rocket science.

understanding a for loop will lead to understanding a foreach loop.

using less code across a project means that it will be easier to see what is doing what.

Not sure that being a novice means you can't learn stuff or use good practice.

There's teaching a novice something and coming across like you're trying to belittle someone with less knowledge than yourself.
 
not sure where in my posts I belittled anyone. I just merely suggested a for loop and posted the accompanying code.

A for loop to me is the best way to do it, with the least amount of code.

Maybe if I'd posted in lolspeak and thrown the term n00b about then I'd understand the criticism.
 
Back
Top Bottom