Jump to content

Drop down values - aarrgghh!


Go to solution Solved by Psycho,

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>
Edited by Psycho

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; 
					?>
Edited by ramone_johnny
  • Solution

 

<?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>
 
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.