代码之家  ›  专栏  ›  技术社区  ›  Alberto Delrio

如何使用explode搜索多个关键字并显示结果

php
  •  -1
  • Alberto Delrio  · 技术社区  · 6 年前

    如何使用explode搜索多个关键字我尝试了explode功能,但似乎无法实现。我也尝试显示它,我认为我没有正确使用explode函数。有人能告诉我我的问题吗。还更新了视图。我还没有更新这里的新代码,并告诉我必须做什么。

    这是我的代码:

    Controller : 
    public function search_title(){
    
        $title['mypage']="Learning Resource Platform";
        $this->load->view('template/header1',$title);
        $this->load->model("Learning_model");
        $title = $this->input->post('search');
        $piecesoftitle = explode(" ", $title);
    
        foreach($piecesoftitle as $part) {
            if(!empty(trim($part))){
    
        $results[] = $this->Learning_model->search_title($part);
    }
    

    }

        $data['result']= $results;
    
        if(isset($part) and !empty($part)){
    
            $data['actor'] = $this->Learning_model->search_title($part);
            $data['links'] = '';
    
            } else {
    
            redirect($this->browse());
            }
            $this->load->view('Learning/browse',$data);
            $this->load->view('template/footer');
    
        }
    

    模型

    public function search_title($part){
    
    $this->db->join('courses_tbl', 'courses_tbl.course_id = projects_tbl.course_id');
    $this->db->join('advisers_tbl', 'advisers_tbl.adv_id = projects_tbl.adv_id');
    $this->db->select("*");
    $this->db->from("projects_tbl");
    $this->db->like("title", $part);
    $query = $this->db->get();
    
    return $query->result();
    

    }

            }
    }
    

    }

    查看:

    <h1> Search </h1>
    
      <div class="search-container">
      <div class="row upse">
        <div class="col-sm-2"></div>
        <div class = "col-sm-6 form-group">
          <form class = "form-inline  sss" role = "form" action="<?php echo base_url(); ?>Learning/search_title" method= "post">
                <input type = "text" class="form-control search_box" name = "search" placeholder = "Search here...">
            <button type ="submit" class = "btn btn-info" name = "submit">Search </button>
          </form>
          </div>
      </div>
      </div>
    
    
    <table style = "width: 978px;" class = "table table-striped">
      <thead style ="background-color: #2aabd2;" >
        <tr>
          <th>Title</th>
          <th>Course</th>
          <th>Date Uploaded</th>
          <th>Adviser</th>
        </tr>
      </thead>
    
    <?php
    
      foreach ($actor as $project ) {
       // echo '<tr><td><a href="'.   // base_url('show_project?id='.   // $projects->proj_id. // '">'. // $projects['title']     // '</td><td>'.   // $projects['abbrev'].     // '</td><td>'.     // date('F d, Y (h:i A)', strtotime($projects['date_uploaded'])).       // '</td><td>'.       // $projects['professor'].       // '</td></tr>'       // echo '<tr><td><a href="'. base_url('show_project?id='. $projects->proj_id). '">'.   // $projects->title. '</td><td>'.       // $projects->abbrev. '</td><td>'.       // date('F d, Y (h:i A)', strtotime($projects->date_uploaded)).'</td><td>'.       // $projects->professor. '</td></tr>' ;
       echo generateTableRow($project);
     }
    
     function generateTableRow($project)
    
      {
      $projectUrl = base_url("show_project?id=$project->proj_id");
      $date = date('F d, Y (h:i A)', strtotime($project->date_uploaded));
      $abbrev = ($project->abbrev);
      $professor = ($project->professor);
      $tableRow =  '<tr>';
      $tableRow .= "<td><a href=\"$projectUrl\">$project->title</td>";
      $tableRow .= "<td>$abbrev</td>";
      $tableRow .= "<td>$date</td>";
      $tableRow .= "<td>$professor</td>";
      $tableRow .= '</tr>';
    
      return $tableRow;
     }
    ?>
    

     <?php echo $links ?>
    

    1 回复  |  直到 6 年前
        1
  •  0
  •   Istiaque Ahmed    6 年前

    对于搜索查询的每个部分,调用model方法,然后将它们存储在一个数组中,即 $results . 在中传递此变量 $data 变量从该视图中,您将能够访问完整的数据库结果。相应地在此处显示结果。

    在model函数中,只需返回 return $query->result(); 无论可能有多少个结果(0或更多)。从视图中,可以检查结果中有多少行可用 .

    控制器:

    public function search_title(){
    
        $title['mypage']="Learning Resource Platform";
        $this->load->view('template/header1',$title);
        $this->load->model("Learning_model");
        $title = $this->input->post('search');
        $piecesoftitle = explode(",", $title);
        $results=array();
        foreach($piecesoftitle as $part) {
    
            if(!empty(trim($part))){
    
               $results[] = $this->Learning_model->search_title($part);
            }
    
        }// end of foreach
    
        $data['result']=$results;
        //other data variables if neccesary go here 
    
        $this->load->view('Learning/browse',$data); // load the view with data
    
    }
    

    型号:

    public function search_title($part){
    
        $this->db->join('courses_tbl', 'courses_tbl.course_id = projects_tbl.course_id');
        $this->db->join('advisers_tbl', 'advisers_tbl.adv_id = projects_tbl.adv_id');
        $this->db->select("*");
        $this->db->from("projects_tbl");
        $this->db->like("title", $part);
        $query = $this->db->get();
    
        return $query->result();
    
    }