Welcome Guest, Not a member yet? Register   Sign In
Controller Passing Varibles in URL
#1

[eluser]bdshan[/eluser]
From the user guide:

Quote:If your URI contains more then two segments they will be passed to your function as parameters.

For example, lets say you have a URI like this:

example.com/index.php/products/shoes/sandals/123

Your function will be passed URI segments 3 and 4 ("sandals" and "123"):

I have a url that looks like this: index.php/lookup/stats/bdshan

So I am trying to call the stats function of the lookup controller with a value of 'bdshan'. Simple enough. Here is my controller:

Code:
<?php
    class Lookup extends Controller
    {
    function index()
    {
        echo 'No LFSID Selected';
    }

    function stats($name)
    {
        echo "Segment 3 is: " . $this->uri->segment(3);
        /*
        $sql = "SELECT S.lfsname, S.races_started, S.races_completed, S.laps_completed, S.yellow_flags, S.safety_rating
        FROM safety_stats S
        WHERE S.license_req = '" . $name . "'";
        $data['user'] = $query = $this->db->query($sql);
            $this->load->view('lookup', $data);
        */
    }

    }
?>

I have the echo statement in there just for testing what is getting passed to the function. If I take it out, and uncomment of query code, I get this error:

Quote:A PHP Error was encountered
Severity: Warning

Message: Missing argument 1 for Lookup:Confusedtats()

Filename: controllers/lookup.php

Line Number: 9

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: name

Filename: controllers/lookup.php

Line Number: 15

Why isn't my function getting the variable passed to in from the uri?
#2

[eluser]hvalente13[/eluser]
Hi bdshan,

If you are trying to call the function in other function of the controller, you have to call it like this:
Code:
function_name($this->uri->segment(3))
where the function_name=stats and uri->segment is going to change the $name var in your function stats.

If you are trying to call the function as a page of the controller, you don't have to give it any parameters, as you can pass it through as uri->segment.

Hope this helps
#3

[eluser]bdshan[/eluser]
Maybe you miss understood me, I am trying to call the page like this: http://localhost/tieredracing/index.php/...ats/bdshan

But I get this:
Quote:A PHP Error was encountered
Severity: Warning

Message: Missing argument 1 for Lookup:Confusedtats()

Filename: controllers/lookup.php

Line Number: 9

A PHP Error was encountered
Severity: Notice

Message: Undefined variable: name

Filename: controllers/lookup.php

Line Number: 15

From the errors I can tell that it is using the correct controller and function, but the 3rd uri segement is not getting passed to the function as the user guide states it should.
#4

[eluser]bdshan[/eluser]
I got it working. I was reloading the uri library by accident. Once I removed it from autoload all is working as expected.

Thanks
#5

[eluser]hvalente13[/eluser]
Hi bdshan,

As i've told you, you can access the http://localhost/tieredracing/index.php/...ats/bdshan by running function stats() as a page of the controller.

In this case you don't have to give it any parameters... bdshan will be the uri->segment(3).

So i'll do it like this:

Code:
<?php
    class Lookup extends Controller
    {
    function index()
    {
        echo 'No LFSID Selected';
    }

    function stats()
    {
        /* If $name is the variable that you need to call uri->segment(3), it should be like this */

        $name = $this->uri->segment(3);

        $sql = "SELECT S.lfsname, S.races_started, S.races_completed, S.laps_completed, S.yellow_flags, S.safety_rating
        FROM safety_stats S
        WHERE S.license_req = '" . $name . "'";

        $data['user'] = $query = $this->db->query($sql);
            $this->load->view('lookup', $data);

    }

    }
?>

Tell me if is this work.

Good luck
#6

[eluser]Bl1nk[/eluser]
Ive been looking for a solution of this problem but I found this thread very the same with my problem. I installed new version of codeigniter 1.7.1 with xampp 1.7.2, I dont know if these are compatible.

When I tried this URL:

http://localhost/mostrag/index.php?/most...9156824269

It outputs an error of: Message: Missing argument 1, which I think that my code is correct.

My class looks like this:

class Mostrag extends Controller {

// ...

public function profile($id)
{
// ...
}
}


I hope you can suggest what’s happening in my xampp and codeigniter. Im doing this in Windows Vista.


Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB