Good evening,
I’ve been tearing my hair out over this issue. It must be something simple I’m missing, but, for some reason, I’m not able to track it down, so I thought I would enlist some assistance.
I have some code that is supposed to generate additional data and display it alongside a schedule of employees, based on the user’s access rights.
Any time I run the code where the variable $manager_check in the displaySchedule function equates to a false value (0 or strict boolean), everything executes fine. When it is true, however, I get a one-row table of zeros.
After much testing consisting of die() statements to verify that specific code is, in fact, being executed, I’m finding that, for some reason, my functions are not recognizing that there is data present that has been returned from the database, even after passing checks to ensure that there is, in fact, data that exists.
Again, I’m sure that it must be something simple that I’m missing.
Any help is appreciated.
Code Following:[php]function managerEditScheduleButton($div_id){
return “
<input type=‘button’ value=‘Edit’ onclick='alterSchedule(”".$div_id."")’";
}
function displaySchedule($store_id, $manager_check){
$sql = "SELECT c.*, u.user_firstname, u.user_lastname, u.user_type, p.position_description FROM current_schedule c LEFT JOIN users u on c.user_id = u.user_id LEFT JOIN positions p on u.position_id = p.position_id WHERE c.store_id =".$store_id." ORDER BY u.user_type ASC, u.user_lastname ASC";
$result = dbQuery($sql);
if (dbNumRows($result) > 0){
//create data for managers
if ($manager_check){
$employee_totals_array = createTotalsEmployee($result);
$weekday_totals_array = createTotalsWeekday($result);
$hours_total = createTotalHours($weekday_totals_array);
}
echo "<table width='100%' style='border: 1px solid black;'>";
while($row = dbFetchAssoc($result)){
$code_row = "<tr><td>";
$code_row .= $row['user_firstname']." ".$row['user_lastname']."<br>".$row['position_description']."</td>";
if ($manager_check){
$code_row .= buildSchedule($row, $manager_check, $employee_totals_array[$row['user_id']]);
}else{
var_dump($row);
die("variable NOT recognized.");
$code_row .= buildSchedule($row, $manager_check);
}
echo $code_row."<br>";
}
if ($manager_check){
echo "<tr><td></td>";
foreach($weekday_totals_array as $key => $value){
echo "<td>".$value."</td>";
}
echo "<td>".$hours_total."</td></tr>";
}
echo "</table>";
}
}
function buildSchedule($data, $managerCheck, $emp_hours=""){
$code = “”;
var_dump($data);
die();
foreach($data as $key => $value){
if (substr($key, -2) == "in"){
$code .= "<td class='day' id=".$_SESSION['user_id']."-".$_SESSION['store_id'].">";
$code .= "IN: ";
if($value == NULL || $value == ""){
$code .= "OFF<br>";
}else{
$code .= decodeTime($value)."<br>";
}
}elseif(substr($key, -3) == "out"){
$code .= "OUT: ";
if($value == NULL || $value == ""){
$code .= "OFF";
}else{
$code .= decodeTime($value);
}
if ($managerCheck){
$code .= "<br>".managerEditScheduleButton($_SESSION['user_id']."-".$_SESSION['store_id']);
}
$code .= "</td>";
}
}
if($managerCheck){
$code .= "<td>".$emp_hours."</td>";
}
return $code;
}
function createTotalsEmployee($result){
while($row = dbFetchAssoc($result)){
$total = 0;
foreach($row as $key => $value){
if (substr($key, -2) == “in”){
$temp_total = $value;
}elseif(substr($key, -3) == “out”){
$temp_total -= $value;
$total += $temp_total;
}
}
$total -= 2*$total;
$emp_total[$row[‘user_id’]] = $total;
}
return $emp_total;
}
function createTotalsWeekday($result){
$weekday_total = array(‘sunday’=>0, ‘monday’=>0, ‘tuesday’=>0, ‘wednesday’=>0, ‘thursday’=>0, ‘friday’=>0, ‘saturday’=>0);
while($row = dbFetchAssoc($result)){
foreach($row as $key => $value){
$total = 0;
if (substr($key, -2) == "in"){
$temp_total = $value;
}elseif(substr($key, -3) == "out"){
$temp_total -= $value;
$total += $temp_total;
$pos = strripos($key, "_");
$weekday = substr($key, 0, $pos);
$total -= 2*$total;
$weekday_total[$weekday] += $total;
}
}
}
return $weekday_total;
}
function createTotalHours($weekday_array){
$total = 0;
foreach($weekday_array as $key=>$value){
$total += $value;
}
return $total;
}[/php]
Thanks, Digwood.