1

设计在表格中显示的报告(PHP 输出 HTML)。数据看起来像这样(着色不是我的):

Date         PID  Client Name  Activity         Hours
2012-10-15   52   Company Y    Report writing   3.5
2012-10-15   16   Company D    Meeting          1.3
2012-10-15   21   Company F    Telecon          0.3
2012-10-15   52   Company Y    Telecon          0.8
2012-10-16   16   Company D    Client visit     1.7
2012-10-16   52   Company Y    Preparation      1.8
2012-10-16   16   Company D    Edit MMS 12      7.1

我已经编写了从 MySQL 获取数据、逐行提取数据mysql_fetch_assoc、构建包含值的 HTML 表并输出的所有内容。基本的东西——通过 jQuery 调用 Ajax 来完成,看起来很棒。

我现在被要求按 CID/CompanyName(相同的东西)和输出(我认为是单独的表格)对项目进行分组,并在每个 companytable 下方汇总每个公司的总小时数。

我只想使用一个 mysql_query,这意味着在 PHP 中进行组/计算。

我刚刚度过了一个为期 3 天的周末,完成了数据库重组并编写了初始报告,但我很难考虑清楚这一点。一个善良的灵魂会帮助我解决这个问题吗?我已经开始了几种不同的算法,它们的意大利面条失控了。我知道有一种优雅、简单的方法,但目前我无法想象它。

4

1 回答 1

0

在用户的善意指导下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;
于 2012-11-19T18:13:03.053 回答