Reputation: 131
i want show my products name in url now i get my product with id and my url display like that. www.mydomain.com/home/single/1
i want to show my product url with product name Like. www.mydomain.com/home/single/New-Dell-Laptop
Here is my code that's get post with it's id number.
Controller
//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);
}
Model
//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');
}
Views
//show all products 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>
//show single product (product.php)
<p><?=$product['product_name'];?></p>
<p><?=$product['product_price'];?></p>
<p><?=$product['product_description'];?></p>
Upvotes: 2
Views: 77
Reputation: 359
The string you`re humanizing, will be in a field, right?
www.mydomain.com/home/single/New-Dell-Laptop
So, if you select in database for "New Dell Laptop"
It will return 1 record, right? If so, you can configure something like this on Routes in config.
$route['single/{product}'] = 'productController/{product}';
Then when you load productController/New-Dell-Laptop
You can use $this->uri->segment(3) to get the value, and search in Database, retrieve page, and show to user.
You should use something like this:
www.mydomain.com/home/single/34/New-Dell-Laptop
Where 34 will be the id to the page and will be easy to you locate on database, but you should not get troubles to find if you search for "New Dell Laptop"
Upvotes: 1
Reputation: 3008
You can use routes.
In your 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;
}
And then in your view, change
<a href="<?=base_url();?>home/single/<?=$one['prod_id'];?>">`
by
<a href="<?=base_url();?>home/single/<?=$one['product_name'];?>">`
Upvotes: 0