我有一张表格:
<?php
$attr = array('id'=>'urlSubmit');
$urlInputAttr = array('name'=>'urlInput','value'=>'yourdomain.com','maxlength'=>'50','size'=>'25');
echo form_open('urlSubmission',$attr);
echo form_input($urlInputAttr);
#echo form_submit('urlInput', '');
echo form_close();
?>
一个名为 urlsubmission
的控制器 $this->load->model('domaincheckmodel');
$this->domaincheckmodel->verifyduplicates($this->input->post('urlInput'));
和模型中的函数(domaincheckmodel),它基本上检查重复记录并插入新域:
function verifyduplicates($tldEntered){
# $_POSTed value of urlInput
## Gather if the domain exists in db
$DupDomains = $this->db->get_where('ClientDomain', array('tld'=>$tldEntered));
if($DupDomains->num_rows() > 0 ){
$this->load->view('err/domainexists'); ##domain already used
}
# else, no domain present, insert.
else{
#array of insert values:
$insertNewDomain = array('tld'=>$this->input->post('urlInput',TRUE));
$this->db->insert('ClientDomain', $insertNewDomain);
$this->load->view('success/domainfree'); ##domain is free and has been entered.
}
}
答案 0 :(得分:1)
$this->domaincheckmodel->verifyduplicates($this->input->post('urlInput'));
function verifyduplicates($tldEntered){
# $_POSTed value of urlInput
## Gather if the domain exists in db
$DupDomains = $this->db->get_where('ClientDomain', array('tld'=>$tldEntered));
您从表单传递到控制器到模型,您确定post变量是否保持填充状态?试试上面的内容,在控制器中捕获post变量并将其传递给模型,而不是试图在模型中读取它?
关于将参数传递给函数的一点说明。您可以通过括号内的任何内容执行此操作,如下所示。
控制器:
$myVariable = $this->someModel->someFunction($someParameter)
型号:
function someFunction($variableIWantToPopulateWithSomeParameter)
因此someParameter从控制器传递到模型中的函数名称。有一件事需要注意,那就是模型函数现在EXPECTS一个参数,如果你不给它一个,即你调用someFunction()你会得到一个错误。这可以通过给它一个默认值来避免:
function someFunction($myVariable = 1)
要做的是说如果我没有传递给我的值,我将使$ myVariable等于1,如果我这样做,我将用新值覆盖1。因此,如果您向该函数发送两个调用,那么这就是您所期望的:
//$myVariable is going to be 1, the default.
$这 - &GT; someModel-&GT; someFunction();
//$myVariable is going to be 5, the value passed to it
$这 - &GT; someModel-&GT; someFunction(5);