Welcome Guest, Not a member yet? Register   Sign In
pass a variable to a controller
#1

[eluser]Corbee[/eluser]
Hi, I'm a newbie and am experimenting with a walkthrough and am trying to pass a variable to another controller. how do I do that?

here is my code. I wanted to pass the $name
Code:
<?php
    if (count($navlist))
    {
    echo "<ul>";
    foreach ($navlist as $id => $name)
    {
    echo "<li>";
    echo anchor(base_url()."index.php/products/pdcat/$id",$name);
    echo "</li>"; ?&gt;
    <img src="&lt;?php echo base_url(); ?&gt;images/line-space.png" alt="" width="195" height="12" />
        &lt;?php
    }
    echo "</ul>";
    }
                    
    ?&gt;

I tried passing it to the controller below and it didn't work
Code:
function pdcat($name)
{
     echo $name;
}

what do you think is the right way to pass the variable?

Thanks
#2

[eluser]Militis[/eluser]
First, welcome to the wonderful world of Codeigniter.

Second, I'd like you to refer to this page of the user guide: http://ellislab.com/codeigniter/user-gui...elper.html

More specifically, the anchor() section:
Quote:Note: If you are building links that are internal to your application do not include the base URL (http://...). This will be added automatically from the information specified in your config file. Include only the URI segments you wish appended to the URL.

So that line would look something like this:
Code:
echo anchor("products/pdcat/$id",$name);

Now, I'm guessing when you tried this:
Code:
function pdcat($name)
{
     echo $name;
}
you ended up getting $id instead of $name. One way to do it would be to modify your anchor to this:
Code:
echo anchor("products/pdcat/$id/$name",$name);
and your controller to this:
Code:
function pdcat($id, $name)
{
     echo $id;
     echo $name;
}

The params passed to your function in the controller are the URI segments. In the case of your current code, $id. In the case of my modified version, $id and $name in that order.

There are lots of other ways to do it, and some are probably better than others. The user guide was a great help to me while learning and later developing my first projects. It still is, in fact. For your convenience, here's a link to the Table of Contents.
#3

[eluser]Corbee[/eluser]
It works, thank you very much!!!

Also thanks for the links and the explanation. It made things much clear for me now than before.
#4

[eluser]Militis[/eluser]
Not a problem. Good luck on your project.
#5

[eluser]Mimi kesini[/eluser]
i have try the code but not working..
please help me.. i want to pass the variable to other file index... Sad

in my login controller

$data = array();
$this->load->model('login_model');
$user = $data['Username'];
echo anchor('edit_user',$user);

in my user_edit controller

function index($user)
{
echo $user;
//$this->load->model('motools_model');
//$un = $user;
//$data = array();
//$data = $this->motools_model->editUser($un);

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

they said: Missing argument 1 for edit_user::index() and Undefined variable: user

please help me.... Sad
#6

[eluser]Klain[/eluser]
[quote author="Mimi kesini" date="1292500575"]i have try the code but not working..
please help me.. i want to pass the variable to other file index... Sad

in my login controller

$data = array();
$this->load->model('login_model');
$user = $data['Username'];
echo anchor('edit_user',$user);

in my user_edit controller

function index($user)
{
echo $user;
//$this->load->model('motools_model');
//$un = $user;
//$data = array();
//$data = $this->motools_model->editUser($un);

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

they said: Missing argument 1 for edit_user::index() and Undefined variable: user

please help me.... Sad[/quote]
login controller:

echo anchor('edit_user/'.$user,"Edit user");

the url will be something like : xxx/index.php/edit_user/user_id




Theme © iAndrew 2016 - Forum software by © MyBB