Welcome Guest, Not a member yet? Register   Sign In
Routes for URLs
#1

[eluser]the_unforgiven[/eluser]
Hi all,

I have a site and its a small e-commerce site and i need to know how to setup friendly urls.

For example i have:

home/categories/20 for instance and want it to display home/categories/t-shirts or whatever is written in the url box in the admin system i made, how can achieve this via htaccess? or routes?

Help greatly appreciated.
#2

[eluser]PhilTem[/eluser]
http://codeigniter.com/nightly_user_guid...tion-calls

Which means you just need to basically translate t-shirt to 20 and then you can grab every item for that category

(I'm assuming that home is a controller of yours since this isn't quite obvious from your post)
#3

[eluser]the_unforgiven[/eluser]
Here's the code relating to what i was talking about apologies i forgot to post the code.

Code:
function categories() {
  $id = $this->uri->segment(3);
  $meta = $this->MPages->get_details($id);
  $data['customer'] = $this->user_model->customerDetails();
    
  $data['cust_acc'] = $this->session->userdata('acc_number');
  $data['cust_name'] = $this->session->userdata('name');
  $data['cust_id'] = $this->session->userdata('customer_id');
  
     $data['listings'] = $this->MCats->get_listings($this->uri->segment(3));
  $data['category'] = $this->MCats->get_current_cat($this->uri->segment(3));
  
  $data['title'] = '';

  $data['metadesc'] = '';
  $data['metakeywords'] = '';


  $data['content'] = 'grid';
  $this->load->vars($data);
  $this->load->view('template', $data);
  
}

The above basically translates to home/categories/ then a number and i want it to display in the url as home/categories/ then instead of the number i want the word that is typed in by the admin user in this case id 20 would t-shirts so i want t-shirts to display in the url, and for each category.
#4

[eluser]PhilTem[/eluser]
Code:
function categories($slug) {
  $id = $this->translate_slug_to_id($id);
  // go on with your code from line 3 on
}

and now you will just need a method, that will look into some table where you have a relation between category-id and its slug. That's a very simple task getting the ID for that slug, though.

PS: As you see, you do not need to use $this->uri->segment(3) since it will automatically be the first argument passed to your categories method.
#5

[eluser]the_unforgiven[/eluser]
So i would need a new method $this->translate_slug_to_id then what would i put in here?
#6

[eluser]PhilTem[/eluser]
You would put a db call in there where you get the category_id for the given category_slug.
#7

[eluser]the_unforgiven[/eluser]
In my model ive added:

Code:
function cat_slug($slug = FALSE)
{  
  $options = array('path' => $slug);
  if ($slug === FALSE)
  {
   $query = $this->db->get('categories');
   return $query->result_array();
  }
  $query = $this->db->get_where('categories', $options);
  return $query->row_array();
}

Then in my controller done this:

Code:
function categories($slug) {
  $id = $this->uri->segment(3);
  $id = $this->MCats->cat_slug($id);
  $meta = $this->MPages->get_details($id);
  $data['customer'] = $this->user_model->customerDetails();
  
    
  $data['cust_acc'] = $this->session->userdata('acc_number');
  $data['cust_name'] = $this->session->userdata('name');
  $data['cust_id'] = $this->session->userdata('customer_id');
  
     $data['listings'] = $this->MCats->get_listings($id);
  $data['category'] = $this->MCats->get_current_cat($id);
  
  $data['title'] = '';

  $data['metadesc'] = '';
  $data['metakeywords'] = '';


  $data['content'] = 'grid';
  $this->load->vars($data);
  $this->load->view('template', $data);
  
}

Is all this right, has its throwing an error on a different model page@?
#8

[eluser]the_unforgiven[/eluser]
Anyone?
#9

[eluser]PhilTem[/eluser]
You want you're code debugged by us?

Well, anyways, you want your method cat_slug to just return the category-ID, however, you are basically returning an array of arrays. You need to grab only one row from the DB and if you got one row in your query you will need to just return the ID not the whole array.



I beg your pardon, but I guess you're not really familiar with PHP coding at all and got your high number of posts by just asking people on how to write code (I was looking at your other posts and they are most of the times asking others to write your code).
#10

[eluser]the_unforgiven[/eluser]
No need to be snotty yes i do know PHP and I;m still learning CI all i ask that someone just shows me how to certain things, most of the code i have written i have written from scratch, if you dont want to help just dont post!

I guess your a jumped up college grad that knows it all arent you and doesnt want to help others, well thats fine just dont post and dont reply! Simples!




Theme © iAndrew 2016 - Forum software by © MyBB