Jump to content

Best way to do this?


Kenny Pollock

Recommended Posts

I have a value, let's say "4" retrieved from a row in the database.

 

I want to have a drop down that's like this:

<option value="4">statusname from the status table for the id of 4</option>

<option value="1">another option that's NOT 4 from the status table</option>

<option value="2">another option that's NOT 4 from the status table</option>

<option value="3">another option that's NOT 4 from the status table</option>

 

So that the current option is selected, and all others are in the drop down and can be selected

Link to comment
Share on other sites

This is an example of some code I used for selecting the month.  Pull their selection from the database and then assign it to the variable of "$month_of_year"

 

<select size="1" name="billing_renew_month">
 <option<? if ($month_of_year == 1) echo " SELECTED"; ?> value="1">January</option>
 <option<? if ($month_of_year == 2) echo " SELECTED"; ?> value="2">February</option>
 <option<? if ($month_of_year == 3) echo " SELECTED"; ?> value="3">March</option>
 <option<? if ($month_of_year == 4) echo " SELECTED"; ?> value="4">April</option>
 <option<? if ($month_of_year == 5) echo " SELECTED"; ?> value="5">May</option>
 <option<? if ($month_of_year == 6) echo " SELECTED"; ?> value="6">June</option>
 <option<? if ($month_of_year == 7) echo " SELECTED"; ?> value="7">July</option>
 <option<? if ($month_of_year ==  echo " SELECTED"; ?> value="8">August</option>
 <option<? if ($month_of_year == 9) echo " SELECTED"; ?> value="9">September</option>
 <option<? if ($month_of_year == 10) echo " SELECTED"; ?> value="10">Ocotober</option>
 <option<? if ($month_of_year == 11) echo " SELECTED"; ?> value="11">November</option>
 <option<? if ($month_of_year == 12) echo " SELECTED"; ?> value="12">December</option>
</select>

Link to comment
Share on other sites

Another member of another forum helped me figure it out...

 

function generate_options($option)
{
$sql = "SELECT options FROM option_table";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result))
    {
    print("<option value=\"$row[option]\" ");
    if($row[option] == $option)
        {
        print("selected ");
        }
    print(">OPTION $row[option]</option>\n");
    }
}

Link to comment
Share on other sites

Use the ? notation to make things prettier...

 


$cur = 3;
echo '<select name="blah">' . "\n";
for ($i = 1; $i <= 4; $i++) {
  echo '<option value="'.$i.'"'.($i == $cur ? ' selected="1"' : '').'>'.$i.'</option>' . "\n";
}
echo '</select>' . "\n";

Link to comment
Share on other sites

Never use MS for web-standard references... always W3.

 

According to XHTML1.0/HTML4.01, all attributes must have a value, and all tags must have an escape (hence <br />)

 

W3 specifically states selected="selected" here

http://www.w3schools.com/tags/tag_option.asp

 

But as long as you have a value (even 0 or false works) it will interpret. It simply checks for the existence of the attribute, not the value.

 

Simply putting SELECTED will work, but it will not follow HTML4.01 standards.

Link to comment
Share on other sites

I don't understand. What reason?

 

Try parsing html using MS' 'standards' and you'll find you have a bloated pile of junk. Parsing W3 complaint HTML is very easy and efficient.

 

When your exceptions to rules have exceptions themselves, you might as well just quit.

Link to comment
Share on other sites

Yes, and those browsers have to parse code. If everyone could follow W3 html standards, our browsers would run leaps more efficient.

 

I've had to program many php scripts that grabs the source from a webpage, and parses certain elements. If those pages aren't HTML4.01 compliant, I have to add an extreme amount of exceptions

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.