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
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Help with a dynamic select box?

    I was wondering if anyone could help. I have a list of websites to publish to in a CMS system that is called in via a function I created:

    PHP Code:

            <?php 
                $type_array
    =get_possible_sites(); 
                foreach (
    $type_array as $sites
               { 
                  print(
    "<option value=\"".$sites['site_id']."\"" . ($site_id == $sites['site_id'] ? 'selected="selected"' '') . ">» ".$sites['name']."</option>
                         <option value=\""
    .$share_one."\"" . ($site_id == $share_one 'selected="selected"' '') . ">» ".$share_one_name."</option>
                         <option value=\""
    .$share_two."\"" . ($site_id == $share_two 'selected="selected"' '') . ">» ".$share_two_name."</option>
                         <option value=\""
    .$share_country."\"" . ($site_id == $share_country 'selected="selected"' '') . ">» ".$share_country_name."</option>
                         <option value=\""
    .$share_uk."\"" . ($site_id == $share_uk 'selected="selected"' '') . ">» ".$share_uk_name."</option>
                         <option value=\""
    .$share_group."\"" . ($site_id == $share_group 'selected="selected"' '') . ">» ".$share_group_name."</option>\n"); 
                         
               } 
            
    ?>
    Now some of these variable will be empty and contain nothing in them. Now rather than going through them one-by-one and saying:

    PHP Code:

    if ($share_two != '') {
    //Output to the list
    else
    //Dont show on the list

    Is there a more efficent way of doing this?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    The best option would be to not allow the get_possible_sites() to return empty values.
    But, if you want to keep them and just don't want to display them, you'll need to make the check. You can make it with less code though:
    PHP Code:
    foreach ($type_array as $sites
               { 
          if (empty(
    $sites))
          {
                continue;
          }
                  print(
    "<option value=\"".$sites['site_id']."\"" . ($site_id == $sites['site_id'] ? 'selected="selected"' '') . ">» ".$sites['name']."</option>
                         <option value=\""
    .$share_one."\"" . ($site_id == $share_one 'selected="selected"' '') . ">» ".$share_one_name."</option>
                         <option value=\""
    .$share_two."\"" . ($site_id == $share_two 'selected="selected"' '') . ">» ".$share_two_name."</option>
                         <option value=\""
    .$share_country."\"" . ($site_id == $share_country 'selected="selected"' '') . ">» ".$share_country_name."</option>
                         <option value=\""
    .$share_uk."\"" . ($site_id == $share_uk 'selected="selected"' '') . ">» ".$share_uk_name."</option>
                         <option value=\""
    .$share_group."\"" . ($site_id == $share_group 'selected="selected"' '') . ">» ".$share_group_name."</option>\n"); 
                         
               } 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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