SandyK
SandyK

Reputation: 465

codeigniter uri segment not working

I have a Ajax function -

$(document).ready(function(){
$.ajax({
        url: base_url+'movies/index/this_week/hindi',
        type: "POST",
        success: function( data )
        {               
        $("#news1").html(data);
        }                 
     });
});

In my movies controller, when i print $this->uri->segment(2) or $this->uri->segment(3) in _remap() function it always return random value. Sometimes it returns index for both statements or sometimes it returns 'index' and 'hindi' etc.

All the time when i refresh the page, it returns random value. I am getting confused. Why it occurs?

Here is _remap() function.

function _remap()
{
    $segment_1 = $this->uri->segment(1);
    echo "1==>".$this->uri->segment(1);
    echo " 2==>".$this->uri->segment(2);
    echo " 3==>".$this->uri->segment(3);
    echo " 4==>".$this->uri->segment(4);

    switch ($segment_1) {
        case null:
              case false:
              case '':
                     $this->index();
                     break;

              case 'movies':
                    if($this->uri->segment(2) == "index" && $this->uri->segment(3) == "this_week")
                   {
                            $this->moviedescription($this->uri->segment(4));
                   }
                   else
                   {
                         $this->moviedescription();
                   }
                   break;

            default:
                    $this->index();
            break;
            }
        }

Any help appreciated. Thanks in advance.

Upvotes: 0

Views: 6451

Answers (3)

sakibmoon
sakibmoon

Reputation: 2032

I have checked your code in my computer and it works perfectly. So, the problem is not in the code if base_url is defined correctly in JAVASCRIPT. Double check that.

You may have set other routes in routes config file which is messing with the url or you may have problem with .htaccess file.

Upvotes: 0

Shomz
Shomz

Reputation: 37701

There's probably a better approach than using uri segments. Try using function parameters, like this:

Your movies controller:

public function index($when = false, $type = false) { 
    ...
    echo $when, ', ', $type;
    // $when should be this_week and $type should be hindi, in your example)
    // $when isn't the best variable name, but you get the idea
}

And then just access those parameters normally as variables as they will always be the same, no random values. I've set their default values to false just in case you don't use them all the time (for example, if you have the default movies page).

Upvotes: 1

This_is_me
This_is_me

Reputation: 918

it should not give you random values, that realy wierd and don't now for sure.

when you make your ajax request (from your example) to the controller, the reveiving controller should have:

echo $this->uri->segment(1); // result "movies"
echo $this->uri->segment(2); // result "index"
echo $this->uri->segment(3); // result "this_week"
echo $this->uri->segment(4); // result "hindi"

you could pass an second param for the default value.

var_dump($this->uri->segment(5,FALSE)); // result BOOL FALSE

Upvotes: 0

Related Questions