Welcome Guest, Not a member yet? Register   Sign In
Problem using uri->segment - should be easy....[SOLVED]
#1

[eluser]tinawina[/eluser]
Hi CodeIgniters -

This is totally strange. I'm just trying to throw together a simple rating script. People log in and get the opportunity to rate an article. They click on a graphic -- stars representing a rating of 1, 2, 3, 4, or 5. The URL leads to my ratings controller and looks like this:

http://mysite.org/ratings/name_of_article/1 (if they chose to give a "1" rating)

In my routes.php file I have this setting established:

$route['ratings/:any'] = "ratings/index";

I can't seem to get anything out of $this->uri->segment(3) which would be the rating. I can get the article name from $this->uri->segment(2) with no problem. But where segment(3) is concerned I get this error message:

Fatal error: Call to a member function segment() on a non-object.....

That message refers to the line that tries to assign $this->uri->segment(3) to $this->rating. I'm not sure what's gone wrong -- this always has worked for me just fine before. Any ideas?

Thanks for your help!

Ratings controller:

Code:
class Ratings extends Controller
{
    function __construct()
    {
        parent::Controller();

        // Login check
        if ($this->session->userdata('logged_in') != true) { header("Location: /log/in"); }

        $this->load->model('ratings_data');
        $this->uri = $this->uri->segment(2);
        $this->rating = $this->uri->segment(3);
        $this->screen_name = $this->session->userdata('screen_name');
    }

    function index()
    {
        // check to make sure this user hasn't already rated this article

        $rated_by_user = $this->ratings_data->checkUser($this->uri, $this->screen_name);

        if ($rated_by_user === true)
        {
            redirect ('article/' . $this->uri); // this user already submitted a rating, send back to article
        }
        else // this user hasn't rated this article yet. add to ratings table and send back to article with rating included in tally.
        {
            $update = $this->ratings_data->updateListing($this->uri, $this->rate, $this->screen_name);
            redirect ('article/' . $this->uri);
        }
    }
}
#2

[eluser]JamesD[/eluser]
[quote author="tinawina" date="1197766261"]Hi CodeIgniters -

This is totally strange. I'm just trying to throw together a simple rating script. People log in and get the opportunity to rate an article. They click on a graphic -- stars representing a rating of 1, 2, 3, 4, or 5. The URL leads to my ratings controller and looks like this:

http://mysite.org/ratings/name_of_article/1 (if they chose to give a "1" rating)

In my routes.php file I have this setting established:

$route['ratings/:any'] = "ratings/index";

I can't seem to get anything out of $this->uri->segment(3) which would be the rating. I can get the article name from $this->uri->segment(2) with no problem. But where segment(3) is concerned I get this error message:

Fatal error: Call to a member function segment() on a non-object.....

That message refers to the line that tries to assign $this->uri->segment(3) to $this->rating. I'm not sure what's gone wrong -- this always has worked for me just fine before. Any ideas?

Thanks for your help!
[/quote]

Hello,

Have you tried to use the rsegment function to get the newly routed segment?
#3

[eluser]tinawina[/eluser]
Hi and yes - I tried the rsegment and got -

Fatal error: Call to a member function rsegment() on a non-object...

This is so weird - I use $this->uri->segment() all over the place and never had a problem. This one has me perplexed!

Appreciate any other ideas!
#4

[eluser]tonanbarbarian[/eluser]
You problem is that you are destroying the uri object and then trying to access it
Code:
$this->uri = $this->uri->segment(2);
$this->rating = $this->uri->segment(3);
this first line is replacing the uri object with the 2nd segment
then in the very next line you are trying to access the uri object again which no longer exists
change to something like this
Code:
$this->id = $this->uri->segment(2);
$this->rating = $this->uri->segment(3);
and then elsewhere in your code change $this->uri to $this->id
#5

[eluser]ejangi[/eluser]
I have found that using the php5 __construct() doesn't work so well for me... Try doing just:
Code:
class Ratings extends Controller
{
    function Ratings()
    {
        parent::Controller();
    }
}
#6

[eluser]tinawina[/eluser]
I just recoded the controller to use $_SERVER['REQUEST_URI'] rather than $this->uri->segment and it works. But, because I LOVE CI, I'm going to undo what I did and swap out $this->uri for $this->article as per tonanbarbarian's suggestion. I totally see what you are saying! And I knew it wasn't CI letting me down!

ucantblamem -- I've read this about php5 and problems using __construct. I haven't had a problem, but will stick that tip in my programming toolbox as a test when all else fails.

Thanks for all the help! I have pasted my $_SERVER['REQUEST_URI'] controller below just in case someone wants to use it for.....something. ~tinawina

Code:
function index()
    {
        $uri = $_SERVER['REQUEST_URI'];    

        $uri = eregi_replace('/ratings/', '', $uri);
        $explode_uri = explode('/', $uri);
        $rate_uri = $explode_uri[0];
        $rating = $explode_uri[1];

        $rated_by_user = $this->ratings_data->checkUser($rate_uri, $this->screen_name);
        if ($rated_by_user === true)
        {
            redirect ('article/' . $rate_uri);
        }
        else
        {
            $update = $this->ratings_data->updateListing($rate_uri, $rating, $this->screen_name);
            redirect ('article/' . $rate_uri);
        }
    }
#7

[eluser]tinawina[/eluser]
Ok - recoded so that $this->uri is now $this->article and $this->rating = $this->uri->segment(3); Works like a charm. THANKS!




Theme © iAndrew 2016 - Forum software by © MyBB