0
每当数据库中有一个标题和平台与提交的相同的游戏时,我想检查(在提交给db之前)。CodeIgniter并提交回调
我有这样的控制器:
public function addGame($id = null) { // ID is for children games
$this->load->library(array('images','form_validation'));
$this->load->helper('form');
$this->load->model('contrib_model');
$this->form_validation->set_rules('gameTitle', 'Tytuł gry', 'required|callback_checkGamePlatform');
if($this->form_validation->run() == FALSE) {
$data['title'] = 'Dodaj grę';
$data['genres'] = $this->contrib_model->getGenres();
$data['platforms'] = $this->contrib_model->getPlatforms();
$data['developers'] = $this->contrib_model->getDevelopers();
$this->template->load('template','theme/contribute/addGame',$data);
} else {
$data['submit'] = $this->contrib_model->insertGame();; //submits data
$this->load->view('theme/contribute/emptyPage', $data); //loads view
}
}
public function checkGamePlatform() {
$platform = $this->input->post('gamePlatform');
$game = $this->input->post('gameTitle');
$q = $this->contrib_model->checkGame($game,$platform);
echo $q;
if($q === '0') {
$this->form_validation->set_message('checkGamePlatform','gra istnieje');
}
}
而这一模式:
function checkGame($name,$platform) {
$q = $this->db->get_where('games',array(
'name' => $name,
'plat' => $platform
));
return $q -> num_rows();
}
的问题是,这是行不通的。我做错了什么?
伟大的作品!你也可以告诉我,我可以,而不是显示消息,加载视图?我试过用'$ this-> load-> view('error.php');'而不是'set_message',但它没有奏效。 – 2012-08-06 13:35:07
很高兴帮助。我不知道它是否可以加载视图,但似乎不太可能。 – 2012-08-06 13:55:33