Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Select Option and Database Query

    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.

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    I'm afraid I found the description of the problem a little confusing but I think this is what you're after:
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
     <head>
    </head>
    <body>
    <table><tr>
    <td><select name='Category'>
    <option value="-1">--Select--</option>
    <? 
    $s_array 
    = array(1=>'Test 1','Test 2','Test 3');
    foreach(
    $s_array as $value=>$text){
     echo 
    '<option value="'.$value.'"';
     if(
    array_key_exists(@$onenav->Category$s_array)){
      echo 
    ' selected';
     }
     echo 
    '>'.$text.'</option>';
    }
    ?>
    </select> <br />
    </td>  
    </tr></table>
     
    </body>
    </html>
    If not, or you need any explanations let me know.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •