如何从view-controller-model传递值并获取结果codeigniter

时间:2014-07-24 05:45:59

标签: php codeigniter

MY CONTROLLER PAGE

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Searchresult extends CI_Controller {
public function __construct(){
 parent::__construct();
 $this->load->model('searchresultss','','TRUE');
    }
 public function users()
 {
     $this->load->helper('url');
$this->load->view('includes/kheader');
$data['showdata']   =   $this->searchresultss->login();
$this->load->view('searchresult',$data);

$this->load->view('includes/khelp');
$this->load->view('includes/kfooter');
 }
}

MY MODEL PAGE

<?php
Class Searchresultss extends CI_Model
{

 function login($per_page=3)
 {


$query="SELECT *
FROM users
WHERE  

if('$se_ct'!='',sect =  '$se_ct' AND if('$subsect' !='',subsect =  '$subsect',subsect like  '%%'),sect like  '%%' AND subsect like  '%%')
AND
IF( '$cou_ntry' !='', country =  '$cou_ntry'
AND 
if('$sta_te' !='', state =  '$sta_te'
AND  
if('$ci_ty' !='',city =  '$ci_ty',city like  '%%'),state LIKE  '%%'
AND city LIKE  '%%'), country LIKE  '%%'
AND state LIKE  '%%'
AND city LIKE  '%%' ) 
AND age >=  '$age_from'
AND age <=  '$age_to'
AND 
IF('$qualification' !='',qualification =  '$qualification',  qualification LIKE  '%%' ) 
AND gender =  '$look'
And status='1'";
$data=array();
$query=$this->db->query($query);
$data['results']=$query->result();
$data['count']=$query->num_rows();
$data['pages']=ceil($count/$per_page);

return $data;
 }
}

这是我的HTML页面

<form action="<?php echo base_url();?>searchresult/users" method="post">
        <div id="tab1" class="tab_content" >

        <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Looking for</td>
    <td>
    <input type="radio" name="look" id="look" value="female" checked="checked"/> <span style="color:#000000;">Bride</span> <input type="radio" name="look" id="look" value="male" /><span style="color:#000000;"> Groom</span></td>
  </tr>
  <tr>
    <td>Age</td>
    <td> <span style="color:#000000;">From </span>
         <select name="age_from" id="age" class="inp_age"   >
    <option>18</option>
    <option>19</option>
<option selected>20</option>
<option>21</option>
<option>22</option>
 </select><span style="color:#000000;">&nbsp; To</span>
                     <select name="age_to" id="age" class="inp_age"   >
       <option>18</option>
    <option>19</option>
<option >20</option>

<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>

<option selected="selected">70</option>
          </select>
         </td>
  </tr>
  <tr>
    <td>Sect</td>
    <td><select name="sect" id="sect" onchange="return SelectSect(this.value);" class="inp_search" >
  <!-- <select name="cont" id="cont" onchange="return SelectState(this.value);" class="inp">-->
          <option value="">Any sect</option>
            <?php
         $sect=mysql_query("select sect_id,sect_name from `geo_sect` order by sect_name asc");
         while($sects=mysql_fetch_array($sect))
         {
         ?>
   <option value="<?php echo $sects['sect_id'];?>"><?php echo $sects['sect_name'];?></option>
         <?php
         }
         ?>

           </select></td>
  </tr>
 <tr>
    <td>Sub Sect</td>
    <td><select name="subsect" id="subsect"  onchange="return SelectSub(this.value);" class="inp_search" >
<!--<select name="state" id="state"  onchange="return SelectCity(this.value);" class="inp" >-->
         <option value="">Any subsect</option>

           </select></td>
  </tr>
  <tr>
    <td>Country</td>
    <td> <select name="country" id="country" onchange="return SelectState(this.value);" class="inp_search">
         <option value="">Any Country</option>
         <?php
         $countries=mysql_query("select country_id,Country_name from `kk.geo_countries` order by Country_name asc");
         while($country=mysql_fetch_array($countries))
         {
         ?>
          <option value="<?php echo $country['country_id'];?>" style="width:10px"><?php echo $country['Country_name'];?></option>
          <?php
          }
          ?>
           </select></td>
  </tr>
  <tr>
    <td>State</td>
    <td><select name="state" id="state"  onchange="return SelectCity(this.value);" class="inp_search" >
         <option value="">Any state</option>

           </select></td>
  </tr>
  <tr>
    <td>City</td>
    <td> <select name="city" id="city" class="inp_search" >
         <option value="">Any city</option>

          </select></td>
  </tr>
  <tr>
    <td>Qualification</td>
    <td><select name="qualification" id="qualification"  class="inp_search" >
     <option value="">Any  qualification</option>
        <?php
         $qualification=mysql_query("select qualification_id,qualification from `qualification` order by qualification asc");
         while($qualifications=mysql_fetch_array($qualification))
         {
         ?>
          <option value="<?php echo $qualifications['qualification_id'];?>"><?php echo $qualifications['qualification'];?></option>
          <?php
          }
          ?>
 </select></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>  <div style="padding-left:100px;"> <input type="submit" name="submit" class="button green search_btn" value="Search" /></div></td>
  </tr>
</table>
</div>
 </form>

我使用时

    echo "<pre>";
    print_r($showdata);

在我的浏览页面中出现错误

 A PHP Error was encountered
 Severity: Notice
 Message: Undefined variable: se_ct ........

这是将值从视图传递到控制器到模型的正确方法吗?我不确定我的控制器是否将值传递给模型,我也不确定模型页面。

2 个答案:

答案 0 :(得分:1)

我正在根据你的问题谈论错误,所以以下是我的意见。

我认为下面的问题

$data['results']=$query->result();

它应该像

$data['results']=$query->result_array();

然后执行以下操作

echo "<pre>";
print_r($showdata);

它不会显示任何错误。过程不正确。你应该把控制器中的所有值都像@Aeolun所说,然后在函数中传递它。

并且您的模型函数应该看起来像

model function login($look,$age,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)

从函数中删除$ per_page = 3,因为你将它定义为3.所以不需要将它作为参数传递。

如果您需要什么,请告诉我。

/ *更新* /

控制器功能

$look = $this->input->post('gender');
$age_from = $this->input->post('age_from');
$age_to = $this->input->post('age_to');
$age = $this->input->post('age');
$se_ct = $this->input->post('sect');
$subsect = $this->input->post('subsect');
$coun_try = $this->input->post('country');
$sta_te = $this->input->post('state');
$ci_ty = $this->input->post('city');
$qualification = $this->input->post('qualification');

$data['showdata']   =   $this->searchresultss->login($per_page=3,$look,$age, $age_to,$age_from,$se_ct,$subsect,$cou‌​n_try, $sta_te, $ci_ty,$qualification);

和模型功能

model function login($per_page,$look,$age,$age_to,$age_from,$se_ct,$subsect,$coun_try, $sta_te, $ci_ty,$qualification)

,您的查询应该是

$query="SELECT *
FROM users
WHERE  

if('$se_ct'!='',sect =  '$se_ct' AND if('$subsect' !='',subsect =  '$subsect',subsect like  '%%'),sect like  '%%' AND subsect like  '%%')
AND
IF( '$coun_try' !='', country =  '$coun_try'
AND 
if('$sta_te' !='', state =  '$sta_te'
AND  
if('$ci_ty' !='',city =  '$ci_ty',city like  '%%'),state LIKE  '%%'
AND city LIKE  '%%'), country LIKE  '%%'
AND state LIKE  '%%'
AND city LIKE  '%%' ) 
AND age >=  '$age_from'
AND age <=  '$age_to'
AND 
IF('$qualification' !='',qualification =  '$qualification',  qualification LIKE  '%%' ) 
AND gender =  '$look'
And status='1'";
$data=array();
$query=$this->db->query($query);
$data['results']=$query->result_array();
$data['count']=$query->num_rows();
$data['pages']=ceil($data['count']/3); // if $per_page is undefined the make it 3;

答案 1 :(得分:0)

在我看到的情况下,你在控制器,视图和模型之间传递值的方法是正确的,除了你缺少所有输入参数,除了&quot; per_page&#39;,你应该全部添加到模型中功能

//IN MODEL
function login($sect, $country, $state, $city, $per_page=3)

然后,当您将这些参数添加到模型函数中时,您可以使用以下命令从请求中获取它们:

//IN CONTROLLER
$sect = $this->input->post('sect');
$country = $this->input->post('country');
$state = $this->input->post('state');
$city = $this->input->post('city');
$this->searchresultss->login($sect, $country, $state, $city);

您的模型中的查询中也存在潜在的SQL注入漏洞,请使用Codeigniter中内置的参数工具。例如:

//IN MODEL
$query="select * from users where country = ? AND sect = ? AND state = ? AND city = ? limit ?";
$query=$this->db->query($query, [ $country, $sect, $state, $city, $per_page ]);

此外,您在视图中直接使用其他SQL查询,您可能希望将其移至已有的模型或其他模型。

请注意:我没有使用您表单中的所有字段,因为我认为这会使示例不清楚,但您可以使用相同的方法添加其余字段。