array_combine和关联结果

时间:2013-10-07 10:54:48

标签: php yii

我正在使用YII框架

$job_status = Yii::app()->db->createCommand()
            ->select('count(*) as total_count, Image_status')
            ->from('AllDetail')
            ->where("JobID=:job_id",array(':job_id'=>$job_id))
            ->group('Image_status')
            ->queryAll();
$status_key = array();
$images = array();
foreach($job_status as $job){
    $imgstate = array_push($status_key,CompClass::statusMaster($job['Image_status']));
    $tot_img = array_push($images,$job['total_count']);
}

Status Master指的是我的组件类中的一个函数

哪个

public static function statusMaster($status_code)
{
    switch($status_code)
    {
        case 0:
        $alias = 'QUEUE';
        break;
            case 1:
        $alias = 'PROCESSING';
        break;
            case 2:
        $alias = 'COMPLETED';
        break;

    }
            return $alias;
        }

这是我的观点

<strong>Queued: <?php echo $job_status['QUEUE']; ?></strong><br />
<strong>Processing: <?php echo $job_status['PROCESSING']; ?></strong><br />
<strong>Completed: <?php echo $job_status['COMPLETED']; ?></strong><br />

如果上面的脚本没有任何status_code(即)

,则会抛出错误

如果数据库中的图片有status_code 0 and 1 but not 2,我会收到undefined index completed。相反,它应显示为0

我是否可以使用任何其他有效方法的异常处理

2 个答案:

答案 0 :(得分:0)

在查询中为每个'image_status'设置一个值,然后保存您调用“statusMaster($ status_code)”函数。然后在foreach($ job_status)中的$ job对象中有正确的值。

$job_status = Yii::app()->db->createCommand()
            ->select('count(*) as total_count, CASE Image_status
                                                    WHEN 0 THEN "QUEUE"
                                                    WHEN 1 THEN "PROCESSING"
                                                    WHEN 2 THEN "COMPLETED"
                                                    ELSE "DEFOULT VALUE"
                                                    END as Image_status ')
            ->from('AllDetail')
            ->where("JobID=:job_id",array(':job_id'=>$job_id))
            ->group('Image_status')
            ->queryAll();

视图

<strong>Queued: <?php echo isset($job_status['QUEUE'])?  $job_status['QUEUE'] : ''; ?></strong><br />
<strong>Processing: <?php isset($job_status['PROCESSING'])?  $job_status['PROCESSING'] : ''; ?></strong><br />
<strong>Completed: <?php isset($job_status['COMPLETED'])?  $job_status['COMPLETED'] : '';?></strong><br />

答案 1 :(得分:0)

您尝试根据其代表编号设置状态,请尝试以下操作:

<strong>Queued: <?php echo CompClass::statusMaster($job['Image_status']); ?></strong><br />

并且对于未定义的使用isset()和!empty()以确保您处理有效变量。

这就是全部!