2

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>

2 Answers 2

1

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"

Sign up to request clarification or add additional context in comments.

Comments

0

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'];?>">`

6 Comments

This Error show A PHP Error was encountered Severity: Notice Message: Use of undefined constant EXT - assumed 'EXT' Filename: config/routes.php Line Number: 14 Backtrace: File: C:\xampp\htdocs\onlinemarket\application\config\routes.php Line: 14 Function: _error_handler File: C:\xampp\htdocs\onlinemarket\index.php Line: 292 Function: require_once
The error message message is clear, just replace the first line by require_once( BASEPATH .'database/DB.php');. You could have found this by yourself :) Also you should consider @Raphael Schubert's answer
product image and data not show. which get from database through id
My Controllers: 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); } My Views: <?php foreach($profiles as $vendor) : ?> <a href="<?=base_url();?>home/profile/<?=$vendor['vendor_id'];?>"><img src="<?=base_url();?>uploads/<?=$vendor['ven_img'];?>"></a> <a href="<?=base_url();?>home/profile/<?=$vendor['vendor_id'];?>"><?=$vendor['ven_firstname'];?></a> <?php endforeach; ?>
show Data view File: <p><img src="<?=base_url();?>uploads/<?=$profile['ven_logo'];?>"></p> <p><img src="<?=base_url();?>uploads/<?=$profile['ven_img'];?>"></p> <p>First Name : <?=$profile['ven_firstname'];?></p> <p>Last Name : <?=$profile['ven_lastname'];?></p> <p>Username : <?=$profile['username'];?></p> <p>Email : <?=$profile['ven_email'];?></p>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.