SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast ericdgreene's Avatar
    Join Date
    Nov 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Looking for elegant solution to echo 'selected' option

    So I have code like this

    HTML Code:
    <select name="s" size="20">
      <option value="402">Audi</option>
      <option value="74">BMW</option>
      <option value="201">Ford</option>
      <option value="35">Saab</option>
      etc etc
    </select>
    The list goes on and on. I actually have a value for about 1000 models, not just the make.
    Anyway to echo that my value is selected, in every single option I paste in the following:

    PHP Code:
    <?php if ($_GET['s'] == '74') { echo 'selected'; } ?>
    Then I change the number value in there for each one.

    So I end up with a list like this:

    HTML Code:
    <select name="s" size="20">
      <option value="402" <?php if ($_GET['s'] == '402') { echo 'selected'; } ?>>Audi</option>
      <option value="74" <?php if ($_GET['s'] == '74') { echo 'selected'; } ?>>BMW</option>
      <option value="201" <?php if ($_GET['s'] == '201') { echo 'selected'; } ?>>Ford</option>
      <option value="35" <?php if ($_GET['s'] == '35') { echo 'selected'; } ?>>Saab</option>
    </select>
    This is going to take awhile since I have roughly 1000 models to enter. I've done about 30 and I'm sick of it already. Is there a way to elegantly do this so I'm not changing the number value every time? I wouldn't mind if all I had to paste was a single line of code into every option. It's much worse to have to go back and change the value for each and every one of them..

    Any ideas?

  2. #2
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this output for each one going to be "selected"?

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So, are you storing the key (35) and the make/model (Saab 900) in a database? If not, where are these values coming from?

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using an array include to store make&model

    Why not include an array with your make & model? Then iterate the form?
    This is only a suggestion mind you, and my first post.

    makemodel.list.php:
    Code:
    <?php
    $makemodel = array(1 => 'Audi', 'BMW', 'Ford', 'Saab') ;
    
    
    ?>
    Then, use this as your form?
    Code:
    <?php
    include 'makemodel.list.php' ;
    
    echo '<select name="s" size="20">' ;
    
    foreach ($makemodel as $i => $value) {
       echo "<option value=\"$i\"";
       if ($_REQUEST['s'] == $i) 
          {
              echo " selected" ; 
          }
       echo ">$makemodel[$i]</option>" ;
    }
    
    echo '</select>' ;
    
    ?>

  5. #5
    SitePoint Enthusiast ericdgreene's Avatar
    Join Date
    Nov 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jaynesh View Post
    Is this output for each one going to be "selected"?
    Yes, just echo "selected"

  6. #6
    SitePoint Enthusiast ericdgreene's Avatar
    Join Date
    Nov 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    So, are you storing the key (35) and the make/model (Saab 900) in a database? If not, where are these values coming from?
    No, not in a database. It's all hard coded.

  7. #7
    SitePoint Enthusiast ericdgreene's Avatar
    Join Date
    Nov 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Frenz48 View Post
    Why not include an array with your make & model? Then iterate the form?
    This is only a suggestion mind you, and my first post.

    makemodel.list.php:
    Code:
    <?php
    $makemodel = array(1 => 'Audi', 'BMW', 'Ford', 'Saab') ;
    ?>
    Then, use this as your form?
    Code:
    <?php
    include 'makemodel.list.php' ;
    
    echo '<select name="s" size="20">' ;
    
    foreach ($makemodel as $i => $value) {
       echo "<option value=\"$i\"";
       if ($_REQUEST['s'] == $i) 
          {
              echo " selected" ; 
          }
       echo ">$makemodel[$i]</option>" ;
    }
    echo '</select>' ;
    ?>
    Thanks, I really like your thinking on this. The only thing is, each model has a specific number to it, so I can't just run through each sequentially... Would there be a way to add the specific number within the array somehow? Hmmm well now you have me thinking thanks for the suggestion.

  8. #8
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can indeed make the array as you wish!
    Code:
    <?php
    $makemodel = array(402 => 'Audi', 74 => 'BMW', 201 => 'Ford', 35 => 'Saab') ; // Keep going down this line, 
    ?>

  9. #9
    SitePoint Enthusiast ericdgreene's Avatar
    Join Date
    Nov 2011
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Frenz48 View Post
    You can indeed make the array as you wish!
    Code:
    <?php
    $makemodel = array(402 => 'Audi', 74 => 'BMW', 201 => 'Ford', 35 => 'Saab') ; // Keep going down this line, 
    ?>
    Yes, exactly! Thanks I think I can now put it all together. Very helpful!


Bookmarks

Posting Permissions

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