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

  • SEARCH
  • Home
  • 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 8955517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:34:42+00:00 2026-06-15T14:34:42+00:00

I am not quite getting the code below right but I have a drop

  • 0

I am not quite getting the code below right but I have a drop down menu below where I want it to display these values below:

1
1/2
2
2/3
3
3/4
4
4/5

...

10

Now I also want to be able to keep the option selected after a submit so I have tried this code below but the problem is that it is only display values 1/2, 2/3, 3/4, 4/5... etc.

foreach ($years as $year) {
    if ($validSubmission && $year == $getduration) {
         if ($year != $max_year) {
         $nextYear = $year + 1;
      $durationHTML .= "<option value='" . $year . "' selected='selected'>$year/$nextYear</option>".PHP_EOL;      
     }else{
    $durationHTML .= "<option value='" . $year . "' selected='selected'>$year</option>".PHP_EOL;  
}
}else{
         if ($year != $max_year) {
         $nextYear = $year + 1;
    $durationHTML .= "<option value='" . $year . "'>$year/$nextYear</option>".PHP_EOL;
}else{
    $durationHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL;
     }
}
}

The origanl code was this below where it displays the correct options but did not perform the $validSubmission variable so that it does not keep the option selected after submitting a page:

foreach ($years as $year) {
    $durationHTML .= "<option>$year</option>".PHP_EOL;  
    if ($year != $max_year) {
         $nextYear = $year + 1;
         $durationHTML .= "<option>$year/$nextYear</option>".PHP_EOL;              
    }
}
$durationHTML .= '</select>';

The code below does keep the option displayed after submit but does not display the values with / in between the values:

 foreach ($years as $year) {
        if ($validSubmission && $year == $getduration) {
            $durationHTML .= "<option value='" . $year . "' selected='selected'>$year</option>" . PHP_EOL;
        } else {
            $durationHTML .= "<option value='" . $year . "'>$year</option>" . PHP_EOL;
        }
    }

But trying to combine both the code has not worked and that is the problem I am having

  • 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-15T14:34:44+00:00Added an answer on June 15, 2026 at 2:34 pm

    Try this modified/simplified code. By using a ternary comparison – $var = condition ? if_true : if_false – to set the selected='selected' attribute, you can minimize the code (from ~17 lines to ~9).

    foreach ($years as $year) {
    
        //Gets the single years - 1,2,3...
        $sel =($validSubmission && $year == $getduration)? " selected='selected'": "";
        $durationHTML .= "<option value='$year'$sel>$year</option>".PHP_EOL;
    
        //Gets the double years - 1/2,2/3,3/4...
        if ($year != $max_year) {
        $nextYear = $year + 1;
        $sel =($validSubmission && ($year.'/'.$nextYear) == $getduration)? " selected='selected'": "";
        $durationHTML .= "<option value='$year/$nextYear'$sel>$year/$nextYear</option>".PHP_EOL;  
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm obviously not quite getting the 'end-of-file' concept with C++ as the below program
Related but not quite what I am looking for merge-two-branches-what-direction. Situation: I have inherited
I've been writing CUDA code for quite a while, but I'm just now getting
I'm trying to build an associative array in PHP dynamically, and not quite getting
I'm working on sorting a list of objects, and unfortunately, I'm not quite getting
I am getting an error message, and I not quite sure where the issue
Im not quite sure what I am doing wrong here, I have folloed the
I'm not quite sure how it works but what I need to do is
I'm not quite sure what's going on, but several issues are occurring on our
I am getting the following error message on the code below (which is at

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.