0

Want to make a pulldown for hours and I want it to be on the hour now (it is now 22:40 so the pulldown should be on 22).

Here is the code:

<?php
$hour_now= date(G);
$minute_now=date(i);
?>
          <form action="index.php" method="post" id="schedule_form">
<p>
                    <label>Hour</label>
                    <select name="send_hour">
<?php                    	
for($i = 0; $i < 24; $i++){
    echo '<option value="' . $i .'"'. $select. '>'.$i.'</option>';
if ($si !== $hour_now)
{
$select = "";
}else{
$select = "selected";
}
       
    }
echo '</select>';
?>

Why isn't the pulldown on 22?

Any help is appreciatedl.

Greets,

Ivan.

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by tuukie
0

You're echoing before the thing is given a value and also you were using $si instead of $i:

<?php
$hour_now= date("G");
$minute_now=date("i");
?>
<form action="index.php" method="post" id="schedule_form">
   <label for="send_hour">Hour</label>
   <select name="send_hour">
<?php                    	
for($i = 0; $i < 24; $i++){
   if ($i !== $hour_now){
      $select = "";
   }else{
      $select = ' selected = "selected"';
   }
   echo "<option value=\"$i\"{$select}>$i</option>";
}
?>
</select>
...
</form>
0

Ardav, thanx for the quick reply. I get your comments that I am echoing to soon, I missed the typo so also thanx for that, but when I run your code, the pulldown doesn't show the right selected hour.

So very close, but not there yet.

Greets,

Ivan.

0
<?php
$hour_now= date("G");
$minute_now=date("i");
?>
<form action="index.php" method="post" id="schedule_form">
   <label for="send_hour">Hour</label>
   <select name="send_hour">
<?php                    	
for($i = 0; $i < 24; $i++){
   if ($i != $hour_now){
      $select = "";
   }else{
      $select = ' selected = "selected"';
   }
   echo "<option value=\"$i\"{$select}>$i</option>";
}
?>
</select>
...
</form>

Perhaps a data comparison error - $hour_now may be a string and you're comparing it to an integer ($i). Therefore do a simple comparison (!=) as opposed to a more in-depth comparison that includes datatype (!==).

Perhaps I'm way wrong here!

0

That was it!! != instead of !==

Thanx a million I was looking for hours and hours and I am (as you probably guessed) new to this!

Thanx,

Ivan.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.