TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-31-2008, 05:08 PM   #1 (permalink)
The Wanderer
 
Join Date: Jun 2005
Posts: 21
Thanks: 1
Jako is on a distinguished road
Default Sorting by age issue.

I have a table that stores users birthdays, date, month year, etc.

I then have a function which turns these into an age, I.E. 24.

Problem is when I want to show all users who are a specific age I get stuck. I can do it an easy way and sort by their year born, but this will sometimes show a fluctuation because everyone born from 1988 may not be 20 yet.

Is there any work around for this?
Jako is offline  
Reply With Quote
Old 07-31-2008, 05:50 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

You could use MySQL to work out their age. However, the MySQL for that is rather intensive, and would require the HAVING clause. How about simply returning them all by the year, and then in the loop simply calculate their age and if it doesn't match ignore that one and continue.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
The Following User Says Thank You to Karl For This Useful Post:
Jako (07-31-2008)
Old 07-31-2008, 07:20 PM   #3 (permalink)
The Wanderer
 
Join Date: Jun 2005
Posts: 21
Thanks: 1
Jako is on a distinguished road
Default

Quote:
Originally Posted by Karl View Post
You could use MySQL to work out their age. However, the MySQL for that is rather intensive, and would require the HAVING clause. How about simply returning them all by the year, and then in the loop simply calculate their age and if it doesn't match ignore that one and continue.
Thanks for the reply, I'm just learning PHP along the way and I'm not the best at while loops.

How would I go about setting it up?

The number I am going to pass through will be the age then, so

$id=$_GET['id']; would be 23 for example.

And the variables are $month, $day and $year which when put in calculate_age($month, $day, $year) will calculate the age.

Not too sure how to write the while loop to look for the specified age.
Jako is offline  
Reply With Quote
Old 07-31-2008, 07:27 PM   #4 (permalink)
The Contributor
 
Ross's Avatar
 
Join Date: Jan 2008
Location: England, UK
Posts: 83
Thanks: 3
Ross is on a distinguished road
Default

You'll use this a lot when dealing with multiple rows of data:

PHP Code:
$query mysql_query('SELECT * FROM `people`');

// Check at leats one row has been returned
if(mysql_num_rows($query) < 1)
{
    exit;
}

/**
 * $query is a resource that contains multiple rows of data, you can 
 * use fetch_assoc or fetch_array to get this data
 */
 
/**
 * While loops keep going while the condition is true, or in this case while there are still rows in $query
 */
$ages = array();

while(
$row mysql_fetch_assoc($query))
{
    
// Add a new element to the array with the age
    
$ages[] = calculate_age($row['day'], $row['month'], $row['year']);

Ross is offline  
Reply With Quote
Old 07-31-2008, 07:42 PM   #5 (permalink)
The Wanderer
 
Join Date: Jun 2005
Posts: 21
Thanks: 1
Jako is on a distinguished road
Default

PHP Code:
$ages = array();

while(
$row mysql_fetch_assoc($result))
{
    
// Add a new element to the array with the age
    
$ages[] =  calculate_age($month$day$year);

Ok so I condensed it down to what I think I needed, but how do I input the age into there.

Like I said above, if $id = 23, how do I only display the results from the calculation function which are 23.
Jako is offline  
Reply With Quote
Old 07-31-2008, 08:00 PM   #6 (permalink)
The Wanderer
 
Join Date: Jun 2005
Posts: 21
Thanks: 1
Jako is on a distinguished road
Default

I ended up getting it, finally figured it out haha but thanks to all for your help.

After I built the array, I used the in_array() function to find the $id which is the age I was looking for to output my results.

thanks.
Jako is offline  
Reply With Quote
Old 07-31-2008, 09:34 PM   #7 (permalink)
The Contributor
 
Ross's Avatar
 
Join Date: Jan 2008
Location: England, UK
Posts: 83
Thanks: 3
Ross is on a distinguished road
Default

Quote:
Originally Posted by Jako View Post
I ended up getting it, finally figured it out haha but thanks to all for your help.

After I built the array, I used the in_array() function to find the $id which is the age I was looking for to output my results.

thanks.
Were you only looking to get the age of person.id #23? IF so you're much better off doing something like this:

PHP Code:
$query mysql_query('SELECT * FROM `people` WHERE `id` = 23 LIMIT 1');

$person mysql_fetch_assoc($query);
// Get the data from this array as in the while loop - but you don't need one for this example

print_r($person); 
It's much more efficient and easier if you only need one person's results.
Ross is offline  
Reply With Quote
Old 08-02-2008, 05:47 AM   #8 (permalink)
The Wanderer
 
Join Date: Jun 2005
Posts: 21
Thanks: 1
Jako is on a distinguished road
Default

This is what I ended up going with which worked perfectly

PHP Code:
<?php 
while($row mysql_fetch_assoc($result)) {
$ages = array();
$ages[] = calculate_age($row['month'], $row['day'], $row['year']);
if (
in_array($a$ages)) { ?>
and $a was the age I was passing through the url.
Jako is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:44 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design