Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:42:36+00:00 2026-06-03T13:42:36+00:00

I have a fairly simple problem. I have a select dropdown with values 1

  • 0

I have a fairly simple problem. I have a select dropdown with values 1 to 10. This is an edit screen so users have already chosen a value that should be selected. I am giving them the chance to change it. So all I need to do is insert the word selected in the option value. To accomplish this simple task, I have been doing a bunch of inelegant if thens. Is there a best practice or simple way to do this. It seems unnecessary overhead to do all these if thens and create 10 new variables. Note for reasons of keeping html separate from php logic, I do not want to write the option values out sequentially I have the html in a separate area and just need to set the variable selected in the appropriate places.

if ($row['points']==1){
$select1 = "selected"}
if ($row['points']==2){
$select2 = "selected"}
//values 3 through 9
if ($row['points']==2){
$select2 = "selected"}


echo '<select name="points">';
echo '<option value = 1'. $select1.'>1</option>';
echo '<option value=2'. $select1.'>2</option>';
//values 3 through 9
echo '<option value=10'.$select10.'>10</option>';
echo '</select>';
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:42:37+00:00Added an answer on June 3, 2026 at 1:42 pm

    Because you are using single selectable list (no multiple selected allowed) i.e. so only one can be selected at once.. look for http://fi.php.net/str_replace

    echo str_replace("value='".$selected_value."'", "value='".$selected_value."' selected", "<option value='1'>1</option><option value='2'>2</option> ....");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a fairly simple problem to me but I have been having
I am learning LINQ and this seems like a fairly simple problem. I have
My problem is fairly simple. I have table sets that store product sets (more
I have a fairly simple sync problem. I have a table with about 10
I have a fairly simple const struct in some C code that simply holds
I have a fairly simple python loop that calls a few functions, and writes
I have fairly simple problem but I can not think of the simple solution.
I have a fairly simple bash shell scripting problem. I want to sed a
I have this idea that using SQL VIEWS to abstract simple database computations (such
I have a fairly simple process running that periodically pulls RSS feeds and updates

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.