Jump to content

[SOLVED] Select Option and Database Query


gc40

Recommended Posts

I am trying to write a function for an update script I am doing.

 

Currently, there is a table called Category which contains a numeric value of 1-3. The Category is displayed as text output thanks to an array and or if statement.

 

If Category = 1, then Output = Test 1

If Category = 2, then Output = Test 2

If Category = 3, then Output = Test 3

 

Here is what my code currently looks like:

              <td><select name='Category'>

                    <? $s_array = array('Test 1','Test 2','Test 3')?>

                    <option SELECT value='<? if(array_key_exists($onenav->Category, $s_array)) {?><?php echo($onenav->Category);?>'><? echo $s_array[$onenav->Category] ;?><? } ?></option>

                    <option value='1'> Test 1 </option>

                    <option value='2'> Test 2 </option>

                    <option value='3'> Test 3 </option>

                </select> <br />

              </td>

 

You will notice that the array exist and SHOULD list the Category name if the number matches the array, however, it does not. Also, you will notice that the Test 1, Test 2 and Test 3 are listed as select <option> tags. This is because when a user is updating the page, if the current Category is Test 1, and he wishes to update to Test 2, then he can. However, I would like it so that NO Category is repeated twice. If there anything I can do to allow me to have the option selected if it exist? and also, how do I get that array working to list the Category as Text output selecting the option that matches the current database value.

Link to comment
Share on other sites

Hi..

        you can get that via this also..

 

                   <option value='1' <?php if($onenav->Category == "test1") { echo "selected"; } ?>> Test 1 </option>
                    <option value='2' <?php if($onenav->Category == "test2") { echo "selected"; } ?>> Test 2 </option>
                    <option value='3' <?php if($onenav->Category == "test3") { echo "selected"; } ?>> Test 3 </option>

            suppose this would help you.   

Regards,

Vijay

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.