Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4259132
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:44:32+00:00 2026-05-21T05:44:32+00:00

I’ve got a site with user profiles. On the home page I am trying

  • 0

I’ve got a site with user profiles. On the home page I am trying to show little summaries of each person’s profile. This summary includes the name of the person, the gender and the age of the person.

        <ul>                
            {section name=i loop=$person_list}
                <li><a href="user_detail.php?id={$person_list[i].id}">{$person_list[i].username}, {$person_list[i].gender}, {$age[i]}
            {/section}
        </ul>

I previously used a foreach loop to run through them, but I need the index to also show the display age calculated in the index.php file.

if (isset($_GET['name']) && isset($_GET['gender'])) 
{
   $name = $_GET['name'];
   $gender = $_GET['gender'];
} 
else 
{
   $name = "";
   $gender = "";
}

define("PERSONS_PER_PAGE", 10);


if (isset($_GET['offset'])) 
{
    $offset = $_GET['offset'];
} 
else 
{
    $offset = 0;
}


list($person_list, $num_persons) = get_persons($name, $gender, $offset, PERSONS_PER_PAGE);

$ages = array();


for ($i = 0; $i < count($person_list); $i++)
{
    $ages[$i] = age($person_list[$i].$dateOfBirth, time());
}

$smarty = new Smarty;
$smarty->assign("name", $name);
$smarty->assign("gender", $gender);
$smarty->assign("persons_per_page", PERSONS_PER_PAGE);
$smarty->assign("interests", $interests);
$smarty->assign("offset", $offset);
$smarty->assign("num_persons", $num_persons);
$smarty->assign("person_list", $person_list);
$smarty->assign("ages", $ages);
$smarty->display("index.tpl");

Don’t worry too much about the $offset and PERSONS_PER_PAGE, that’s only used for pagination.

My age function looks like this:

function age($birth)
{
   $birth_date = new DateTime();
      $birth_date = $birth;
   $birth_date->setTimestamp($birth);

   $now = time();

   $now_date = new DateTime();
   $now_date->setTimestamp($now);

   $interval = $birth_date->diff($now_date); // $interval is a DateInterval


   $age = $interval->y; // number of years in the interval
   return $age;
}

Additionally, in case it’s relevant; my get_persons function looks like this:

function get_persons($name, $gender, $offset, $persons_per_page) 
{
    $connection = mysql_open();

    $query = "SELECT SQL_CALC_FOUND_ROWS id, username, gender, dateOfBirth FROM Person";
    if ($name && $gender) 
    {
        $query .= " WHERE name like '%$name%' AND gender like '%$gender%'";
    }
    $query .= " order by id";
    $query .= " LIMIT $offset, $persons_per_page";
    // print "$query<br>\n";

    $result = mysql_query($query, $connection) or show_error();

    $r = mysql_query("SELECT FOUND_ROWS()", $connection) or showerror();
    $r = mysql_fetch_array($r);
    $num_entries = $r[0];

    $entries = array();

    $person_list = array();
    while ($person = mysql_fetch_array($result)) {
        $person_list[] = $person;
    }

    mysql_close($connection) or show_error();
    return array($person_list, $num_entries);
}

and the table for a person looks like this:

create table if not exists Person
(
  id int not null auto_increment primary key,
  username varchar(10) not null,
  name varchar(40) not null,
  gender varchar(1) not null,
  dateOfBirth timestamp not null,
  email varchar(40) not null
);

This produces the following error:

Notice: Undefined variable: dateOfBirth in /net/homes.ict.griffith.edu.au/export/home/s2737451/public_html/wp/labs/lab5/index.php on line 39 Fatal error: Call to a member function setTimestamp() on a non-object in /net/homes.ict.griffith.edu.au/export/home/s2737451/public_html/wp/labs/lab5/includes/defs.php on line 167

Thanks in advance, I really need help on this 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T05:44:33+00:00Added an answer on May 21, 2026 at 5:44 am

    or you can do it by changing your sql command:

    function get_persons($name, $gender, $offset, $persons_per_page) 
    {
        $connection = mysql_open();
    
       $query = "SELECT SQL_CALC_FOUND_ROWS id, username, gender, (YEAR(NOW())-YEAR(dateOfBirth)) as age FROM Person";
        if ($name && $gender) 
        {
            $query .= " WHERE name like '%$name%' AND gender like '%$gender%'";
        }
        $query .= " order by id";
        $query .= " LIMIT $offset, $persons_per_page";
    
        $result = mysql_query($query, $connection) or show_error();
    
        $r = mysql_query("SELECT FOUND_ROWS()", $connection) or showerror();
        $r = mysql_fetch_array($r);
        $num_entries = $r[0];
    
        $entries = array();
    
        $person_list = array();
        while ($person = mysql_fetch_array($result)) {
            $person_list[] = $person;
        }
    
        mysql_close($connection) or show_error();
        return array($person_list, $num_entries);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.