Problem using uri->segment - should be easy....[SOLVED] |
[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
[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?
[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!
[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); 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);
[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
[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()
[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! |
Welcome Guest, Not a member yet? Register Sign In |