Jump to content

age drop down *SOLVED*


digitalgod

Recommended Posts

does anyone know how I can make a drop down where the value is the year depending on the chosen age.

For instance if I select 18 in the drop down the value would be 1988...

Because the only thing I have in my database is the date of birth and I would like to search the database by selecting from age something to age something.

Or would it be better to calculate the year after the form has been submitted?
Link to comment
Share on other sites

well that's what I have in my db, date_birth is in this format 2006-07-31 and I already have a function to calculate the age but I really don't know how to calculate the exact year depending on the current date.

I have 2 drop downs, something like Select age between 1st drop down and 2nd drop down
Link to comment
Share on other sites

thanks guys

ryanlwh that worked perfectly with a few tweaks, was just wondering if you could explain it to me.

I don't understand why you did -$age year

*edit*

just noticed your edit, I'm guessing that query is much faster right? And I need to search between 2 dates, so if I select between the ages of 18 and 35 the query has to look for everything >= 18 and <= 35
Link to comment
Share on other sites

the -$age was for strtotime to deduct $age years from now, but then i remembered it won't work for dates before 1970, so i switched to mysql, which supports every date values (well, technically, until year 9999).

since you marked it as solved, i assume you figured it out??
[code]$query = "SELECT * FROM yourtable WHERE dob BETWEEN (NOW() - INTERVAL $age_from YEAR) AND (NOW() - INTERVAL $age_to YEAR) ";
[/code]
Link to comment
Share on other sites

hey ryanlwh I don't think it's working properly (unless I'm doing something wrong)

here's what I have (used your old code but I'll try again with MySQL)

[code]
<select name="from" style="width:60px; height:17px; font-family:tahoma; font-size:10px; color:#9A400C ">
<?php
for ($i=18;$i <= 100;$i++) {
   $bDate = date('Y-m-d',strtotime("-$i year"));
   echo '<option value="'.$bDate.'">'.$i.'</option>';
}
?>
</select>
and
<select name="to" style="width:60px; height:17px; font-family:tahoma; font-size:10px; color:#9A400C ">
<?php
for ($i=18;$i <= 100;$i++) {
   $bDate = date('Y-m-d',strtotime("-$i year"));
    echo '<option value="'.$bDate.'"';
    if ($i==35) { echo " selected"; }
echo '>'.$i.'</option>';
     }
?>
</select>
[/code]

and the processing part
[code]
<?php
// If I chose between the ages of 22 and 22 I get
$qtmp = array();
                   foreach($_POST as $k => $v)
                   switch ($k) {
                    case 'from':
echo $v; //returns 1984-07-31  which is not right
$qtmp[] = "date_birth <='" . $v . "' AND ";
break;
case 'to':
echo $v; //returns 1984-07-31 which is not right
$qtmp[] = "date_birth >='" . $v . "' AND ";
break;
}
?>
[/code]
Link to comment
Share on other sites

ok tha didn't work either, change my code to try your MySQL query but now it won't show me ny results

[code]
<select name="from" style="width:60px; height:17px; font-family:tahoma; font-size:10px; color:#9A400C ">
<?php
for ($i=18;$i <= 100;$i++) {
   echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>
and
<select name="to" style="width:60px; height:17px; font-family:tahoma; font-size:10px; color:#9A400C ">
<?php
for ($i=18;$i <= 100;$i++) {
    echo '<option value="'.$i.'"';
    if ($i==35) { echo " selected"; }
echo '>'.$i.'</option>';
     }
?>
</select>
[/code]

and

[code]
<?php
foreach($_POST as $k => $v)
switch ($k) {
      case 'from':
$qtmp[] = "date_birth BETWEEN (NOW() - INTERVAL " . $v . " YEAR) AND";
break;
      case 'to':
$qtmp[] = "(NOW() - INTERVAL " . $v . " YEAR) AND ";
break;
}
$query = "SELECT * FROM " . $prefix . "users " . implode(' ', $qtmp) . "level='2' ORDER by id DESC";
$_SESSION['custom_browse'] = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>
[/code]

but you can be 22 and born in 1983 so it won't display you :P
Link to comment
Share on other sites

ahh yes, so the 'from' limit should be one year further back, and only > than that date. so between won't work here. use > and <=

am i correct? for some reason i'm not thinking clearly today.

EDIT: oh one more thing, from and to should be swapped too:)

Link to comment
Share on other sites

must be the heat ;) don't know about you but I'm cooking over here hehe

hmm I don't get what you mean

I switched it to this and I'm getting an error

[code=php:0]
case 'from':
$qtmp[] = "date_birth <= (NOW() - INTERVAL " . $v . " YEAR) AND";
break;
case 'to':
$qtmp[] = "> (NOW() - INTERVAL " . $v . " YEAR) AND ";
break;
[/code]
Link to comment
Share on other sites

sure this is what I got when I select between 18 and 22

SELECT * FROM users WHERE date_birth <= (NOW() - INTERVAL 18 YEAR) AND date_birth >= (NOW() - INTERVAL 21 YEAR) AND level='2' ORDER by id DESC

so I'm guessing I should take off the -1 but then we'll back to the same old problem again...
Link to comment
Share on other sites

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.