Jump to content

Archived

This topic is now archived and is closed to further replies.

digitalgod

age drop down *SOLVED*

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?

Share this post


Link to post
Share on other sites
but that won't always work...

let's say you were born on september in 1980 that would mean that you're 25 and not 26 yet. So if I'm looking for people that are 25 years old it will only return everyone that was born in 1981

Share this post


Link to post
Share on other sites
It's pretty the same thing, in that case you will have to ask for the user birthday year+month.
Evaluate if the current month has passed or not. In that logic I would suggest adding also the day.

Use something like: date('Y-m-d');

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
[code]
<?php
function test($day, $month, $year) {
if (($day + $month + $year) < date('Ymd'))
echo "less";
else
echo "more";
}
echo test("20060731");
?>
[/code]

Sorry for this newbe code.. but it was the first thing I could get. If you could improve it I would reply this post again.

Share this post


Link to post
Share on other sites
[code]
$age = 18;
$query = "SELECT * FROM yourtable WHERE dob <= (NOW() - INTERVAL $age YEAR)";
[/code]

Share this post


Link to post
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

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
is the drop down not giving you correct dates after age 36? the date funciton only work with dates after 1970.

and i thought anyone born on 7/31/1984 just turned 22 years old today??

Share this post


Link to post
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

Share this post


Link to post
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:)

Share this post


Link to post
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]

Share this post


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

Share this post


Link to post
Share on other sites
yeah my bad just fixed that a few seconds ago, but it still doesn't work if I still have to choose from 21 to 23 to get people that are 22, selecting 22 to 22 gives me nothing

Share this post


Link to post
Share on other sites
gives me this query... for some reason $v seems to disappear or something

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

Share this post


Link to post
Share on other sites
still doesn't show any entries when selecting 22 - 22, works perfectly if I select different ages though.....

*edit*

actually no, if I do 18-23 it doesn't show me the entries that have 22....

Share this post


Link to post
Share on other sites
maybe take off that level=2 for now, so we only test the age part.

Share this post


Link to post
Share on other sites
well I don't see what that will change, the level is whether the user is a superadmin, admin, user, etc but ok I'll give it a shot

*edit*

still gives me the same result

Share this post


Link to post
Share on other sites
can you output the current query so we can see what's wrong with it...

Share this post


Link to post
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...

Share this post


Link to post
Share on other sites

×

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.