...

View Full Version : Help with a dynamic select box?



stfc_boy
05-08-2009, 06:42 PM
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
$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:




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


Is there a more efficent way of doing this?

Fou-Lu
05-08-2009, 07:52 PM
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:


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");

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum