Jump to content

[SOLVED] select box+query from database


drummer101

Recommended Posts

So I've got a date select box, what I'd like to do is query the database for the date, and have the date in the database show up in the date select box (<option selected="selected">) but I can't quite wrap my brain around how to do it, and keep them in numerical order.

 

// Month Select Box

 

echo "<select>";

 

for($m=1; $m<=12; $m=$m+1){

 

echo "<option value=\"$m\" name=\"month\">$m</option>";

 

}

 

echo "</select>";

 

 

 

// Day Select Box

 

echo "<select>";

 

for($d=1; $d<=31; $d=$d+1){

 

echo "<option value=\"$d\" name=\"day\">$d</option>";

 

}

 

echo "</select>";

 

 

 

 

 

// Year Select Box

 

echo "<select>";

 

for($y=2008; $y>=2008; $y=$y-1){

 

echo "<option value=\"$y\" name=\"year\">$y</option>";

 

}

[/php

Link to comment
https://forums.phpfreaks.com/topic/134799-solved-select-boxquery-from-database/
Share on other sites

Assuming that you retrieved your date from the database and split it up into vars...

 

echo "<select>";

   for($m=1; $m<=12; $m=$m+1){

      $selected = ($m == $month)? 'selected' : '';
      echo "<option value=\"$m\" name=\"month\" $selected>$m</option>";

   }

   echo "</select>";

Assuming that you retrieved your date from the database and split it up into vars...

 

echo "<select>";

   for($m=1; $m<=12; $m=$m+1){

      $selected = ($m == $month)? 'selected' : '';
      echo "<option value=\"$m\" name=\"month\" $selected>$m</option>";

   }

   echo "</select>";

Would that not return ALL values as selected?

 

The code I posted above builds the select box and populates it with values.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.