sudowned
正しい方向に進んでくれたユーザーからの親切な指示で、プロジェクトを終えることができました。将来の読者のために、これが私のために働いたものです:
これはアルゴリズムです:
$rCards = Get all time entries for specific user within certain date range
$rCardsPJ = Get PID (project_num)s between those dates
Top Loop (through all projects)
$TopLoopPID = get next project_num
$ttl_hrs_this_pj = 0;
Loop through all time entries ($rCards)
get next rCard
if (PID on this rCard == $TopLoopPID) {
echo this row
$hrs_this_rCard = get hrs from this rCard
$ttl_hrs_this_pj += $hrs_this_rCard
}
}
echo 'Total Hours: ' . $ttl_hrs_this_pj;
}
これはコードです:
$rCards = mysql_query("SELECT `t_id`, `date`, `project_num`, `task_desc`, `hours` FROM `timecards` WHERE `staff_id`='$staff_id' AND `date` BETWEEN '$date_start' AND '$date_finish'");
$rCardsPJ = mysql_query("SELECT `project_num`, COUNT(`project_num`) FROM `timecards` WHERE `staff_id`='$staff_id' AND `date` BETWEEN '$date_start' AND '$date_finish' GROUP BY `project_num`");
$num_cards = mysql_num_rows($rCards);
$num_projs = mysql_num_rows($rCardsPJ);
//If no time cards for this period, respond with a single zero
if ($num_cards < 1) {
echo '0';
exit();
}
$t = ''; //Initialize var (necc)
$cntr = 1;
for($i = 1; $i<=$num_projs; $i++){
//Loop through each project
$aPJ = mysql_fetch_assoc($rCardsPJ);
$this_pj = $aPJ['project_num'];
$this_pjCnt = $aPJ['COUNT(`project_num`)'];
//Counter for row striping
$stripe_cnt = 0;
$csv .= '"Date","PJ#","Project Name","Task Description","Hours"'.chr(13);
$t .= '<table style="width:90%;background:white;border-radius:10px;margin:-8px 0 3px 0; padding:10px 20px;box-shadow:2px 2px 5px;">
<tr>
<th width="90">
Date
</th>
<th width="50">
PJ #
</th>
<th width="250">
Project Name
</th>
<th width="500">
Description
</th>
<th width="50">
Hours
</th>
<th width="10">
Edit
</th>
</tr>
';
$ttl_hrs_this_pj = 0;
for($j = 1; $j<=$num_cards; $j++){
$crd = mysql_fetch_assoc($rCards);
$pj = $crd['project_num'];
/*---------------------------------------------------------------------*/
if($pj == $this_pj) {
$date = $crd['date'];
$project_num = add_zeros($crd['project_num'], '3');
$project_name = get_project_name_from_project_num($project_num);
$task = $crd['task_desc'];
$hours = $crd['hours'];
$t_id = $crd['t_id'];
$ttl_hrs_this_pj += $hours;
$stripe_cnt++; //for row striping
$stripe = (is_odd($stripe_cnt)) ? 'class="tr_odd"' : 'class="tr_even"' ;
$t .= '<tr '.$stripe.'>
<td>
'.$date.'
</td>
<td>
'.$project_num.'
</td>
<td>
'.$project_name.'
</td>
<td>
'.$task.'
</td>
<td style="text-align:right;">
'.$hours.'
</td>
<td>
<img id="tc_"'.$t_id.' class="tc_edit" src="images/Edit_Icon.jp">
</td>
</tr>
';
}
}
$t .='<tr>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<span class="SumHoursLabel">Total Hours:</span>
</td>
<td style="text-align:right;">
<span class="SumHoursValue">'.number_format($ttl_hrs_this_pj,2).'</span>
</td>
</tr>
</table>
<br />';
//Reset counter to top for this mysql resource
mysql_data_seek($rCards,0);
$cntr++;
}
echo $t;