在codeiginter中创建自定义网址
问题描述:
我想要在url中显示我的产品名称 现在我得到我的产品与id和我的网址显示那样。 www.mydomain.com/home/single/1在codeiginter中创建自定义网址
我想显示我的产品网址与产品名称喜欢。 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)
<p><?=$product['product_name'];?></p>
<p><?=$product['product_price'];?></p>
<p><?=$product['product_description'];?></p>
答
您可以使用路由。
在你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'];?>">`
通过
<a href="<?=base_url();?>home/single/<?=$one['product_name'];?>">`
答
字符串`重新人性化,将是在一个领域,右?
www.mydomain.com/home/single/New-Dell-Laptop
所以,如果你选择在“新的戴尔笔记本电脑”数据库
它将返回1分的记录,对不对?如果是这样,你可以在配置中的路由上配置这样的东西。
$route['single/{product}'] = 'productController/{product}';
然后,当你加载ProductController的/新戴尔笔记本电脑
你可以用$这个 - > URI->段(3)获得的价值,并在数据库中搜索,检索页面,展示给用户。
你应该使用这样的事情:
www.mydomain.com/home/single/34/New-Dell-Laptop
凡34将是ID的页面,你找到数据库会很容易,但你不应该麻烦找到,如果你搜索“新的戴尔笔记本电脑”
+0
这种方法不工作! –
此错误显示 一个PHP错误遇到 严重性:注意 消息:使用未定义的常量EXT的 - 假设‘EXT’ 文件名:配置/ routes.php文件 行号:14 回溯: 文件:C:\ XAMPP \ htdocs中\ onlinemarket \程序\ CONFIG \ routes.php文件 行:14 功能:_error_handler 文件:C:\ xampp \ htdocs \ onlinemarket \ index.php Line:292 函数:require_once –
错误消息消息已清除,只需用'require_once(BASEPATH。'database/DB.php');'替换第一行。你可以自己找到这个:)你也应该考虑@Raphael舒伯特的回答 – AdrienXL
产品图像和数据不显示。它通过ID –