所以我有4个院系,他们将通过将$fn
参数public function current()
提供给Faculty Controller
下的class Faculty extends CI_Controller {
public $data=array();
public function __construct(){
parent::__construct();
$this->load->model('Facultydata');
$this->load->helper('res');
$data['facData']=$this->Facultydata->getFacData();
}
public function current($fn=NULL)
{
$data= $this->data;
$this->load->view('header2',$data);
switch ($fn) {
case NULL:
redirect(base_url().'/home/');
break;
case 'faculty1':
$data['facData']=$this->Facultydata->getSpFacData(1);
$this->load->view('faculty',$data);
break;
case 'faculty2':
$data['facData']=$this->Facultydata->getSpFacData(2);
$this->load->view('faculty',$data);
break;
case 'faculty3':
$data['facData']=$this->Facultydata->getSpFacData(3);
$this->load->view('faculty',$data);
break;
case 'faculty4':
$data['facData']=$this->Facultydata->getSpFacData(4);
$this->load->view('faculty',$data);
break;
default:
show_404();
break;
}
$this->load->view('footer',$data);
}
}
来生成个别详细信息页面。现在我知道目前有4个院系所以我手动制作了这些案例。我想改变那个&产生的情况直到数据库中没有系数,即没有。教师表中的行(表的名称是" uc_faculty")。仅当faculty1..faculty4作为参数给出时,该函数才会生成数据。在Null的情况下,它重定向&对于其他人,它给出了404。
控制器 - >
class Facultydata extends CI_Model
{
public $return = [];
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getFacData()
{
$q=$this->db->get('uc_faculty');
return $q->result();
}
public function getSpFacData($tI)
{
$tId="1000".$tI;
$this->db->select('*');
$this->db->from('uc_faculty');
$this->db->where('uc_admin.adm_id',$tId);
$this->db->join('uc_admin', 'uc_admin.adm_id = uc_faculty.fac_id');
$q= $this->db->get();
$error = $q->num_rows() > 0 ? FALSE : TRUE;
if ($error === FALSE)
{
return $q->result();
}
else{
return $error;
}
}
}
模型 - >
NSLocationWhenInUseUsageDescription
我不关心转换案例..如果...也欢迎。我只需要根据uc_faculty表中可用的行数设置可能的参数
答案 0 :(得分:3)
开关不是处理此问题的最佳方法,您应该尝试从字符串中获取数字,进行一些检查并获得结果:
if (is_null($fn)) {
redirect(base_url().'/home/');
} else {
$facultyNumber = (int) explode("faculty", $fn)[1];
if (!$facultyNumber) {
show_404();
} else {
$data['facData'] = $this->Facultydata->getSpFacData($facultyNumber);
$this->load->view('faculty', $data);
}
}