Php 在codeiginter中创建自定义url

Php 在codeiginter中创建自定义url,php,codeigniter,url-rewriting,custom-url,Php,Codeigniter,Url Rewriting,Custom Url,我想在url中显示我的产品名称 现在我得到了我的产品id和我的url显示像这样。 www.mydomain.com/home/single/1 我想用产品名称显示我的产品url,如。 www.mydomain.com/home/single/New-Dell-Laptop 这是我的密码,上面写着我的身份证号码 控制器 //get all products public function index() { $data['products'] = $this->fro

我想在url中显示我的产品名称 现在我得到了我的产品id和我的url显示像这样。 www.mydomain.com/home/single/1

我想用产品名称显示我的产品url,如。 www.mydomain.com/home/single/New-Dell-Laptop

这是我的密码,上面写着我的身份证号码

控制器

//get all products
public function index()
    {
        $data['products'] = $this->front->get_products();
        $this->load->view('index',$data);
    }

    //get single product
public function single($id)
    {
        $data['product']=$this->front->get_product($id);
        $this->load->view('product',$data);
    }
型号

//get all products
    public function get_products()
    {
        $this->db->select()
                 ->from('vendor_products')
                 ->where('DESC','date_added');
        $data = $this->db->get();
        return $data->result_array();
    }

        //get single product
    public function get_product($id)
    {
        $this->db->select()
                 ->from('vendor_products')
                 ->where('prod_id',$id);
        $data = $this->db->get();
        return $data->first_row('array');
    }
视图

//显示所有产品index.php

<?php foreach($products as $one) : ?>
            //create link for single product
        <a href="<?=base_url();?>home/single/<?=$one['prod_id'];?>">

            <?=$one['product_name'];?>
        </a>
        <p><?=$one['product_price'];?></p>

//为单个产品创建链接

//显示单个产品(product.php)

您可以使用路由

config/routes.php中

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get('vendor_products');
$result = $query->result();
foreach( $result as $row )
{
    $route["home/single/" . $row->product_name] = "home/single/" . $row->prod_id;
} 
然后在你看来,改变

<a href="<?=base_url();?>home/single/<?=$one['prod_id'];?>">`

您正在人性化的字符串将在字段中,对吗

www.mydomain.com/home/single/New-Dell-Laptop
所以,如果您在数据库中选择“新戴尔笔记本电脑”

它将返回1条记录,对吗?如果是这样,您可以在配置中的路由上配置类似的内容

$route['single/{product}'] = 'productController/{product}';
然后在加载productController/新的Dell笔记本电脑时

您可以使用$this->uri->segment(3)来获取值,然后在数据库中搜索、检索页面并显示给用户

您应该使用如下内容:

www.mydomain.com/home/single/34/New-Dell-Laptop

其中34将是页面的id,很容易在数据库中找到,但如果搜索“New Dell Laptop”,应该不会有麻烦

此错误显示遇到PHP错误严重性:注意消息:使用未定义的常量EXT-假定的“EXT”文件名:config/routes.PHP行号:14回溯:文件:C:\xampp\htdocs\onlinemarket\application\config\routes.PHP行:14函数:\错误\处理程序文件:C:\xampp\htdocs\onlinemarket\index.PHP行:292函数:require_once错误消息很清楚,只需将第一行替换为
require_once(BASEPATH.database/DB.php')。你可以自己找到这一点:)你也应该考虑@ Raphael Schubert的问答产品图像和数据不显示。通过idMy控制器从数据库获取:
public function vendor_profiles(){$data['profiles']=$this->front->vendor_profiles();$this->load->view('vendors',$data);}public function profile($id){$data['profile']=$this->front->vendor_profile($id);$this->load->view('profile',$data);}
我的视图:
显示数据视图文件:
上传/“>上传/“>名字:

姓氏:

用户名:

电子邮件:


$route['single/{product}'] = 'productController/{product}';
www.mydomain.com/home/single/34/New-Dell-Laptop