【问题标题】:Codeigniter Passing Search Data in a Template ModalCodeigniter 在模板模式中传递搜索数据
【发布时间】:2013-11-03 11:31:04
【问题描述】:

我正在尝试在我的网站上实现搜索功能。我面临无法通过模板传递数据的错误。即使我尝试使用网站包含的关键字进行搜索,我也会在搜索查询中得到一个空白页。我的默认控制器是页面。

主布局视图:

<?php $this->load->view('templates/' .$subview); ?>

页面控制器。我认为我无法从 _search 传递数据。

public function index() {
    // Fetch the page template
    $this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1 , 'homepage')), TRUE); 
    count($this->data['page']) OR show_404(current_url());

    // Fetch the page data
    $method = '_' . $this->data['page']->template;
    if (method_exists($this, $method)) {
        $this->$method();
    }
    else {
        log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
        show_error('Fail : ' . $method);
    }
    // Load the view

    $this->data['subview'] = $this->data['page']->template;
    $this->load->view('_main_layout', $this->data);
}
private function _page(){ // standart page template, just showing for how i do
    $this->load->model('page_m');
    $this->data['pages'] = $this->page_m->get();
}

private function _search(){
    $this->load->model('search_m'); // problem is here i think
}

搜索控制器,关键字功能:

 function keyword()
{
    $keyword = $this->input->post('keyword');
    $this->data['results'] = $this->search_m->search($keyword);
    $this->data['subview'] = 'search';
    $this->load->view('_main_layout',$this->data);
}

搜索模型:

 function search($keyword)
 {
    $this->db->like('body',$keyword);
    return parent::get();
 }

搜索视图:

  <?php if (count($results)): foreach ($results as $result): ?>
    <?php echo $result->title; ?>
    <?php echo $result->body; ?>
  <?php endforeach; endif; ?>

这给出了未定义的变量错误。如果编辑该搜索视图代码:

<?php if(isset($results)): ?>
<?php if (count($results)): foreach ($results as $result): ?>
<?php echo $result->title; ?>
<?php echo $result->body; ?>
<?php endforeach; endif; endif; ?>

此搜索查询后我得到一个空白页,这意味着我认为我无法获得结果数据。 我在这里缺少什么?

【问题讨论】:

    标签: php codeigniter search


    【解决方案1】:

    我看到你加载了模型,但没有调用函数。我在看过去吗?

    您的搜索控制器看起来不像是从默认控制器继承的,是吗?您正在删除我们需要查看的所有代码

    【讨论】:

    • 我继承了默认控制器和默认模型等等。我只是没有在这里写它们,因为该站点的其他功能正在运行。我以为我在搜索控制器中传递数据,但我认为没有工作。
    • 公开你的函数
    • 没问题。对于其他人来说,php.net/manual/en/language.oop5.visibility.php“声明为 public 的类成员可以在任何地方访问。声明为 protected 的成员只能在类本身内部以及继承的类和父类中访问。声明为 private 的成员只能由定义的类访问成员。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2015-06-19
    • 2021-11-24
    相关资源
    最近更新 更多