PHP Sum状态列

时间:2011-08-25 20:51:00

标签: php mysql

我有一个PHP脚本,我试图将状态类型总结为变量。例如,当我执行以下操作时:

echo $val['status'];

我明白了:

  

新的新死亡新进程新的新的新死亡新死亡新死亡新的新进程

我想要做的是按状态类型加总所有这些并将它们放入变量中:

$statusnew
$statusdead
$statusinprocess

所以,如果我要做的话:

echo $statusnew;

我将被呈现为“12”,依此类推。我是PHP的新手,所以我试图找出最有效的方法来做到这一点。我知道有一个array_sum函数,但不确定它是否可以在这里工作,因为我们没有求和数值。

循环:

if($_SESSION['partner'] == 1)
{


 $query = "select l.id as lead_id , l.account_name as account_name , l.first_name as first_name , l.last_name as last_name,l.date_entered as date_entered ,  lc.endpoints_c as endpoints ,  l.status as status from leads  as l
                    join leads_cstm  as lc on lc.id_c = l.id                        
                    where lc.account_id_c = '".$_SESSION['account_id']."'
                    and l.deleted = '0' and l.status <> 'Converted' order by date_entered DESC";


 $db -> PS_Pagination($query, 20, 5, "");
 $db -> setDebug(true);
 $rs = $db->paginate();
 $num_rows = mysql_num_rows($rs);

在脚本中进一步向下:

        while($val = mysql_fetch_assoc($rs)) 
        {
             $date_create = date("m/d/Y", strtotime($val['date_entered']));

我刚从@Joseph添加的内容:

        $count = array(); // for graph

    // then, in your loop:
    if ( array_key_exists($val['status'], $count) )
    $count[$val['status']]++;
    else
    $count[$val['status']] = 1;

    echo $count['New'];

1 个答案:

答案 0 :(得分:2)

$count = array();

while($val = mysql_fetch_assoc($rs)) 
{
    $date_create = date("m/d/Y", strtotime($val['date_entered']));

    if ( array_key_exists($val['status'], $count) )
        $count[$val['status']]++;
    else
        $count[$val['status']] = 1;

    // continue with everything else being done in this loop
}

运行循环后,您可以像这样访问计数:

$count['new']; // 12
$count['dead']; // 4
$count['in proccess']; // 2