Thursday, 15 April 2010

php - Switch Case issue with default value -



php - Switch Case issue with default value -

i running weird situation switch case statement in php, somehow ignoring case , throwing default value. not limited switch case , happening if else well. might wrong doing apart conditional check.

i using codeingiter , below posting code , helpers. hope plenty info allow me know if need more information.

helpers

// employees function get_employees(array $array = array()) { $ci = get_instance(); homecoming $ci->db->get_where('employees', $array)->result(); } // employee status `employment_status` function get_employee_status($id) { $result = get_employees(array('id' => $id)); foreach ($result $employee) { $status = $employee->employment_status; } homecoming ($status !== '') ? $status : 'none'; } // 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; } homecoming $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 $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: code outputs default value (icon) lastly case. ignoring lastly case icon , not tooltip or th class

so how can prepare can output same case everywhere?

edit: --- added output images , var_dump image

please see sec lastly var_dump , output result match at_risk icon. wrong

html output

var_dump()

first of should right function initializing $status variable;

// employee status `employment_status` function get_employee_status($id) { $result = get_employees(array('id' => $id)); $status = ''; foreach ($result $employee) { $status = $employee->employment_status; } homecoming ($status !== '') ? $status : 'none'; }

now, should var_dump of $status , see find in status variable value.

php codeigniter conditional

No comments:

Post a Comment