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

时间:2018-02-24 17:22:43

标签: php

如何使用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 个答案:

答案 0 :(得分:0)

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

在模型函数中,只需返回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();

}