digitalgod Posted July 31, 2006 Share Posted July 31, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/ Share on other sites More sharing options...
pedrobcabral Posted July 31, 2006 Share Posted July 31, 2006 [code]<?php$years = $_POST['YourFormVar'];echo (date('Y')-($years));?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66646 Share on other sites More sharing options...
digitalgod Posted July 31, 2006 Author Share Posted July 31, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66651 Share on other sites More sharing options...
pedrobcabral Posted July 31, 2006 Share Posted July 31, 2006 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'); Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66655 Share on other sites More sharing options...
digitalgod Posted July 31, 2006 Author Share Posted July 31, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66656 Share on other sites More sharing options...
pedrobcabral Posted July 31, 2006 Share Posted July 31, 2006 [code]<?phpfunction test($day, $month, $year) {if (($day + $month + $year) < date('Ymd'))echo "less";elseecho "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. Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66664 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 [code]$age = 18;$query = "SELECT * FROM yourtable WHERE dob <= (NOW() - INTERVAL $age YEAR)";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66673 Share on other sites More sharing options...
digitalgod Posted July 31, 2006 Author Share Posted July 31, 2006 thanks guysryanlwh 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 Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66682 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66698 Share on other sites More sharing options...
digitalgod Posted July 31, 2006 Author Share Posted July 31, 2006 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 "><?phpfor ($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 "> <?phpfor ($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] Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66704 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 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?? Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66708 Share on other sites More sharing options...
digitalgod Posted July 31, 2006 Author Share Posted July 31, 2006 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 "><?phpfor ($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 "> <?phpfor ($i=18;$i <= 100;$i++) { echo '<option value="'.$i.'"'; if ($i==35) { echo " selected"; } echo '>'.$i.'</option>'; } ?></select>[/code]and[code]<?phpforeach($_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 Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66716 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 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:) Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66727 Share on other sites More sharing options...
digitalgod Posted July 31, 2006 Author Share Posted July 31, 2006 must be the heat ;) don't know about you but I'm cooking over here hehehmm I don't get what you meanI 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] Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66739 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 [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] Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66741 Share on other sites More sharing options...
digitalgod Posted July 31, 2006 Author Share Posted July 31, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66746 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 have you tried the $v-1 in "to"? Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66748 Share on other sites More sharing options...
digitalgod Posted July 31, 2006 Author Share Posted July 31, 2006 gives me this query... for some reason $v seems to disappear or somethingSELECT * FROM users WHERE date_birth <= (NOW() - INTERVAL 21 YEAR) AND -1 YEAR) AND level='2' ORDER by id DESC Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66753 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 add parenthesis: INTERVAL ". ($v-1). " YEAR) AND " Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66759 Share on other sites More sharing options...
digitalgod Posted July 31, 2006 Author Share Posted July 31, 2006 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.... Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-66768 Share on other sites More sharing options...
digitalgod Posted August 1, 2006 Author Share Posted August 1, 2006 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-67238 Share on other sites More sharing options...
ryanlwh Posted August 1, 2006 Share Posted August 1, 2006 maybe take off that level=2 for now, so we only test the age part. Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-67276 Share on other sites More sharing options...
digitalgod Posted August 1, 2006 Author Share Posted August 1, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-67291 Share on other sites More sharing options...
ryanlwh Posted August 1, 2006 Share Posted August 1, 2006 can you output the current query so we can see what's wrong with it... Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-67294 Share on other sites More sharing options...
digitalgod Posted August 1, 2006 Author Share Posted August 1, 2006 sure this is what I got when I select between 18 and 22SELECT * FROM users WHERE date_birth <= (NOW() - INTERVAL 18 YEAR) AND date_birth >= (NOW() - INTERVAL 21 YEAR) AND level='2' ORDER by id DESCso I'm guessing I should take off the -1 but then we'll back to the same old problem again... Quote Link to comment https://forums.phpfreaks.com/topic/16144-age-drop-down-solved/#findComment-67297 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.