PHP:帮助解决这个逻辑

发布于 2024-10-02 22:16:44 字数 1406 浏览 0 评论 0原文

我有一个问题。我有一个支持票表(wh_task)。每个任务都有一个 date_completed (d/m/Y) 和 has_met_sla 字段(0 或 -1)。我想允许用户按 date_completed 搜索此表并根据结果显示图表。

图表数据必须如下所示,以便我可以填充条形图(融合图表):

年份:2010 |月份: 11 月 | SLA 满足:12 |错过的 SLA:2

年:2010 年 |月份: 10 月 | SLA 满足:15 |错过的 SLA:1

该图表的 x 轴为数字,y 轴为“2010 年 11 月”。沿 y 的每个月都有 2 列,已满足和未满足。

所以,我可以毫无问题地创建这种图表,但它生成的数据是我无法想出的。以下是我的问题:

        $tsql = "SELECT task_id, has_met_service_level_agreement, date_completed ".
                "FROM wh_task ".
                "WHERE (task_status_id = 5) AND (account_id =$atid)";

        $stmt = sqlsrv_query( $conn, $tsql);
        if( $stmt === false)
        {
                 echo "Error in query preparation/execution.\n";
                 die( print_r( sqlsrv_errors(), true));
        }


        //SLA counters
        $met = 0;
        $missed = 0;



        /* Retrieve each row as an associative array and display the results.*/
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
        {
            $date = $row['date_completed'];
            $monthnumber = date_format($date, "n");
            $year = date_format($date, "Y");
            $hasmetsla = $row['has_met_service_level_agreement'];

        }


    }

你能帮我解释一下这里的逻辑吗?我猜我需要将数据存储在包含月份、年份、已满足总数和未满足总数的数组中。然后,对于每个任务,检查数组中是否已存在年月组合,如果存在,则根据 $hasmetsla 修改总计,如果不存在,则将其添加到数组中?

谢谢大家!

琼西

I have a problem. I have a table of support tickets (wh_task). Each task has a date_completed (d/m/Y) and has_met_sla field (0 or -1). I want to allow the user to search this table by date_completed and display a chart based on the results.

The charts data has to look like this so I can populate a barchart (fusion charts):

Year: 2010 | Month: Nov | SLA Met: 12 | SLA Missed: 2

Year: 2010 | Month: Oct | SLA Met: 15 | SLA Missed: 1

The chart will have the numbers up the x-axis and "Nov 2010" along the y. Each month along the y has 2 columns, met and not met.

so, I can create this kind of chart no problem but it's generating the data I'm having trouble coming up with. Below is my query:

        $tsql = "SELECT task_id, has_met_service_level_agreement, date_completed ".
                "FROM wh_task ".
                "WHERE (task_status_id = 5) AND (account_id =$atid)";

        $stmt = sqlsrv_query( $conn, $tsql);
        if( $stmt === false)
        {
                 echo "Error in query preparation/execution.\n";
                 die( print_r( sqlsrv_errors(), true));
        }


        //SLA counters
        $met = 0;
        $missed = 0;



        /* Retrieve each row as an associative array and display the results.*/
        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
        {
            $date = $row['date_completed'];
            $monthnumber = date_format($date, "n");
            $year = date_format($date, "Y");
            $hasmetsla = $row['has_met_service_level_agreement'];

        }


    }

Can you give me a hand with the logic here? I'm guessing I need to store the data in an array containing the month, the year, the met total, and the not met total. Then for each task check if the year month combination already exist in the array and if so ammend the totals based on $hasmetsla and if not add it to array??

Thanks all!

Jonesy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过去的过去 2024-10-09 22:16:44

我认为你最好在 SQL 中进行这种处理。使用两步解决方案:首先,找到一个查询,为您提供每行的年、月和 sla_met。把它放在视图中。然后对该视图进行查询,使用 group_by、sum() 和 count() 的巧妙组合来计算所需的结果:

CREATE VIEW vw_sla AS SELECT DATEPART(year, date_completed) AS year, DATEPART(month, date_completed), CASE has_met_service_level_agreement WHEN 0 THEN 0 ELSE 1 END as sla_met

SELECT year, month, sum(sla_met), count(*) - sum(sla_met) FROM vw_sla GROUP BY year, month ORDER BY year DESC month DESC

您所需要做的就是从数据库中获取数据,并将其显示在表中。

I think you're better off doing this kind of processing in SQL. Use a two-step solution: First, find a query that gives you the year, month, and sla_met for each row. Put that in a view. Then do a query on that view, using a clever combination of group_by, sum() and count() to calculate the desired result:

CREATE VIEW vw_sla AS SELECT DATEPART(year, date_completed) AS year, DATEPART(month, date_completed), CASE has_met_service_level_agreement WHEN 0 THEN 0 ELSE 1 END as sla_met

SELECT year, month, sum(sla_met), count(*) - sum(sla_met) FROM vw_sla GROUP BY year, month ORDER BY year DESC month DESC

The all you need to do is get the data from the database, and display it in a table.

温暖的光 2024-10-09 22:16:44

如果我必须在 PHP 中执行此操作,我会这样做(使用 SQL 将是更好的选择,但这是事后执行此操作的一种方法):

首先,我将设置一个具有以下结构的多维数组:

array(
    'year1' => array(
        'month1' => array(
            'met' => 0,
            'missed' => 0,
        ),
    ),
),

然后,我将更改 while 循环以执行如下操作:

$yearInfo = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
    $date = $row['date_completed'];
    $monthnumber = date_format($date, "n");
    $year = date_format($date, "Y");
    $hasmetsla = $row['has_met_service_level_agreement'];
    if (!isset($yearInfo[$year])) {
        $yearInfo[$year] = array(
            $monthnumber => array(
                'met' => 0, 
                'missed' => 0
            )
        );
    } elseif (!isset($yearInfo[$year][$monthnumber])) {
        $yearInfo[$year][$monthnumber] = array(
            'met' => 0,
            'missed' => 0,
        );
    }
    $key = $hasmetsla ? 'met' : 'missed';
    $yearInfo[$year][$monthnumber][$key]++;
}

然后,当您显示时:

$data = '';
foreach ($yearInfo as $year => $months) {
    foreach ($months as $month => $status) {
        $data .= 'Year: '.$year.' | '.
              'Month: '.$month.' | '.
              'SLA Met: '.$status['met'].' | '.
              'SLA Missed: '.$status['missed']."\n";
    }
}

Here's what I would do if I had to do this in PHP (using SQL would be the better option, but here's one method of doing it post-facto):

First, I'd setup a multi-dimensional array with the following structure:

array(
    'year1' => array(
        'month1' => array(
            'met' => 0,
            'missed' => 0,
        ),
    ),
),

Then, I'd change the while loop to do something like this:

$yearInfo = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
    $date = $row['date_completed'];
    $monthnumber = date_format($date, "n");
    $year = date_format($date, "Y");
    $hasmetsla = $row['has_met_service_level_agreement'];
    if (!isset($yearInfo[$year])) {
        $yearInfo[$year] = array(
            $monthnumber => array(
                'met' => 0, 
                'missed' => 0
            )
        );
    } elseif (!isset($yearInfo[$year][$monthnumber])) {
        $yearInfo[$year][$monthnumber] = array(
            'met' => 0,
            'missed' => 0,
        );
    }
    $key = $hasmetsla ? 'met' : 'missed';
    $yearInfo[$year][$monthnumber][$key]++;
}

Then, when you display:

$data = '';
foreach ($yearInfo as $year => $months) {
    foreach ($months as $month => $status) {
        $data .= 'Year: '.$year.' | '.
              'Month: '.$month.' | '.
              'SLA Met: '.$status['met'].' | '.
              'SLA Missed: '.$status['missed']."\n";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文