CodeIgniter Forums
Help with linking categories - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Help with linking categories (/showthread.php?tid=51370)

Pages: 1 2


Help with linking categories - El Forum - 05-01-2012

[eluser]the_unforgiven[/eluser]
Hi all,

I'm building a site all is good so far but just com across a little problem....

I have some code grabbing the categories from the database which work just fine, but now I need the code to not only make a page dynamically but also use the path the user types in.

Here's a picture of the db table/row
http://www.imageupload.co.uk/files/3qd3imh61lhigea2s76c.png

And here's the code:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

// Construct Method
function __construct()
{
      parent::__construct();
   $data['cat'] = $this->MCats->getAllCategories(); //This is  the line getting the categroies

   $data['mainf'] = $this->products_model->getMainFeature();
   $skip = $data['mainf']['id'];
   $data['sidef'] = $this->products_model->getRandomProducts(1,$skip);
  
   $this->load->vars($data);
}
// Index
function index()
{
  $data['metadesc'] = '';
  $data['metakeywords'] = '';

  $data['content'] = 'home';
  $data['title'] = "Welcome";

  $this->load->view('template', $data);
}
// Pages
function pages($path){
  
     $page = $this->MPages->getPagePath($path);
  
  $data['title'] = $page['name'];

  $data['metadesc'] = $page['description'];
  $data['metakeywords'] = $page['keywords'];

  $data['page'] = $page;

  $data['content'] = 'page';
  $data['title'] = ucfirst($page['name']);

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


}

Model
Code:
function getAllCategories(){
     $data = array();
     $this->db->order_by('name','asc');
     $Q = $this->db->get('categories');
     if ($Q->num_rows() > 0){
       foreach ($Q->result_array() as $row){
         $data[] = $row;
       }
    }
    $Q->free_result();  
    return $data;
}

So based on this i have this then in my header:

Code:
<ul class="cats">
    <li class="cathead"><a href="">categories</a></li>
    &lt;?php foreach ($cat as $row) : ?&gt;
    <li class="subcats"><a href="&lt;?php echo base_url().strtolower($row['path']); ?&gt;">&lt;?php echo ucfirst($row['name']); ?&gt;</a></li>
    &lt;?php endforeach; ?&gt;
   </ul>

which shows the path from the URL but need it to make a dynamic page now, so i would one achieve this based on my code


Help with linking categories - El Forum - 05-01-2012

[eluser]Stefan Hueg[/eluser]
Okay after reading your post three times I may have got what you want.
You want paths like www.myurl.com/hair-slides and have it routed to your controller?

Then take a look at routes.php in your application/config folder and add the following route:
Code:
$route['(:any)'] = 'home/pages/$1';

But I would not recommend this because you will never be able to access any other controller functions UNTIL you define them specifically in your routes.php.

As a side note:
If you set data to an array (like $data['test'] = 'val') make sure to define your array first ($data = array()), else you will get nasty errors someday.


Help with linking categories - El Forum - 05-01-2012

[eluser]GrahamDj28[/eluser]
Hi,

with dynamic page, do you mean a sub page of a category showing the items belonging to that category?

if so then for e.g.
yourdomain.com/controller/function/parameters

Would become
yourdomain.com/catalog/category/title:category-1/id:1

Catalog is your controller.
Category is the function being called
The rest are parameters you can use in the called function.

This way you can have 1 page displaying different content.

If you mean Dynamic DB pages, well that will take some extending of the CI Core.

Hope this helps you some

[edit]
I see you mean dynamic db pages. There is a forum post showing how you can set a fallback controller when calling dynamic pages.
The way it works is that the router will check to see if a controller exists for the first Uri segment. If no controller is found, it will fallback to a default controller. In this default controller you can then query the db to find the page being requested. This is the way I have my CI Code set up, and the best thing is that you can add physical pages (physical controllers) without breaking your application


Help with linking categories - El Forum - 05-01-2012

[eluser]Stefan Hueg[/eluser]
Kudos Graham, good point Smile Never thought of that.


Help with linking categories - El Forum - 05-01-2012

[eluser]GrahamDj28[/eluser]
I'm working on a CMS that is based on this approach. When it is up and running with little to no bugs i will post a link to a demo and explain how it works underwater.


Help with linking categories - El Forum - 05-01-2012

[eluser]the_unforgiven[/eluser]
Yes i want paths to looks like: www.myurl.com/hair-slides but instead of me creating each page manually, this has to be dynamically has the client will be adding at least 30 categories, so i dont want to create each of those pages.

It needs to create them dynamcally and product a templated page if you get me.


Help with linking categories - El Forum - 05-01-2012

[eluser]the_unforgiven[/eluser]
[quote author="GrahamDj28" date="1335903352"]

[edit]
I see you mean dynamic db pages. There is a forum post showing how you can set a fallback controller when calling dynamic pages.
The way it works is that the router will check to see if a controller exists for the first Uri segment. If no controller is found, it will fallback to a default controller. In this default controller you can then query the db to find the page being requested. This is the way I have my CI Code set up, and the best thing is that you can add physical pages (physical controllers) without breaking your application[/quote]


Where is the forum post....?

As you can see from my last post the cats will be input by the client therefore the related templated pages need to be added dynamically....as its a full custom CMS


Help with linking categories - El Forum - 05-01-2012

[eluser]GrahamDj28[/eluser]
Here it is.
http://ellislab.com/forums/viewthread/140428/

There are some typos in the thread so watch out for that.

But I do have a question.
Is the end user able to create every page of the site dynamicly, or only categories and the items belonging to a category?


Help with linking categories - El Forum - 05-01-2012

[eluser]the_unforgiven[/eluser]
Well when they create a new categorie i need it to create a page with every product in that categorie to show


Help with linking categories - El Forum - 05-01-2012

[eluser]the_unforgiven[/eluser]
What a bummber can't download the Router file mentioned in your post Graham