简体   繁体   中英

Switch Case issue with default value

I am running with some weird situation with Switch Case statement in PHP, where it is somehow ignoring the case and throwing default value. However this is not limited to the Switch Case only and happening with if else as well. So might be something wrong I am doing apart from conditional check.

I am using codeingiter and below I am posting all my code and helpers. I hope this would be enough information but let me know if you need any more information.

Helpers

// 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;
}

Controller

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

// 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;

To clear things again: the code outputs the default value (icon) for the last case . It is ignoring the last case just only for the ICON and not for the tooltip or even th class

So how can I fix this where I can get output same as the case everywhere?

EDIT: --- Added output images and var_dump image

Please see the second last var_dump and output result to match the at_risk icon. Which is wrong

在此处输入图片说明

HTML Output

在此处输入图片说明

var_dump()

在此处输入图片说明

First of all you should correct your this function by properly initializing $status variable;

// 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';
}

Now, you should do var_dump of $status and see what you find in status variable value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM