Welcome Guest, Not a member yet? Register   Sign In
How to pass parameter in "index" function?
#1

[eluser]Sinclair[/eluser]
Hi,

I need to know if it is possible to pass parameters in the index function of the controller.

My code is for Tag listing and I neet to have an URL like http:\\mysite.com\tag\here-is-the-tag

How can I do this?

This is my controller code that is not working:

Code:
<?php

class Tag extends Controller {
    
    function Tag() {
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('array');
        $this->load->library('image_lib');
        $this->load->helper('my_addjsfile_helper');
        $this->load->helper('my_addbodytag_helper');
    
    }
    
    function index($pn_tag) {
        $this->load->model('Template_model');
        
        #BEGIN PASSAGENS P O TEMPLATE PRINCIPAL
        # Aqui coloco as configuracoes estáticas da funcao
        $function_name = 'tag_index'; # [nomedocontroller_nomedafuncao]
        # Aqui coloco as configuracoes dinamicas da funcao
        $title = 'Tags...'; # Aqui defino o título da página
        #Aqui passo as zonas para o template principal
        $resultTemp = $this->Template_model->getZonasComAnunciosActivos();
        #END PASSAGENS P O TEMPLATE PRINCIPAL
        

        # Neste bloco defino a passagem de variáveis para a view
        $this->data_view['result'] = $this->Anuncios_model->getAnunciosTag($pn_tag = $this->uri->segment(2));
        
        
        # Neste bloco defino a passagem de variáveis para o template_view
        $this->data_template['function_name'] = $function_name;
        $this->data_template['title'] = $title;
        $this->data_template['resultTemp'] = $resultTemp;
        
        $this->load->view ('common/template_view', $this->data_template); # Aqui leio o template comum em "common/template_view.php"
    }
    
    
}

?>
#2

[eluser]Shay Falador[/eluser]
The url http:\\mysite.com\tag\here-is-the-tag would get the Tag controller and here-is-the-tag method.
If you want to pass it to the index use a route.

Something like that:
$route['tag/([^/]+)'] = 'tag/index/$1';

Goodluck!
#3

[eluser]jedd[/eluser]
[quote author="Sinclair" date="1264866835"]
I need to know if it is possible to pass parameters in the index function of the controller.
[/quote]

Okay - here's what you do.

You read the [url="/wiki/FAQ"]FAQ[/url].

Sure, the question in the FAQ is actually worded: How do I pass parameters to a controller’s index() function? - so it's possible that if you searched for 'in "index"' or 'in the index' this might be why you didn't find it when you [url="http://codeigniter.com/wiki/How_to_ask_a_good_question/"]looked before asking[/url].
#4

[eluser]Unknown[/eluser]
please check user guide before asking,,

Just use remapping funtion call : http://ellislab.com/codeigniter/user-gui...#remapping


Code:
function index($pn_tag)
{
echo $pn_tag
}

function _remap($method)
{
    if ($method == 'some_method')
    {
        $this->$method();
    }
    else
    {
        $this->index($method);
    }
}


just complete code above :cheese:




Theme © iAndrew 2016 - Forum software by © MyBB