Just started learning codigniter and as far I learned from tutorials and by reading documentation I got and idea a basic about how to develop websites using codigniter as far as we see by using MVC framework there are many advantages so my questions is let suppose if we start building up a listing website obviously it contains view category wise so if I add 10 categories from the database so it does not mean I have to create 10 .php files to be able to get the view like i create a page and in controller i call that view file like this

  public function index() {
        $this->load->view('listings');
  }

Or something like we develop a page within the same controller so the structure will be developed like this

  public function ads() {
        $this->load->view('ads');
  }

Here ads is ads.php and in the url it will show localhost/directoryname/listings/ads
so for category links i posted as <?php echo base_url()."listings/ads/".$query->cat_name; ?>
I am creating a list of ads so in order to to display ad details on a detail page would it be required to create pages for every ad details like I want my link to look like this localhost/directoryname/listings/ads/categoryname/add_name

or somethign like this which i noticed on many websites so for that can any one give me the concept is there a way to make this dynamic i now we can do it by get method but still in that method it will show like this localhost/directoryname/listings/ads?cat=categoryname&add_name=name

Recommended Answers

All 4 Replies

Just give arguments to the ads() controller, for example:

public function ads($cat, $add_name) {
    $data['cat'] = $cat;
    $data['add_name'] = $add_name;

    $this->load->view('ads', $data);
}

In this case the url would look like:

  http://localhost/directoryname/listings/ads/categoryname/name

Otherwise use $this->input->get():

  public function ads() {
    $data['cat'] = $this->input->get('cat');
    $data['add_name'] = $this->input->get('add_name');

    $this->load->view('ads', $data);
}

For more info look at:

So how will I make the link for the user in the views page
<?php echo base_url()."listings/ads/".$query->cat_name; ?> will it be as
<?php echo base_url()."listings/ads/".$query->cat_name."/".$query->sub_cat_name; ?>

If i understand correctly. You can read code Click Here . if you process like then sample results will be similar to this link Click Here web site where if you check in left side bar Glossary.....etc will be fetch from mysql as well as content , all links url slug will be made from database. In your case there is not requirement of dictionary. so you can modify accordingly. hope it helps you in some way. existing site is in asp.net , but we can make that or just take example to process in CI

I'm not sure I've understood your request, if you want to know how to append segments to a link and make it work with a specific logic, then you can use routes. For example, you have a controller application/controllers/ListingAds.php that looks like this:

<?php

class ListingAds extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model(['ads']);
    }

    public function index()
    {
        $data['listings'] = $this->ads->getAll();
        $this->load->view('listings/index', $data);
    }

    public function category($cat)
    {
        $data['listings'] = $this->ads->get($cat);
        $this->load->view('listings/category', $data);
    }

    public function subCategory($cat, $name)
    {
        $data['listings'] = $this->ads->getSub($cat, $name);
        $this->load->view('listings/sub', $data);
    }
}

In your application/config/routes.php file you write:

$route['listings/ads/(:any)/(:any)'] = 'listingAds/subCategory/$1/$2';
$route['listings/ads/(:any)'] = 'listingAds/category/$1';
$route['listings/ads'] = 'listingAds/index';

This allows you to append parameters to the original link and to send the request to a specific method of the controller, for example:

# load index()
http://localhost/listings/ads

# load category()
http://localhost/listings/ads/adobe

# load subCategory()
http://localhost/listings/ads/adobe/lightroom

The order of the routing rules is important:

Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

From: https://codeigniter.com/user_guide/general/routing.html

For the log, here's the model application/models/Ads.php used in the example:

<?php

class Ads extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get($cat)
    {
        return $cat;
    }

    public function getSub($cat, $name)
    {
        return "$cat $name";
    }

    public function getAll()
    {
        return 'All';
    }
}

Which is where you would get the database results for your controller. Hope I've understood your request.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.