dcsimg
www.webdeveloper.com
Results 1 to 9 of 9

Thread: How can I simplify this PHP code

  1. #1
    Join Date
    Nov 2015
    Location
    St. Petersburg, Russia
    Posts
    26

    Question How can I simplify this PHP code

    HTML Code:
    <select name="country">
        <option [PHP]<?php if ($val == '') echo 'selected' ?>[/PHP] value="">Country...</option>
        <option [PHP]<?php if ($val == 'Afganistan') echo 'selected' ?>[/PHP] value="Afganistan">Afghanistan</option>
        <option [PHP]<?php if ($val == 'Albania') echo 'selected' ?>[/PHP] value="Albania">Albania</option>
        <option [PHP]<?php if ($val == 'Algeria') echo 'selected' ?>[/PHP] value="Algeria">Algeria</option>
        <option [PHP]<?php if ($val == 'American Samoa') echo 'selected' ?>[/PHP] value="American Samoa">American Samoa</option>
        <option [PHP]<?php if ($val == 'Andorra') echo 'selected' ?>[/PHP] value="Andorra">Andorra</option>
        <option [PHP]<?php if ($val == 'Angola') echo 'selected' ?>[/PHP] value="Angola">Angola</option>
        <option [PHP]<?php if ($val == 'Anguilla') echo 'selected' ?>[/PHP] value="Anguilla">Anguilla</option>
        ....

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    3,459
    Looks like your post is in a bit of distress!
    You wrap the forum tag around the block of code and not the individual elements...
    PHP Code:
    <select name="country">
        <option <?php if ($val == '') echo 'selected' ?> value="">Country...</option>
        <option <?php if ($val == 'Afganistan') echo 'selected' ?> value="Afganistan">Afghanistan</option>
        <option <?php if ($val == 'Albania') echo 'selected' ?> value="Albania">Albania</option>
        <option <?php if ($val == 'Algeria') echo 'selected' ?> value="Algeria">Algeria</option>
        <option <?php if ($val == 'American Samoa') echo 'selected' ?> value="American Samoa">American Samoa</option>
        <option <?php if ($val == 'Andorra') echo 'selected' ?> value="Andorra">Andorra</option>
        <option <?php if ($val == 'Angola') echo 'selected' ?> value="Angola">Angola</option>
        <option <?php if ($val == 'Anguilla') echo 'selected' ?> value="Anguilla">Anguilla</option>
    --> JQuery questions here <-- ... and please remember to wrap any code you have in forum tags:-
    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    3,459
    Not the best way of doing it because I have no idea how you have selected the "selected" option in your script.
    PHP Code:
    <select name="country">
    <?php
    $opts 
    = array( ""=>""
               
    "afganistan"=>"Afganistan"
               
    "albania"=>"Albania",
               
    "algeria"=>"Algeria"
               
    "american_samoa"=>"American Samoa"
               
    );
    $selected_option "albania";
    $out = array();
    foreach(
    $opts as $o=>$opt){
      
    $selected = ( $selected_option == $o ) ? " selected" ""
      
    $out[]    = sprintf("<option value='%s'%s>%s</option>",$o,$selected,$opt[$o]);
    }
    echo 
    $out;
    ?>
    </select>
    If possible, turn the thing in to an array of elements to work with and output the string, this way you will either get an empty select element if something is wrong but the script runs rather than broken HTML and a PHP parser throwing lots of errors.
    --> JQuery questions here <-- ... and please remember to wrap any code you have in forum tags:-
    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Nov 2015
    Location
    St. Petersburg, Russia
    Posts
    26
    ok, 'cause I'm li'l bit stressed to my code.

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    3,459
    Spotted an error

    PHP Code:
    echo $out
    needs to be

    PHP Code:
    echo implodePHP_EOL$out); 
    --> JQuery questions here <-- ... and please remember to wrap any code you have in forum tags:-
    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  6. #6
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    3,459
    Quote Originally Posted by tubc2015 View Post
    ok, 'cause I'm li'l bit stressed to my code.
    Will be AFK for a while, should be plenty of people to jump in with their ideas on how to do this.
    --> JQuery questions here <-- ... and please remember to wrap any code you have in forum tags:-
    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    20,979
    I'd probably skip building the output array, and just do something like:
    PHP Code:
    <?php
    // might want this array defined in a separate include file or even a database query,
    // rather than typing it all out in this file:
    $options = array(
        
    'Afghanistan',
        
    'Albania',
        
    'Algeria',
        
    'American Samoa',
        
    'Andora',
        
    'Angola',
        
    // . . .
        
    'Zambia'
    );
    ?>
    <select name="country">
      <option <?php if ($val == '') echo 'selected' ?> value="">Country...</option>
    <?php foreach($options as $option)
    {
        
    printf(
            
    "  <option %svalue='%s'>%s</option>\n",
            
    $val == $option 'selected ' '',
            
    $option,
            
    $option
        
    );
    }
    ?>
    </select>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

  8. #8
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    3,459
    I chose
    Code:
    $opts = array( ""=>"", 
               "afganistan"=>"Afganistan", 
               "albania"=>"Albania",
    mainly to allow for a value of any description as well as an option of any value, for example...
    PHP Code:
    $opts = array( ""=>""
               
    "0a3b9f"=>"America, including United Kingdom, Iran, Libya, Lebanon, Kuwait, Iraq, Somalia, Bosnia, Saudi Arabia, Afghanistan, Sudan, Kosovo, Yemen, Pakistan and soon Syria"
               
    "0000b5"=>"Albania"
    --> JQuery questions here <-- ... and please remember to wrap any code you have in forum tags:-
    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  9. #9
    Join Date
    Nov 2015
    Location
    St. Petersburg, Russia
    Posts
    26
    NogDog, it works!

    So, I will only add for example,
    PHP Code:
    $val "Albania"
    PHP Code:
     to the PHP code below.


    <?php
    // might want this array defined in a separate include file or even a database query,
    // rather than typing it all out in this file:
    $options = array(
        
    'Afghanistan',
        
    'Albania',
        
    'Algeria',
        
    'American Samoa',
        
    'Andora',
        
    'Angola',
        
    // . . .
        
    'Zambia'
    );

    $val "Albania";

    ?>
    <select name="country">
      <option <?php if ($val == '') echo 'selected' ?> value="">Country...</option>
    <?php foreach($options as $option)
    {
        
    printf(
            
    "  <option %svalue='%s'>%s</option>\n",
            
    $val == $option 'selected ' '',
            
    $option,
            
    $option
        
    );
    }
    ?>
    </select>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles