Jump to content

Drop down values - aarrgghh!


ramone_johnny

Recommended Posts

Hey guys,

This is probably a complete no brainer, but I'm stuck with it.

 

Here's the code I currently have.

<select name="share_prefage" class="textarea" style="width:150px">
<? 
if (!isset($_SESSION['share_prefage'])) 
echo "<option value='Doesnt Matter'>Doesnt Matter</option>";
else
{
echo "<option selected value=" . $_SESSION['share_prefage'] . ">Around " . $_SESSION['share_prefage'] . "</option>";
}
{
for ($i=18; $i<=99; $i++) 
echo "<option value='$i'>Around $i</option>";
}
?>
</select>

Put simply here's what I'm trying to accomplish

 

1. If the session variable has not been set, set the default value to "Doesnt Matter.

2. If the session variable has been set, write it within the drop down BUT...

 

if it equals "Doesnt Matter" then don't write Doesnt Matter twice, and dont write "Around Doesnt Matter"

 

3. Keep the loop at the end regardless.

Link to comment
https://forums.phpfreaks.com/topic/278087-drop-down-values-aarrgghh/
Share on other sites


<?php

$prefage_options = '';
$selected_value = false;
if (!isset($_SESSION['share_prefage']) || !ctype_digit($_SESSION['share_prefage']))
{
    // no session value or session value is text
    $prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n";
}
else
{
    //Session value is number. Set selected value
    $selected_value = $_SESSION['share_prefage'];
}

for ($i=18; $i<=99; $i++)
{
    $selected = ($i == $selected_value) ? ' selected="selected"' : '';
    $prefage_options .= "<option value='{$i}'{$selected}>Around {$i}</option>\n";
}
?>
<select name="share_prefage" class="textarea" style="width:150px">
<?php echo $prefage_options; ?>    
</select>

And, have you tried to change it as to how you need it? Just look at the code and follow the logic. It should be very simple to make sure "Doesnt matter" is always displayed. I'm not trying to be an ass, just trying to get you to think through the problem.

Thanks man.

Sorry, I'm not intending to be lazy - I'm just completely new at PHP.

Here's what i ended up with. It seems to work, but I'd say it could be written more efficiently.
 

  <?php

					$prefage_options = '';
					$selected_value = false;
					if (!isset($_SESSION['share_prefage']) || !ctype_digit($_SESSION['share_prefage']))
					{
						// no session value or session value is text
						$prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n";
					}
					else
					{	
						$selected_value = $_SESSION['share_prefage'];
					}
					
					for ($i=18; $i<=99; $i++)
					{
						$selected = ($i == $selected_value) ? ' selected="selected"' : '';
						$prefage_options .= "<option value='{$i}'{$selected}>Around {$i}</option>\n";
					}
					?>
					<select name="share_prefage" class="textarea" style="width:150px">
					<?php 
					if (isset($_SESSION['share_prefage']) AND ctype_digit($_SESSION['share_prefage']))
					{
					//Session value is number. Set selected value
					$prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n";
					}
					echo $prefage_options; 
					?>    
					</select>

Here's what I changed/added.
 

<?php 
					if (isset($_SESSION['share_prefage']) AND ctype_digit($_SESSION['share_prefage']))
					{
					//Session value is number. Set selected value
					$prefage_options .= "<option value='Doesnt Matter'>Doesnt Matter</option>\n";
					}
					echo $prefage_options; 
					?>

 

<?php

$selected_value = false;
if (isset($_SESSION['share_prefage']) && ctype_digit($_SESSION['share_prefage']))
{
    //Session value is number. Set selected value
    $selected_value = $_SESSION['share_prefage'];
}

$prefage_options = "<option value='Doesnt Matter'>Doesnt Matter</option>\n";

for ($i=18; $i<=99; $i++)
{
    $selected = ($i == $selected_value) ? ' selected="selected"' : '';
    $prefage_options .= "<option value='{$i}'{$selected}>Around {$i}</option>\n";
}
?>
<select name="share_prefage" class="textarea" style="width:150px">
<?php echo $prefage_options; ?>    
</select>
 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.