【问题标题】:codeigniter how to restrict number of url segments in a pagecodeigniter如何限制页面中的url段数
【发布时间】:2017-12-20 20:24:50
【问题描述】:

我有一个可以进行搜索的 codeigniter 函数,它工作正常,但是当我在单个页面上进行新搜索时出现问题,在单击搜索按钮时,单个页面的相同 url 在 url 栏上重复,因此采取我到一个错误的链接。在下面的 sn-ps 中查看它的行为;

http://localhost/newsapp/bulletins/view/31

http://localhost/newsapp/bulletins/view/view/31

http://localhost/newsapp/bulletins/view/view/view/31 http://localhost/newsapp/bulletins/view/view/view/view/31

这里是函数;

      public function livesearch() {

     $keyword = $this->input->post('keyword');
    $query = $this->news_model->get_live_items($keyword);

    foreach ($query as $row):
     echo "<li><a href='view/$row->id'>" . $row->title . "</a></li>";
    endforeach;
}

这个在另一个页面中显示搜索结果:

  public function search_keyword()
 {
    $keyword = $this->input->post('keyword');
    $data['results'] =$this->news_model->get_live_items($keyword);
    $data["top_news"] = $this->news_model->topnews();
    $data["latest_news"] = $this->news_model->latestnews();
    $this->load->view('result_view',$data);
}

终于到了所有魔法发生的地方;

 function view($id)
{       
    $data['news'] = $this->news_model->get_one_news($id);
    $data["top_news"] = $this->news_model->topnews();
    $data["latest_news"] = $this->news_model->latestnews();
   $data['content'] = 'single'; // template part
   $this->load->view('includes/template',$data);
}

【问题讨论】:

    标签: php codeigniter url


    【解决方案1】:

    你的 livesearch 方法应该是

        public function livesearch() {
            $keyword = $this->input->post('keyword');
            $query = $this->news_model->get_live_items($keyword);
    
            foreach ($query as $row):
                echo "<li><a href='" . base_url('bulletins/view/' . $row->id) . "'>" . $row->title . "</a></li>";
            endforeach;
        }
    

    【讨论】:

      【解决方案2】:

      尝试提供这样的完整链接

      echo "<li><a href='".base_url()."view/$row->id'>" . $row->title . "</a></li>";
      

      您只是包含了view/$row-&gt;id,因此它是在添加 url 而不是生成所需的 url。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-09
        • 1970-01-01
        相关资源
        最近更新 更多