Codeigniter:实现搜索功能

Posted

技术标签:

【中文标题】Codeigniter:实现搜索功能【英文标题】:Codeigniter: implementing search functionality 【发布时间】:2020-07-18 11:02:23 【问题描述】:

错误: 遇到 php 错误 严重性:通知

消息:未定义变量:记录

文件名:views/Search_data.php

行号:80

回溯:

文件:/home/paychzaq/public_html/application/views/Search_data.php 线路:80 函数:_error_handler

文件:/home/paychzaq/public_html/application/controllers/Search.php 线路:13 功能:查看

文件:/home/paychzaq/public_html/index.php 线路:315 函数:require_once

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends CI_Controller 
    function __construct() 
    parent::__construct();
     $this->load->model('Search_model'); 
     $this->load->database();
     $this->load->library('session');
 

function index()
$this->load->view('Search_data');


public function searchUser() 

        $key = $this->input->post('search');

        if(isset($key) and !empty($key))
            $data['records'] = $this->Search_model->searchRecord($key);
            $data['link'] = '';
            $data['message'] = 'Search Results';
            $this->load->view('Search_data' , $data);
        
        else 
            redirect('Search') ;
        
    

?>

型号:

<?php
class Search_model extends CI_Model 

    public function searchRecord($key)
    
        $this->db->select('*');
        $this->db->from('campaigns');
        $this->db->like('title',$key);
        $this->db->or_like('category',$key);
        $this->db->or_like('description',$key);
        $query = $this->db->get();

          if($query->num_rows()>0)
          return $query->result();
        
    

?>

查看:

<?php
  foreach($records as $row)
  
  $sr=$row->photo;
$ur=   site_url()."/campaign_info/campaign_detail/".$row->campaignid."/".$row->title."";

?>
           <div class="col-lg-3 col-sm-6">
            <div style="height: 40rem;" class="card card-product">
              <div class="card-header border-0">
                <h2 class="h6">
                  <a href="#"><?php echo $row->title?></a>
                </h2>
              </div>
              <!-- Product image -->
              <figure class="figure">
               <a href="<?php echo $ur; ?>"> <img  src='<?php echo $sr; ?>' style="max-width: 100%; height: auto; object-fit: contain;" class="img-center img-fluid"></a>
              </figure>
              <div class="card-body">
                <!-- Price -->
                <div class="d-flex align-items-center mt-4">
                  <span class="h6 mb-0"> <?php echo $row->price?> RS</span>
                  <span class="badge badge-success rounded-pill ml-auto"><a style="color: white"href="<?php echo $ur;?>">Details</a></span>

                </div>
              </div>
              <div class="p-4 border-top">
                <div class="row">
                  <div class="col-6 text-center">
                    <a href="<?php echo "#abc".$row->campaignid;?>" class="btn btn-sm btn-white btn-icon-only rounded-circle ml-4" data-toggle="modal">
                <span class="btn-inner--icon"><i class="fas fa-paper-plane"></i></span>
            </a>
                    <span class="d-block text-sm">Send Proposal</span>
                  </div>
                  <div class="col-6 text-center">
                    <span class="h5 mb-0"><?php echo $row->deadline ?></span><sup></sup>
                    <span class="d-block text-sm">Deadline</span>
                  </div>
                </div>
              </div>

<?php
   $url='proposal/validation/'.$row->campaignid;
  ?>
            <div class="modal fade" id="<?php echo "abc".$row->campaignid;?>" tabindex="-1" role="dialog" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
              <div class="modal-body"> 
              <?php echo form_open($url)?>
               <div class="form-group">
                  <label class="form-control-label">
                    Your Fees
                  </label>
                  <input name="fee" type="text" class="form-control">
                </div>
                <div class="form-group">
                  <label class="form-control-label">
                    Days To Complete
                  </label>
                  <input  name="days" type="text" class="form-control">
                </div>
                <!-- Project description -->
                <div class="form-group">
                  <label class="form-control-label mb-0">
                    Details 
                  </label>
                  <small class="form-text text-muted mb-2 mt-0">
                    This textarea will autosize while you type
                  </small>
                  <textarea name="details"class="form-control" data-toggle="autosize" rows="2"></textarea>
                </div>
                <button type="submit" class="btn btn-sm btn-primary rounded-pill mr-auto">Send</button>
               <?php echo form_close()?>
              </div>

            </div>
          </div>
        </div>
              <div class="card-footer">
                <div class="actions d-flex justify-content-between">
                  <a href="#" class="action-item">
                    <i class="fas fa-star"></i>
                  </a>
                  <a href="#" class="action-item">
                    <i class="fas fa-chart-pie"></i>
                  </a>
                  <a href="#" class="action-item text-danger">
                    <i class="fas fa-trash-alt"></i>
                  </a>
                </div>
              </div>
            </div>
</div>


    <?php
    
       ?>

【问题讨论】:

foreach($records as $row) $records 是在哪里定义的?它不在您的代码中。 请多描述一下您的问题 记录在控制器中定义:$data['records'] = $this->Search_model->searchRecord($key); 【参考方案1】:

我认为它是未定义的,因为在您的 index 方法中您尚未声明 $records 变量。在您看来,如果$records 可用,请尝试过滤:

<?php
if (!empty($records)) 
  foreach($records as $row)
  
  ...

【讨论】:

【参考方案2】:

在模型中

if($query->num_rows()>0)
   return $query->result();
else
   return array();

【讨论】:

以上是关于Codeigniter:实现搜索功能的主要内容,如果未能解决你的问题,请参考以下文章

多个类似子句 Codeigniter

CodeIgniter框架多条件搜索查询分页功能解决方案

CodeIgniter "like()" 函数在搜索词中带有 % 通配符

php codeigniter 邮编搜索!

在 CodeIgniter 中使用搜索过滤器进行分页

如何使用带有codeigniter的数据库中的JQuery使用自动完成功能?