php - 如何在没有 "like"参数的情况下在 codeigniter 中进行搜索?

标签 php html mysql database codeigniter

型号:

function search()
{
    $cari=$this->db->query("select * from uhd_user_order where user_order_reference  ");
    return $cari->result();;
}

Controller :

function search_keyword()
{
    $this->input->GET('cari', TRUE);
    $beda['cari'] = $this->order_test_m->search();
    $this->load->view('admin/a',$beda);
}

查看:

<?php if(count($cari)>0){
    foreach ($cari as $row => $test) {
    }?>

    <?= $test->sendername ?>

    <?php
} ?>

我需要进行搜索,如果我输入正确的代码, View 的结果就会显示出来。就像,如果我只搜索“s”,数据库中的数据将不会显示。

如何做到这一点?

最佳答案

我不确定你想要什么,我对你的代码有一些疑问,但我还不能发表评论。我必须猜测发生了什么:

我认为您的代码的工作原理是:每当单击“搜索”按钮时,数据库中的所有用户名都会显示,对吧?

因为你的模型没有使用“获取”数据在数据库中搜索

//Your function does not recieve the search string.
//This function uses $string to search in the 'users' table, to finds the name field == $string, and returns only 'name' row. 

function search($string){

    $query = $this->db->select('name')
        ->where('name',$string)
        ->get('users');

   //If the result is empty, return false, do nothing.
    if($query->num_rows() === 0){

        return false;
    }

    return $query->result();;
}

Controller

function search_keyword()
{
// Call search function with the input data.
// I think here will have some validation
//for example:
//if($this->form_validation->run() == false){
//       $this->load->view('errors/error');
//}
    $search_string = $this->input->GET('cari', TRUE); 
    $beda['cari'] = $this->order_test_m->search($search_string);
    $this->load->view('admin/a',$beda);
}

查看:

// This will ensure the ul will only display when $cari is not empty.
<?php if(count($cari)>0):?>
<ul>
<?php foreach($cari as $row):?>
   <li><?php echo $row->name;?></li>
<?php endforeach;?>
</ul>
<?php endif; ?>>

关于php - 如何在没有 "like"参数的情况下在 codeigniter 中进行搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37221838/

相关文章:

javascript - 菜单上的箭头处于事件状态时

javascript - 如何在javascript中验证两个复选框

javascript - 使用 Node.js 和 multer 上传压缩文件并将其保存到 MySql 数据库时出现问题

php - Delete NOT IN array 删除所有行,PHP Mysql

php - CodeIgniter Active Record 在查询中使用 "USING"

php - Laravel 递归关系

javascript - 从 jQuery 加载 symfony 2 目录中的文件

php - 警告 : require_once(includes/EliteScript. php) [function.require-once]: 无法打开流: 中没有这样的文件或目录

javascript - 防止内容在位置更改时跳跃到固定

mysql - 哪些已经存在的drop程序?