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 9020127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:56:20+00:00 2026-06-16T04:56:20+00:00

I already have a select option like this: <?php echo ‘Age : <select id=age

  • 0

I already have a select option like this:

<?php
    echo 'Age : <select id="age" name ="age" class="selecta">';
    for($i = 0; $i <= 24; $i += 0.25) 
    { 
?>
    <option value="<?php echo $i ;?>" 
    <?php
    if ($_SESSION['age'] == $i) 
    { 
        echo " selected='selected'"; 
    } 
    ?> > 
    <?php echo $i .'&nbsp&nbsp' ?>
    </option>  
    <?php 
    }
    echo '</select> months <br />';
?>

which works good and displays this :

  0           // means 0 month
  0.25        // means 1 week   or  0.25 of month
  0.50        // means 2 weeks  or half month
  0.75
  1           // means 1 month
  1.25
  ....
  24

but it doesn’t look too good for users. So what I want is to have them like this:

0      // 0month
1 week  
2 weeks
3 weeks
4 weeks  
1 month
1 month and 1 week
1 month and 2 weeks
.....
24 months

It’s not my final desire to be exactly like that but I would something like that.

Is this possible?

  • 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-16T04:56:21+00:00Added an answer on June 16, 2026 at 4:56 am
    for($i = 0; $i <= 24; $i += 0.25) { 
    

    You are having a sequence of 97 iterations here. It is equally to

    range(0, 96);
    

    You then map any of these step to a specific number:

    $map = function($step) {
        return $step * 0.25;
    }
    

    Which would for example create the following sequence:

    0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, ... , 23, 23.25, 23.5, 23.75, 24
    

    So now you don’t want these numbers but just the nice months. You can just calculate them if you take the steps (instead your fractional numbers) more easily:

    month: floor($step / 4)   - division (integer)
    weeks: $step % 4          - modulo   (integer)
    

    Or as the mapping function to turn that into text:

    $map = function ($step) {
        $month = floor($step / 4);
        $week  = $step % 4;
    
        $buffer = '';
    
        if ($month) {
            $buffer .= "$month month";
        }
    
        if ($week) {
            if ($month) {
                $buffer .= ' and ';
            }
    
            $buffer .=  "$week week" . ($week == 1 ? '' : 's');
        }
    
        return $buffer;
    };
    

    For example:

    $map(0)   # (empty)
    $map(1)   # 1 week
    $map(10)  # 2 month and 3 weeks
    $map(95)  # 23 month and 3 weeks
    $map(96)  # 24 month
    

    So now you only need to map the range as keys to their values and then you can do the standard output of a select box with these values:

    $list = range(0, 96);
    
    $selected = 12; // $_SESSION['age']
    
    echo "Age : \n<select id='age' name ='age' class='selecta'>\n";
    foreach ($list as $value)
    {
        $label = $map($value);
        printf("  <option value='%d'%s>%s</option>\n", $value, $value === $selected ? " selected='selected'" : '', $label);
    }
    echo "</select>\n";
    

    Which gives you your output (Demo):

    Age : 
    <select id='age' name ='age' class='selecta'>
      <option value='0'></option>
      <option value='1'>1 week</option>
      <option value='2'>2 weeks</option>
      <option value='3'>3 weeks</option>
      <option value='4'>1 month</option>
      ...
      <option value='11'>2 month and 3 weeks</option>
      <option value='12' selected='selected'>3 month</option>
      <option value='13'>3 month and 1 week</option>
      ...
      <option value='50'>12 month and 2 weeks</option>
      <option value='51'>12 month and 3 weeks</option>
      <option value='52'>13 month</option>
      <option value='53'>13 month and 1 week</option>
      <option value='54'>13 month and 2 weeks</option>
      ...
      <option value='92'>23 month</option>
      <option value='93'>23 month and 1 week</option>
      <option value='94'>23 month and 2 weeks</option>
      <option value='95'>23 month and 3 weeks</option>
      <option value='96'>24 month</option>
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I have the following select box: <select class=event_selection name=Event[] id=Event_<?php echo $i; ?>
I have this piece of code for select option: <select class=select-box name=select-choice-month id=select-locations> <option
I have a nested php array. I have the first select populated already. I
Can any one help me with this.. i have an option box like this
i already have a project with TreeNode class which creates a hierachy of nodes
I have already have this code to force these URLs to HTTPS: RewriteCond %{HTTPS}
Basically, I am doing this: I have already read the database and obtained all
I have a combobox in a form, written in html, like following: <form name=formname
I have a single item HTML select box on which I'd like to find
I want to have a simple select->option dropdown list that I am not passing

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.