Welcome Guest, Not a member yet? Register   Sign In
URI control
#1

[eluser]Perkin5[/eluser]
I am building a shopping site and there is a facility to choose a category with a dropdown list. Choosing a category (call it 'greetings') links you to a controller method called 'category' which loads a page like this:
Code:
$data = array(
    'body_id'=>'cards',
    'title'=>'HistoryStore - cards',
    'content'=>'cards_view',
    'category'=>$catsearch,
    'catrows'=>$catrows,
    'records'=>$this->Cards_model->cat_search($catsearch),
    'price'=>$this->Cards_model->get_price()
    );
    $this->load->view('template_view', $data);

The resulting URI is then www.site/shop/category

I want it to be www.site/shop/category/greetings

So I need to create a segment3 of the URI. How could I achieve that?
#2

[eluser]Samus[/eluser]
This should give you an idea..

Code:
class Shop extends CI_Controller {
function category($category='') {
echo $category;
}
}

In this situation you now have an url: site.com/shop/category/something

If you visit this url, it will echo 'something'.

or if you visit site.com/shop/category/greetings it will echo 'greetings'. The example's a bit vague, but I hope that helps you understand how URLs are created with CI.

Simply the value of your method is the third URI
#3

[eluser]Perkin5[/eluser]
Don't follow this. The controller name ('shop' in this case) provides segment 1. The method ('category' in this case) provides segment 2. What will provide a segment 3? Where does the 'something' come from? The only time I've seen a segment 3 is with pagination where the offset is segment 3.
#4

[eluser]Carmichael[/eluser]
Read about routes here: http://ellislab.com/codeigniter/user-gui...uting.html

application/config/routes.php
Code:
$route['shop/category/:(any)'] = 'shop/category';
will let you have URL's like this: site.com/shop/category/something
#5

[eluser]Samus[/eluser]
[quote author="Perkin5" date="1333839642"]Don't follow this. The controller name ('shop' in this case) provides segment 1. The method ('category' in this case) provides segment 2. What will provide a segment 3? Where does the 'something' come from? The only time I've seen a segment 3 is with pagination where the offset is segment 3.[/quote]
You have a method like this:

Code:
function($one, $two, $three) {

}

$one then becomes the third parameter,
$two becomes the fourth,
$three becomes the fifth.

I don't know how else I can explain this, as it's pretty much the basics of CI.
#6

[eluser]Perkin5[/eluser]
Ah yes, I wasn't aware of this - thanks a lot. I will read this carefully, it clearly answers my question.
#7

[eluser]Perkin5[/eluser]
Actually, having read it carefully, I don't think it does answer my question. URI routing appears to be a way of modifying an existing URI so it will link to a different method. That's not what I want. Can I put it another way?

I can write:

Code:
redirect('model/method/'.$variable);

and when 'method' is called, the URI is like this:

Code:
www.site.com/model/method/$variable

So what I'm looking for is a way, within 'method', to add the third segment on to the URI just like I can do it with redirect.

Is that possible?
#8

[eluser]minerbog[/eluser]
[quote author="Samus" date="1333840983"][quote author="Perkin5" date="1333839642"]Don't follow this. The controller name ('shop' in this case) provides segment 1. The method ('category' in this case) provides segment 2. What will provide a segment 3? Where does the 'something' come from? The only time I've seen a segment 3 is with pagination where the offset is segment 3.[/quote]
You have a method like this:

Code:
function($one, $two, $three) {

}

$one then becomes the third parameter,
$two becomes the fourth,
$three becomes the fifth.

I don't know how else I can explain this, as it's pretty much the basics of CI.[/quote]

Now you learn something everyday!!

I've been using CI for nearly two years and I didn't know that!! However, if you don't always need a third parameter the function will then break, which is why I would always get the segments via $this->uri->segments(n);

But still great to know! :-)

Gav.
#9

[eluser]minerbog[/eluser]
[quote author="Perkin5" date="1333891717"]Actually, having read it carefully, I don't think it does answer my question. URI routing appears to be a way of modifying an existing URI so it will link to a different method. That's not what I want. Can I put it another way?

I can write:

Code:
redirect('model/method/'.$variable);

and when 'method' is called, the URI is like this:

Code:
www.site.com/model/method/$variable

So what I'm looking for is a way, within 'method', to add the third segment on to the URI just like I can do it with redirect.

Is that possible?[/quote]

Sorry, really don't get what your trying to do 8-/
#10

[eluser]Samus[/eluser]
[quote author="minerbog" date="1333893116"][quote author="Samus" date="1333840983"][quote author="Perkin5" date="1333839642"]Don't follow this. The controller name ('shop' in this case) provides segment 1. The method ('category' in this case) provides segment 2. What will provide a segment 3? Where does the 'something' come from? The only time I've seen a segment 3 is with pagination where the offset is segment 3.[/quote]
You have a method like this:

Code:
function($one, $two, $three) {

}

$one then becomes the third parameter,
$two becomes the fourth,
$three becomes the fifth.

I don't know how else I can explain this, as it's pretty much the basics of CI.[/quote]

Now you learn something everyday!!

I've been using CI for nearly two years and I didn't know that!! However, if you don't always need a third parameter the function will then break, which is why I would always get the segments via $this->uri->segments(n);

But still great to know! :-)

Gav.[/quote]

Or you can just give the function a default value.

Code:
function samus($one = '', $two = 'fourth') {

}
which prevents the undefined variable message.




Theme © iAndrew 2016 - Forum software by © MyBB