Welcome Guest, Not a member yet? Register   Sign In
Can I pass URL variables to the index function of a controller ?
#1

[eluser]sirwan.me[/eluser]
Can I pass URL variables to the index function of a controller ?

for example :
Code:
class Post extends Controller {

    function Post()
    {
        parent::Controller();    
    }
    
    function index($username="", $slug=""){
        $this->load->view('header');
        
        $this->load->model('post_model');
        $data['query'] = $this->post_model->get_post($username, $slug);
        
        $this->load->view('post', $data);
        $this->load->view('footer');
    }
}
#2

[eluser]Buso[/eluser]
yes you can

http://ellislab.com/codeigniter/user-gui.../urls.html
#3

[eluser]zhjx922[/eluser]
$this->router->fetch_method() ? 是要这个?
#4

[eluser]suba[/eluser]
can pass variable to the index function.
example:
your url is www.example.com/test/45

you can get url value via two ways.
1
Code:
$this->url->segment(n);// n means 1,2,3,4,etc.,
//or
$this->url->uri_to_assoc(n)
2.
Code:
class Test extends Controller
{
function index($id)//here add argument variable
{
// through the index function you can access first value from url.
//first value means next of controller name.
$this->data['id']=$id;//45
$this->load->view('test_view',$data);
}
}

in your view page
Code:
<html>
<body>
<h1>Id is&lt;?=$id?&gt;</h1>&lt;!-- 45 --&gt;
&lt;/body&gt;
&lt;/html&gt;
#5

[eluser]sirwan.me[/eluser]
hi suba when i do your second method it doesnt work..

it doesnt seem to allow it when i pass variables to the index function of a controller, but works with all the other functinos on in the controller




Theme © iAndrew 2016 - Forum software by © MyBB