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.

Recommended Answers

All 4 Replies

Member Avatar for diafol

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>

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.

Member Avatar for diafol
<?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!

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.