Jump to content

Changing default select position due to MySQL result...


ActaNonVerba1

Recommended Posts

Hey guys.

Me again...

 

Essentially what i am doing is pulling data from a MySQL database about the number of thumbnails on a page.

The user can then change this using a <select> dropdown menu.

 

How ever, i want the <select> to default to the amount already specified by the Database.

I know i can do this by inserting a Selected attribute to one of the <options> but what is the best way of doing this?

 

Heres my code..

$NumberOfThumbnails = mysql_result($data, 0,"NumberOfThumbnails");
<select name="numberofthumbnails"> 
    	<option value="0">None</option>
        <option value="2">2</option>
        <option value="4">4</option>
        <option value="6">6</option>
        <option value="8">8</option>
        <option value="10">10</option>
        <option value="12">12</option>
        <option value="14">14</option>
        <option value="16">16</option>
        <option value="18">18</option>
        <option value="20">20</option>
        <option value="22">22</option>
        <option value="24">24</option>
        <option value="25">25</option>
        <option value="26">26</option>
        <option value="27">27</option>
        <option value="28">28</option>
    </select>

 

Thanks - Danny

 

Link to comment
Share on other sites

<?php

$selectedNoOfThumbs = mysql_result($data, 0,"NumberOfThumbnails");
$thumbOptionsList = array(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 25, 26, 27, 28);
$thumbOptionsHTML = '';
foreach($thumbOptionsList as $thumbNo)
{
    $selected = ($thumbNo==$selectedNoOfThumbs) ? ' selected="selected"' : '';
    $thumbOptionsHTML .= "    <option value=\"{$thumbNo}\"{$selected}>{$thumbNo}</option>\n";
}

?>
<select name="numberofthumbnails"> 
    <?php echo $thumbOptionsHTML; ?>
</select>

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.