【问题标题】:SEARCHING with some condition in codeigniter在 codeigniter 中搜索某些条件
【发布时间】:2015-09-26 10:02:33
【问题描述】:

我有一个使用 codeigniter 的网络项目。 我在我的项目中搜索时遇到问题。我必须用一些关键字显示搜索页面的多个结果。 这是我的模型

function find_user($like){
    $this->db->from('user');
    $this->db->like('name', $like,'after');
    $result =$this->db->get();
    return $result->result_array();
}

在我的用户表中,包括

id | name | place_code

在用户表中,place_code 列用于显示用户所在的位置

这是我的控制器

    function search(){

    $query       = $this->input->post('query_cari');

    $find = $this->m_user->find_user($query);
    foreach ($find as $key) {
        $code = $key['place_code'];
        if ($code == '1') {
        $place = 'Secretray';
        }elseif($code == '2'){
        $place = 'OB';
        }elseif($code == '3'){
        $place ='Manager';
        }
    }

    $data['result'] = $find;
    $data['place']  = $place;
    $this->load->view('home/search',$data);
}

这是我的控制器代码,包括logic 用于从办公室用户那里获取职位。但问题是,当我得到1 结果时,place 是正确的。但如果我得到超过 1 个结果,place 出错了,只显示place,因为所有结果都是搜索中最后一个结果中的place。 我想要的是,所有结果都显示了他们自己的place

这里是我的视图

 <?php foreach ($find as $key: ?>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Place</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><?php echo $key['id'] ?></td>
                <td><?php echo $key['name'] ?></td>
                <td><?php echo $place ?></td>
            </tr>
        </tbody>
    </table>
<?php endforeach ?>

【问题讨论】:

  • 不知道这是否能解决问题,但看起来你打错了:$this->db->like('name', $liek,'after');跨度>
  • 你的cari_keluarga()函数在哪里??
  • 抱歉是我的错,我正在更新我的问题

标签: php codeigniter search


【解决方案1】:

你总是排在最后是正常的,因为你的逻辑不正确。首先,您的模型中有错误。其次,你可以改进你的模型代码:

function find_user($like)
{
   $this->db->like('name', $like, 'after');

   return $this->db->get('user')->result();
}

现在,在您的控制器中,您想根据place_code 的值更改变量place。您可以实时向stdClass() 添加新密钥(或更改现有密钥),如下所示:

foreach($find as $i => $result)
{
   // By default, we assume the 'place_code' is equal to '1'
   $find[$i]->place = 'Secretray';

   if($result->place_code == '2')
       $find[$i]->place = 'OB';
   else
       $find[$i]->place = 'Manager';
}

$data['find'] = $find;
$this->load->view('home/search', $data);

最后,在你看来:

 <?php foreach ($find as $result){ ?>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Place</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><?php echo $result->id; ?></td>
                <td><?php echo $result->name; ?></td>
                <td><?php echo $result->place; ?></td>
            </tr>
        </tbody>
    </table>
<?php } ?>

【讨论】:

  • 哈哈,天哪,我忘记了codeigniter中的result,所以谢谢,我的问题解决了
猜你喜欢
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 2016-08-22
  • 2014-07-10
  • 2015-09-07
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
相关资源
最近更新 更多