kentcommunications
asked on
Making a function work, problem with variables
I'm getting this error when I run my code:
Notice: Undefined variable: arrcar in test.php on line 166
I know that the error, involving arrcar, is only happening if the column "update" is completely empty. If there is any data, including data that doesn't meet the requirements of $ndays, it works fine. How do I set arrcar in the function so that, even if the update column is empty, the page won't display an error message?
Notice: Undefined variable: arrcar in test.php on line 166
I know that the error, involving arrcar, is only happening if the column "update" is completely empty. If there is any data, including data that doesn't meet the requirements of $ndays, it works fine. How do I set arrcar in the function so that, even if the update column is empty, the page won't display an error message?
<?php do { ?>
<?php
//tests to see if the last change value is empty (therefore never set)
if (isset($row_taskscar['update'])) {
$thisday = date("Y-m-d");
//sets the last change date to a variable $date
$date = $row_taskscar['update'];
//format the date for mktime() function -- you need day, month, year
$year = substr($date, 0,4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);
//sets the frequency to a variable $freq
$freq = $row_taskscar['freq'];
//sets the variable to hold the new date. Calculates the new date by
//evaluating the lastchange date and adding the frequency amount measured in months
$new_datecar = date("Y-m-d", mktime(0, 0, 0, $month +$freq, $day , $year));
date_diff("$thisday", "$new_datecar");
if ($ndays <= 15) {
global $arrcar;
$arrcar[] = array('task' => $row_taskscar['task'], 'adate' => $new_datecar);
}
}
?>
<?php } while ($row_taskscar = mysql_fetch_assoc($taskscar));
function funcar($arrcar) {
if(!is_array($arrcar))
{ return false; }
foreach($arrcar as $element) {
$car_taskelm = $element["task"];
$car_dateelm = $element["adate"];
echo "<b>$car_taskelm</b> is due on <b>$car_dateelm</b><br /><br />";
}
}
funcar($arrcar);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I removed the global line, and added in $arrcar = array(); at line 3, but now nothing is displaying, even though I know there should be (and was).
Using
<pre><?php print_r($arrcar); ?></pre>
It's showing the array as empty, though it shouldn't be.
Using
<pre><?php print_r($arrcar); ?></pre>
It's showing the array as empty, though it shouldn't be.
In that case the "shouldn't be (empty)" part is due to a data dependent problem of some sort.
In the snippet above $ndays is undefined, but the code tests it on line 27. So you might want to look at that variable - where is it set?
Also, consider making your life simpler by following the indentation guidelines for conditional and control statements, like this in the code snippet. Make the curly braces line up, and it will be much easier to read and understand the intent of the code!
HTH, ~Ray
In the snippet above $ndays is undefined, but the code tests it on line 27. So you might want to look at that variable - where is it set?
Also, consider making your life simpler by following the indentation guidelines for conditional and control statements, like this in the code snippet. Make the curly braces line up, and it will be much easier to read and understand the intent of the code!
HTH, ~Ray
if ($ndays <= 15)
{
$arrcar[] = array(
'task' => $row_taskscar['task'],
'adate' => $new_datecar
);
}
ASKER
I figured it out. arrcar = array(); was inside the do...while loop. When I declare it beforehand, it's properly displaying. Thanks!
Aha! Good catch.
Best, ~Ray