切换案例问题与默认值

时间:2014-06-24 09:31:51

标签: php codeigniter conditional

我在PHP中使用Switch Case语句运行时出现了一些奇怪的情况,它在某种程度上忽略了这种情况并抛出了默认值。但是,这不仅限于Switch Case,也适用于if else。因此,除了条件检查之外,我可能会做错事。

我正在使用编码器,下面我发布了所有代码和帮助器。我希望这是足够的信息,但如果您需要更多信息,请告诉我。

助手

// get employees
function get_employees(array $array = array())
{
    $CI = get_instance();
    return $CI->db->get_where('employees', $array)->result();
}

// get employee status from the `employment_status`
function get_employee_status($id)
{
    $result = get_employees(array('id' => $id));

    foreach ($result as $employee)
    {
        $status = $employee->employment_status;
    }

    return ($status !== '') ? $status : 'none';
}


// get employee status icon (based on status)
function get_employee_status_icon($id, $tooltip = TRUE)
{
    $status = get_employee_status($id);

    $get_icon = ($tooltip) ? 'rel="tooltip" title="' . ucwords(str_replace('_', ' ', $status)) . '"' : NULL;

    switch ($status)
    {
        case 'active':
            $status_icon = '<span class="glyphicon glyphicon-ok" ' . $get_icon . '></span>';
            break;

        case 'at_risK':
            $status_icon = '<span class="glyphicon glyphicon-warning-sign" ' . $get_icon . '></span>';
            break;

        case 'attrition':
            $status_icon = '<span class="glyphicon glyphicon-ban-circle" ' . $get_icon . '></span>';
            break;

        default:
            $status_icon = '<span class="glyphicon glyphicon-exclamation-sign" ' . $get_icon . '></span>';
            break;
    }
    return $status_icon;
}

控制器

public function employees()
{
    $this->data['title'] = '<i class="fa fa-users"></i> ' . lang('emp_all');

    $base_url   = base_url() . 'admin/hr/employees';
    $numbs      = $this->employees_model->filter_count();
    $total_rows = $numbs['count'];
    $limit      = get_option('per_page');
    $page       = ($this->uri->segment(4) !== FALSE) ? (int) $this->uri->segment(4) : 0;

    get_pagination($base_url, $total_rows, $limit, 4, 2, TRUE);

    $this->data['results'] = $this->employees_model->fetch_rows($limit, $page);
    $this->data['links']   = $this->pagination->create_links();

    $this->load->view('hr/employees/index', $this->data);
}

查看文件

// view file
foreach ($results as $employee):

    do_print(get_employee_status($employee->id) . ' - ' .get_employee_status_icon($employee->id));    
    echo '<tr>';
    ...
    echo '<td class="status-' . get_employee_status($employee->id) . '">' . get_employee_status_icon($employee->id) . '</td>';
    ...
    echo '</tr>';
endforeach;

要再次清除事物:代码输出最后default的{​​{1}}值(图标)。它忽略了仅针对ICON的最后一种情况,而不是工具提示甚至是case

那么如何才能解决这个问题呢?我可以在各处获得与输出相同的输出?

编辑:---添加了输出图像和var_dump图像

请查看最后一个var_dump并输出结果以匹配th图标。哪个错了

enter image description here

HTML输出

enter image description here

的var_dump()

enter image description here

1 个答案:

答案 0 :(得分:0)

首先,你应该通过正确初始化$ status变量来纠正你的这个功能;

// get employee status from the `employment_status`
function get_employee_status($id)
{
    $result = get_employees(array('id' => $id));

    $status = '';
    foreach ($result as $employee)
    {
        $status = $employee->employment_status;
    }

    return ($status !== '') ? $status : 'none';
}

现在,您应该执行var_dump $ status并查看您在状态变量值中找到的内容。