【问题标题】:redirect url codeigniter when product id changes in URL当 URL 中的产品 id 更改时重定向 url codeigniter
【发布时间】:2017-05-30 14:57:52
【问题描述】:

我的网址类似于 base_url/Category/productname-254

这里 254 是该产品的 ID。

类别也通过在 route.php 中添加路由来实现,例如 $route['(:any)/(:any)'] = '控制器/索引/$1/$1';

如果我将产品 ID 从 254 更改为产品 ID 344,那么它应该重定向到 344 产品页面。

如下网址:

http://www.marketsandmarkets.com/Market-Reports/cardiac-monitoring-advanced-technologies-and-global-market-55.html

如果您将上面 URL 中的最后一个数字 55 更改为数字 58,那么它将重定向到下面的 URL http://www.marketsandmarkets.com/Market-Reports/nanomaterials-58.html

这是产品 ID 58 的产品页面

这是我获取 URL 的函数:

function get_report_url($reporttitle,$reportid){
        //$title1 = str_replace(array( '(', ')' ), '',$reporttitle);
        $title1 = str_replace(array( '(', ')' ), '',$reporttitle);
        $title1 = str_replace(array( ',','?' ), "-",$title1);
        $title1 = str_replace(array( "'", "&", "º","°","ordm;" ), "",$title1);

        $title=explode(" ",$title1);
        //$title=explode(" ",$row['reportTitle']);
        $fintitle="";
        foreach($title as $words){
            $fintitle=$fintitle.$words.'-';
            }
        $fintitle=$fintitle.$reportid;  

        $query1 = $this->db->query("SELECT category FROM ur1 WHERE id=".$reportid);
        $cat = $query1->row();
        $url = base_url()."".$cat->category."/".$fintitle;
        //$url = base_url()."index/report/".$cat->category."/".$fintitle;
        return $url;
    }

控制器去上面的url:

class Index extends CI_Controller {
function __construct(){
    parent::__construct();
}

function report($cat,$rep){
    $ttl = explode("-",$this->uri->segment(2));
    $reportid = end($ttl);
    //echo "reportid  ".$reportid;
    /*echo "category ".$cat;*/
    //exit;
    $this->load->model('show_model','',TRUE);
    $arr_result = $this->show_model->get_report_details($reportid);
    //echo "reportid  ".$reportid;
    /*print_r($arr_result);
    exit;*/
    $data['error'] = $arr_result[0];
    $data['result'] = $arr_result[1];
    $this->load->view('includes/header');
    $this->load->view('includes/breadcrumbs');
    $this->load->view('pages/reportpage',$data);
    $this->load->view('includes/footer');
}

}

路由.php

$route['(:any)/(:any)'] = 'index/report/$1/$1';

任何帮助表示赞赏。 提前致谢

【问题讨论】:

  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请参阅How to Ask 页面以获得澄清此问题的帮助。有一些代码会很有帮助。
  • 你遇到了什么错误?
  • 我没有收到任何错误,它只是没有重定向.. 停留在同一页面上
  • 没有看到视图、控制器、路由代码很难分辨。
  • 再次编辑问题...使用控制器和路由。

标签: php .htaccess codeigniter url-redirection


【解决方案1】:
//go to application->config->routes.php  

$route['404_override'] = 'Category'; //override error page with category controller 
class Category extends MY_Controller {        
 function index($product_id=''){   
   //get product data using product id and load data on view   
  }  
 }

【讨论】:

    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 2021-10-12
    • 2019-11-11
    • 2019-12-04
    • 1970-01-01
    • 2014-01-07
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多