Avatar of Jeff
Jeff
 asked on

Passing a variable to a function

I have a function:
function team_agegroup($row) {
  $today = date('m-d-Y');
  $year_today = date("Y");
  $cutdate = "07-31-" . date("Y");
     
  if ($today >= $cutdate)
  {
    $tag = (($year_today - $row['teamyear']) + 1);
  }
  else
  {
    $tag = (($year_today - $row['teamyear']));
  }
  return $tag;
}

The problem is I have several querys and the query I am passing is:
while ($rowt1=mysql_fetch_array($resultt1))

How do I get this to work?
PHP

Avatar of undefined
Last Comment
Roger Baklund

8/22/2022 - Mon
Roger Baklund

You can do it like this:

while ($rowt1=mysql_fetch_array($resultt1)) {
  $tag = team_agegroup($rowt1);
  ...
}

Or am I misunderstanding your question?
Jeff

ASKER
Tried that with no luck. I am sure it is because of $row['teamyear']

if ($today >= $cutdate)
  {
    $tag = (($year_today - $row['teamyear']) + 1);
  }
  else
  {
    $tag = (($year_today - $row['teamyear']));
Roger Baklund

The query that is used to produce $resultt1, does it contain a column named 'teamyear'? If not, it will not work. Two options:

1) alias the column to 'teamyear' in the query: SELECT col1,col2,someyear as teamyear FROM SomeTable

2) change the function:

function team_agegroup($year) {
  $today = date('m-d-Y');
  $year_today = date("Y");
  $cutdate = "07-31-" . date("Y");
     
  if ($today >= $cutdate)
  {
    $tag = (($year_today - $year) + 1);
  }
  else
  {
    $tag = (($year_today - $year));
  }
  return $tag;
}

...and call it like this:

while ($rowt1=mysql_fetch_array($resultt1)) {
  $tag = team_agegroup($rowt1['someyear']);
  ...
}
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Jeff

ASKER
Yes, teamyear is in the query. I am afraid to change the function because it is used in several other areas in the site. There is also another query just below this one for team 2 ($rowt2).
Roger Baklund

If teamyear is in the query it should work. What error do you get?

>> There is also another query just below this one for team 2 ($rowt2).

As long as the query contains the 'teamyear' column, you can call the function in the same way:

while ($rowt2=mysql_fetch_array($resultt2)) {
  $tag = team_agegroup($rowt2);
  ...
}

When the function is executed, the $row variable in the function works as a placeholder for whatever variable you send in to it, it is not a problem that the variables in the function call is called $rowt1 and $rowt2. Even this should work:

$a = array('teamyear'=>1999);
$tag = team_agegroup($a);
Jeff

ASKER
No error, 2009 age group in the results.

The value I get now is 2009, which means
$year_today - $row['teamyear'] or 2009 - 0 = 2009

When I echo $rowt1['teamyear'] I get the correct value (ie 2004)

If I change the function to:
function team_agegroup($rowt1) {
  $today = date('m-d-Y');
  $year_today = date("Y");
  $cutdate = "07-31-" . date("Y");
     
  if ($today >= $cutdate)
  {
    $tag = (($year_today - $rowt1['teamyear']) + 1);
  }
  else
  {
    $tag = (($year_today - $rowt1['teamyear']));
  }
  return $tag;
}

I get the correct results???
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Roger Baklund

>> I get the correct results???

Is that a question? Or are you telling me...?

If it is a question: try it. It should make no difference.

If you are telling me... that was odd! That is not how functions work in php. Please show a larger portion of your code.
Ray Paseur


<?php // RAY_temp_agegroup.php
 
// FUNCTION TO DETERMINE THE TEAM AGE GROUP
function team_agegroup($row) 
{
 
// USE ISO-8601 FORMAT DATES
   $today = date('Y-m-d');
   $year_today = date("Y");
   $cutdate = year_today . '-07-31';
 
// DETERMINE THE CUT DATE     
   if ($today >= $cutdate)
   {
      $tag = ($year_today - $row['teamyear']) + 1;
   } else
   {
      $tag = $year_today - $row['teamyear'];
   }
   return $tag;
}
 
 
// HOW TO USE THIS ON A ROW OF DATA
// ASSUME QUERY DONE AND RESULTS SET IN $res
while ($row = mysql_fetch_assoc($res))
{
   $tag = team_agegroup($row);
   echo "<br/>$tag";
}

Open in new window

Roger Baklund

Ray, that was my first instinct too, but a closer inpection reveals that the year is allways the same, so comparing with a m-d-Y format should work. The asker say this code is used by other areas of the site, so I think the function in itself is ok.

jeffey48, how are you using the $tag variable after it is fetched from the function? Please show the entire while loop.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Jeff

ASKER
Here is the code. I had to coment the function and hard code it to get it working for now.

FUNCTION
// Team Age Group
function team_agegroup($row) {
  $today = date('m-d-Y');
  $year_today = date("Y");
  $cutdate = "07-31-" . date("Y");
     
  if ($today >= $cutdate)
  {
    $tag = (($year_today - $row['teamyear']) + 1);
  }
  else
  {
    $tag = (($year_today - $row['teamyear']));
  }
  return $tag;
}


PAGE CODE
$sqlteam1 = "SELECT * FROM tbl_team WHERE associationid = '" . $_SESSION['associationid'] ."' AND teamactive='1'";
    if ((isset($_SESSION['setTeamAgeGroup1'])) AND ($_SESSION['setTeamAgeGroup1'] != ""))
    {
        $AgeGroup = $_SESSION['setTeamAgeGroup1'];
    }
    if (isset($AgeGroup)) {
        $year = date("Y");
        if ($season == "Spring")
    {
        $teamyear = ($year - $AgeGroup) - 1;
    }
    if ($season = "Fall")
    {
        $teamyear = ($year - $AgeGroup);
    }

    $sqlteam1 = $sqlteam1 . " AND (teamyear = '$teamyear'";
    if ((isset($_SESSION['setTeamAgeGroup1'])) AND ($_SESSION['setTeamAgeGroup1'] != ""))
    {
        $AgeGroup = $_SESSION['setTeamAgeGroup1'];
        $teamyear = team_year_agrp($AgeGroup);
        $sqlteam1 = $sqlteam1 . " OR teamyear = '$teamyear')";
    }
    else
    {
        $sqlteam1 = $sqlteam1 . ")";
    }
}
if ((isset($_SESSION['setTeamGender'])) AND ($_SESSION['setTeamGender'] != ""))
{
    if (($_SESSION['setTeamGender']) == "B") { $sqlteam1 = $sqlteam1 . " AND teamgender = 'M'"; }
    if (($_SESSION['setTeamGender']) == "G") { $sqlteam1 = $sqlteam1 . " AND teamgender = 'F'"; }
}
$sqlteam1 = $sqlteam1 . " ORDER BY teamname";
if ($debug) echo "<br>Home Team - $sqlteam1<br>";
    $resultt1 = mysql_query($sqlteam1);
?>
<td nowrap valign="middle" id="text2b">
  <select name="Team1ID">
    <option value="0">Select Team</option>
<?php while ($rowt1=mysql_fetch_array($resultt1)) { ?>
    <option value="<? echo $rowt1['teamid']; ?>"<?php if ($rowt1['teamid'] == $rowSched['team1id']) { echo " selected"; }?>><?php echo $rowt1['teamname'];
//  $tag = team_agegroup($row);
//
  $today = date('m-d-Y');
  $year_today = date("Y");
  $cutdate = "07-31-" . date("Y");
     
  if ($today >= $cutdate)
  {
    $tag = (($year_today - $rowt1['teamyear']) + 1);
  }
  else
  {
    $tag = (($year_today - $rowt1['teamyear']));
  }
//

     echo " U-$tag" ;
     echo $rowt1['teamgender'];
  if ($rowt1['teamclass'] == '1') { echo " Rec"; } elseif ($rowt1['teamclass'] == '2') { echo " Comp"; }
  elseif ($rowt1['teamclass'] == '3')
  { echo " Adult"; }
    echo "</option>\n";
  } ?>
</select>
Ray Paseur

If the question is about how you pass arguments to a function and how you retrieve the results, this may be what you're looking for.
FUNCTION
// Team Age Group
function team_agegroup($row) {
  $today = date('m-d-Y');
  $year_today = date("Y");
  $cutdate = "07-31-" . date("Y");
     
  if ($today >= $cutdate)
  {
    $tag = (($year_today - $row['teamyear']) + 1);
  }
  else
  {
    $tag = (($year_today - $row['teamyear']));
  }
  return $tag;
}
 
 
PAGE CODE
$sqlteam1 = "SELECT * FROM tbl_team WHERE associationid = '" . $_SESSION['associationid'] ."' AND teamactive='1'";
    if ((isset($_SESSION['setTeamAgeGroup1'])) AND ($_SESSION['setTeamAgeGroup1'] != ""))
    {
        $AgeGroup = $_SESSION['setTeamAgeGroup1'];
    }
    if (isset($AgeGroup)) {
        $year = date("Y");
        if ($season == "Spring")
    {
        $teamyear = ($year - $AgeGroup) - 1;
    }
    if ($season = "Fall")
    {
        $teamyear = ($year - $AgeGroup);
    }
 
    $sqlteam1 = $sqlteam1 . " AND (teamyear = '$teamyear'";
    if ((isset($_SESSION['setTeamAgeGroup1'])) AND ($_SESSION['setTeamAgeGroup1'] != ""))
    {
        $AgeGroup = $_SESSION['setTeamAgeGroup1'];
        $teamyear = team_year_agrp($AgeGroup);
        $sqlteam1 = $sqlteam1 . " OR teamyear = '$teamyear')";
    }
    else
    {
        $sqlteam1 = $sqlteam1 . ")";
    }
}
if ((isset($_SESSION['setTeamGender'])) AND ($_SESSION['setTeamGender'] != ""))
{
    if (($_SESSION['setTeamGender']) == "B") { $sqlteam1 = $sqlteam1 . " AND teamgender = 'M'"; }
    if (($_SESSION['setTeamGender']) == "G") { $sqlteam1 = $sqlteam1 . " AND teamgender = 'F'"; }
}
$sqlteam1 = $sqlteam1 . " ORDER BY teamname";
if ($debug) echo "<br>Home Team - $sqlteam1<br>";
    $resultt1 = mysql_query($sqlteam1);
?>
<td nowrap valign="middle" id="text2b">
  <select name="Team1ID">
    <option value="0">Select Team</option>
<?php while ($rowt1=mysql_fetch_array($resultt1)) { ?>
    <option value="<? echo $rowt1['teamid']; ?>"<?php if ($rowt1['teamid'] == $rowSched['team1id']) { echo " selected"; }?>><?php echo $rowt1['teamname'];
    $tag = team_agegroup($row);
     echo " U-$tag" ;
     echo $rowt1['teamgender'];
  if ($rowt1['teamclass'] == '1') { echo " Rec"; } elseif ($rowt1['teamclass'] == '2') { echo " Comp"; }
  elseif ($rowt1['teamclass'] == '3')
  { echo " Adult"; }
    echo "</option>\n";
  } ?>
</select>

Open in new window

Ray Paseur

As far as date processing goes, here are the general guidelines for a safe approach.

All internal representations of DATE, TIME and DATETIME should use the ISO8601 date format.  In PHP this is date('c') or date('Y-m-d H:i:s').  These formats have several benefits.  They are all fixed-length strings, so they are easy to line up in columns.  They collate correctly when they are sorted and compared.  They match up perfectly with the MySQL DATETIME fields.

All external representations are at the discretion of the designer.  We call those things "pretty dates" because they are in a more human-friendly format.

Translation between the ISO date and the pretty date is done via date() and strtotime() like the example in the code snippet here.

If you follow this practice you will find that you can avoid a huge amount of heavy lifting when you're dealing with date and time issues in your programming.

Best to all, ~Ray
echo date('l, F j, Y G:ia', strtotime('2009-03-15 12:18:15'));

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Roger Baklund

The commented line:

//  $tag = team_agegroup($row);

Here you are using $row, but the variable fetched is called $rowt1. It should be:

$tag = team_agegroup($rowt1);

How does this work:
$sqlteam1 = "SELECT * FROM tbl_team WHERE associationid = '" . $_SESSION['associationid'] ."' AND teamactive='1'";
    if ((isset($_SESSION['setTeamAgeGroup1'])) AND ($_SESSION['setTeamAgeGroup1'] != ""))
    {
        $AgeGroup = $_SESSION['setTeamAgeGroup1'];
    }
    if (isset($AgeGroup)) {
        $year = date("Y");
        if ($season == "Spring")
    {
        $teamyear = ($year - $AgeGroup) - 1;
    }
    if ($season = "Fall")
    {
        $teamyear = ($year - $AgeGroup);
    }
 
    $sqlteam1 = $sqlteam1 . " AND (teamyear = '$teamyear'";
    if ((isset($_SESSION['setTeamAgeGroup1'])) AND ($_SESSION['setTeamAgeGroup1'] != ""))
    {
        $AgeGroup = $_SESSION['setTeamAgeGroup1'];
        $teamyear = team_year_agrp($AgeGroup);
        $sqlteam1 = $sqlteam1 . " OR teamyear = '$teamyear')";
    }
    else
    {
        $sqlteam1 = $sqlteam1 . ")";
    }
}
if ((isset($_SESSION['setTeamGender'])) AND ($_SESSION['setTeamGender'] != ""))
{
    if (($_SESSION['setTeamGender']) == "B") { $sqlteam1 = $sqlteam1 . " AND teamgender = 'M'"; }
    if (($_SESSION['setTeamGender']) == "G") { $sqlteam1 = $sqlteam1 . " AND teamgender = 'F'"; }
}
$sqlteam1 = $sqlteam1 . " ORDER BY teamname";
if ($debug) echo "<br>Home Team - $sqlteam1<br>";
    $resultt1 = mysql_query($sqlteam1);
?>
<td nowrap valign="middle" id="text2b">
  <select name="Team1ID">
    <option value="0">Select Team</option>
<?php while ($rowt1=mysql_fetch_array($resultt1)) { ?>
    <option value="<? echo $rowt1['teamid']; ?>"<?php if ($rowt1['teamid'] == $rowSched['team1id']) { echo " selected"; }?>><?php echo $rowt1['teamname'];
$tag = team_agegroup($rowt1);
/*
  $today = date('m-d-Y');
  $year_today = date("Y");
  $cutdate = "07-31-" . date("Y");
     
  if ($today >= $cutdate)
  {
    $tag = (($year_today - $rowt1['teamyear']) + 1);
  }
  else
  {
    $tag = (($year_today - $rowt1['teamyear']));
  }
*/
     echo " U-$tag" ;
     echo $rowt1['teamgender'];
  if ($rowt1['teamclass'] == '1') { echo " Rec"; } elseif ($rowt1['teamclass'] == '2') { echo " Comp"; }
  elseif ($rowt1['teamclass'] == '3')
  { echo " Adult"; }
    echo "</option>\n";
  } ?>
</select>

Open in new window

SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jeff

ASKER
I cannot test right now as they are adding data, however from my testing last night when I use $rowt1 ($tag = team_agegroup($rowt1);) I got 2009.

I think this is because the var $rowt1['teamyear'] is not getting passed to $row['teamyear']) :
if ($today >= $cutdate)
  {
    $tag = (($year_today - $row['teamyear']) + 1);
  }
  else
  {
    $tag = (($year_today - $row['teamyear']));
  }
  return $tag;
}

Therefore $year_today - $row['teamyear'] is equal to 2009-0=2009

It doesn't seem to be pulling $rowt1['teamyear'] into $row['teamyear']

Sorry crx. The ??? are confusion!
I get the correct results??? meant Correct results if I change
$tag = (($year_today - $row['teamyear']));
to $tag = (($year_today - $rowt1['teamyear']));
but don't know why it doesn't work with $row['teamyear']

Jeff
ASKER CERTIFIED SOLUTION
Roger Baklund

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.