Jump to content

age/year dropdown


adamlacombe

Recommended Posts

Okay I am trying to list out years in a dropdown box. It should show say how many years old. I want to start from 16 though and stop at 100. So the year has to be 1995 for 16 but then I would have to change the code every year. so I was wondering how do I modify this code to do what I explained?

<?php
$start_year = ($start_year) ? $start_year - 1 : date('Y') - 100;
    $end_year = ($end_year) ? $end_year : date('Y');
for ($i = $end_year; $i > $start_year; $i -= 1) {

$date=date(Y);
$age = $i - $date;

        echo '<option value="'.$i.'">'.$age.'</option>';
    }
?>

 

Thanks in advanced

Link to comment
Share on other sites

  • Replies 66
  • Created
  • Last Reply

Top Posters In This Topic

Not everyone born in 1995 is 16 years old yet, so using the year as the value in the <option> tags isn't going to be accurate. Either use the year for both the value and the display, or use the age for both. This would list the years:

$start = date('Y') - 16;
$end = date('Y') - 100;
$years = range( $start, $end );

echo '<select name="year">';
foreach( $years as $v ) {
     echo "<option value=\"$v\">$v</option>\n";
}
echo '</select>';

Link to comment
Share on other sites

Thanks for the quick reply guys.

I am going to try to use that code but here is my whole plan and issues im having.

 

Users table has month, day, and year. I want to make a search that will search AGE to AGE. Is it possible to go about doing that with how I have the table set up?

Link to comment
Share on other sites

This query will set the value of the newly added date field based on the value of the existing day, month, year fields. You just need to edit it to reflect your actual table and field names. Then I have to remember the query to calculate age from DOB . . . I'll work on that while you do this

 

UPDATE `table` SET `new_date_field` = (SELECT CAST(CONCAT_WS('-', `old_year_field`, `old_month_field`, `old_day_field`) AS DATE))

Link to comment
Share on other sites

Okay now my new field "bdate" contains this: 1994-02-22

is that correct?

 

I have this for a age calc:

function DetermineAgeFromDOB ($month,$day,$year)
{

//Get age 
$birthday = mktime(0, 0, 0, $month, $day, $year); 
$today = mktime(0, 0, 0, date('n'), date('j'), date('Y')); 
$age = floor(($today - $birthday) / (86400*365)); 

  return $age; 
} 

Link to comment
Share on other sites

Yes, that's the correct format. You can get MySQL to calculate the age directly in the query string, and it will take into account leap years, using this in the query. This query uses the alias "AS age", so the result will be available in the fetched array as (for example) $array_name['age']. You just need to change the name of `table`.

 

SELECT ( (DATE_FORMAT(CURDATE(),'%Y') - DATE_FORMAT(`bdate`, '%Y') ) - ( DATE_FORMAT(CURDATE(), '00-%m-%d') < DATE_FORMAT( `bdate`, '00-%m-%d')) ) AS age FROM `table`

 

I know it looks pretty confusing, but it isn't as bad as it appears. Let me know if you have any problems with it, or need help getting it to work with an existing query.

Link to comment
Share on other sites

hmm yes i am confused lol

so I got down the drop down lists and that is in a form, submits it to search.php looks like this:

$age_from = clean_up($_POST['age_from']);
$age_to = clean_up($_POST['age_to']);

$r3 = mysql_query("SELECT * FROM users WHERE `year` BETWEEN " . $age_from . "AND " . $age_to."");

 

thats what it looks like now.. so how do I fix it to this one?

 

I'm pretty damn smart when it comes to php and mysql but damn this is all new to me lol so thanks so very much for the help

Link to comment
Share on other sites

This is going to look even more confusing, so I'll separate it into chunks to help it make some sense.

 

It's easier to grasp when you consider that what you're really doing is selecting the result of the calculation of the bdate field, and also using just the result of that calculation in the WHERE clause.

 

SELECT 

`field_1`, 
`field_2`, 
`field_3`, 
( (DATE_FORMAT(CURDATE(),'%Y') - DATE_FORMAT(`bdate`, '%Y') ) - ( DATE_FORMAT(CURDATE(), '00-%m-%d') < DATE_FORMAT( `bdate`, '00-%m-%d')) ) AS age 

FROM `users` 

WHERE 

( (DATE_FORMAT(CURDATE(),'%Y') - DATE_FORMAT(`bdate`, '%Y') ) - ( DATE_FORMAT(CURDATE(), '00-%m-%d') < DATE_FORMAT( `bdate`, '00-%m-%d')) ) 

BETWEEN $age_from AND $age_to

Link to comment
Share on other sites

this is what my search.php file looks like:

<?php
$title="Search";
$metakeywords="search, find";
$metadescription="Search for your love!";
include ('header.php');

$keyword = clean_up($_POST['keyword']);
$gender = clean_up($_POST['gender']);
$age_from = clean_up($_POST['age_from']);
$age_to = clean_up($_POST['age_to']);

$r3 = mysql_query("SELECT `id`, `first`, `last`, `avatar`, 
( (DATE_FORMAT(CURDATE(),'%Y') - DATE_FORMAT(`bdate`, '%Y') ) - ( DATE_FORMAT(CURDATE(), '00-%m-%d') < DATE_FORMAT( `bdate`, '00-%m-%d')) ) AS age 

FROM `users` 

WHERE 

( (DATE_FORMAT(CURDATE(),'%Y') - DATE_FORMAT(`bdate`, '%Y') ) - ( DATE_FORMAT(CURDATE(), '00-%m-%d') < DATE_FORMAT( `bdate`, '00-%m-%d')) ) 

BETWEEN $age_from AND $age_to") or die(mysql_error()); 

echo "$age_from $age_to";
echo "<table width='100%' align='center'>
<div class='content'>
Search users results for: $keyword
<br />";

while($rr3=mysql_fetch_array($r3)){
$user=clean_up($rr3[id]);
$first=clean_up($rr3[first]);
$last=clean_up($rr3[last]);
$avatar=clean_up($rr3['avatar']);

echo "<a href='index.php?action=profile&id=$user'><img src='avatars/$avatar' width='41' height='41' /> $first $last</a><hr>";
}
echo "</div></table>";


echo "</div>";

include ('footer.php'); ?>

 

 

 

and my page with the search form:



            <form action="index.php?action=search" method="post">
              <div class="search_row">
                <div class="search_column_1">
                  <label>Desired Age</label>
                </div>
                <div class="search_column_2">
                  <select class="date" name="age_from">';

$start = date('Y') - 16;
$end = date('Y') - 100;
$years = range( $start, $end );

foreach( $years as $v ) {
$age = $v - date('Y');
     echo "<option value=\"$v\">$age</option>\n";
}
echo '
                  </select>

                  <select class="date" name="age_to">';

$start = date('Y') - 16;
$end = date('Y') - 100;
$years = range( $start, $end );

foreach( $years as $v ) {
$age = $v - date('Y');
     echo "<option value=\"$v\">$age</option>\n";
}
echo '
                  </select>

 

 

and I get no results

Link to comment
Share on other sites

I think I just figured it out without all that. Since we're comparing ages, not years, we need to change the <select> fields so the values they pass are ages. Just need to change the range() assignments to spit out the ages instead of the years. See if that fixes it.

[code=php:0]
$years = range( 16, 100 );  // <--- date() calculation no longer needed . . . 

foreach( $years as $v ) {
$age = $v - date('Y');
     echo "<option value=\"$v\">$age</option>\n";
}
echo '
                  </select>

                  <select class="date" name="age_to">';

$years = range( 16, 100 ); // <--- HERE also . . .

foreach( $years as $v ) {
$age = $v - date('Y');
     echo "<option value=\"$v\">$age</option>\n";
}
echo '</select> . . . 

Link to comment
Share on other sites

oh okay thanks.

I'm gunna bug you again, sorry. I actually spent two whole days trying to figure this all out lol so really thanks for helping me.

 

Now im not so advanced to know how to joi tables together.. tell me if thats what I would need to do..

I have another table called "questions" in that table there is a field called "smoke" how would I add that to the query if its in another table?

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.