JApplication::getUserStateFromRequest() doesn't seem to work

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

JApplication::getUserStateFromRequest() doesn't seem to work

Post by danielperaza » Sat Sep 20, 2008 10:03 pm

Hi guys!, I'm trying to add pagination to a component but JApplication::getUserStateFromRequest() doesn't seem to apply the default value when limitstart state variable is not passed through the request. So the behavior of JPagination is such, that I am able to navigate to the next pages, but unable to return back to the first page. I had to use JRequest::getVar() instead, in order to achieve the desired behavior.

This is the code I'm using in the constructor (I'll only post this section of code since I'm sure the rest of the code is working properly):

Code: Select all

function __construct($id = NULL)
{
	global $mainframe, $option;
	
	dump($mainframe, 'mainframe');
	dump($option, 'option');
	
	parent::__construct();
		
	$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', 2/*$mainframe->getCfg('list_limit')*/, 'int');
        ///////////////////////////////////////////////////////////////////////////////////////////
        //
        // CODE NOT WORKING PROPERLY:
	// $limit_start = $mainframe->getUserStateFromRequest("$option.limitstart", 'limitstart', 0, 'int');
        //
        //////////////////////////////////////////////////////////////////////////////////////////

	$limit_start = JRequest::getVar('limitstart', 0, 'int'); // <-- Meanwhile Workaround
	dump($limit, 'limit');
	$limit_start = ($limit != 0 ? (floor($limit_start / $limit) * $limit) : 0);
	dump($limit_start, 'limit_start');
	$this->setState('limit', $limit);
	$this->setState('limitstart', $limit_start);
	if($id == NULL)
	{
		$cid = JRequest::getVar('cid', false, 'DEFAULT', 'array');
		if($cid)
		{
			$id = $cid[0];
		}else{
			$id = JRequest::getInt('id', 1);
		}
	}
	$this->setId($id);
	dump($this);
}
As you might see, I'm using JDump() as debugging tool.

User avatar
dam-man
Joomla! Exemplar
Joomla! Exemplar
Posts: 7961
Joined: Fri Sep 09, 2005 2:13 pm
Location: The Netherlands
Contact:

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by dam-man » Wed Sep 24, 2008 8:56 am

I'm doing the same as you.
Robert Dam - Joomla Forum Moderator
Dutch Boards | Joomla Coding Boards | English Support Boards

danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by danielperaza » Wed Sep 24, 2008 6:01 pm

What do you mean with that?, is it working for you?, why doesn't work for me too?

copesc
Joomla! Intern
Joomla! Intern
Posts: 72
Joined: Thu Aug 31, 2006 9:27 am
Location: Italy

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by copesc » Wed Nov 19, 2008 6:00 pm

This isnt't working for me, too. Do we have to register that 'limitstart' var somewhere?

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by ianmac » Thu Nov 20, 2008 4:56 am

danielperaza wrote:Hi guys!, I'm trying to add pagination to a component but JApplication::getUserStateFromRequest() doesn't seem to apply the default value when limitstart state variable is not passed through the request. So the behavior of JPagination is such, that I am able to navigate to the next pages, but unable to return back to the first page. I had to use JRequest::getVar() instead, in order to achieve the desired behavior.
That's actually the point of getUserStateFromRequest - it stores state so that you don't always have to pass the variable in the request. The default value is only used if that state variable hasn't been already set.

If you don't want to keep previous values of limitstart, then you should use JRequest::getVar().

Ian

danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by danielperaza » Thu Nov 20, 2008 5:37 pm

That's actually the point of getUserStateFromRequest - it stores state so that you don't always have to pass the variable in the request. The default value is only used if that state variable hasn't been already set.

If you don't want to keep previous values of limitstart, then you should use JRequest::getVar().
Ok well, I'm sorry for misunderstanding. But Packt's book "Mastering Joomla 1.5 Extension and Framework Development" on its page 226 has a code snippet which uses it in the way I was using it. I don't post it to the forum because I don't know if it's legal to do it.

Honestly the book wasn't clear enough for me, and I think it doesn't explain your point.

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by ianmac » Thu Nov 20, 2008 6:40 pm

danielperaza wrote:
That's actually the point of getUserStateFromRequest - it stores state so that you don't always have to pass the variable in the request. The default value is only used if that state variable hasn't been already set.

If you don't want to keep previous values of limitstart, then you should use JRequest::getVar().
Ok well, I'm sorry for misunderstanding. But Packt's book "Mastering Joomla 1.5 Extension and Framework Development" on its page 226 has a code snippet which uses it in the way I was using it. I don't post it to the forum because I don't know if it's legal to do it.

Honestly the book wasn't clear enough for me, and I think it doesn't explain your point.
I din't write the book and I haven't read much of it, but I do have a copy - what page is it on and I'll see if there is something else they are doing.

Ian

danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by danielperaza » Fri Nov 21, 2008 6:45 pm

It is on its page 226 as I posted before. It might have been that I didn't read carefully enough, but it's more likely that I used it correctly.

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by ianmac » Fri Nov 21, 2008 8:04 pm

danielperaza wrote:It is on its page 226 as I posted before. It might have been that I didn't read carefully enough, but it's more likely that I used it correctly.
Oh yeah, I see that now :)

I'll look at it and see, but, I explained how getUserStateFromRequest is supposed to work.

Ian

danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by danielperaza » Sat Nov 22, 2008 12:52 am

Ok, thank you very much, but please take into account that a wiki article or other media would be of great help for clarifying this topic to other users. It's almost imposible to browse thousands of search results in this forum up to find something useful.

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by ianmac » Sat Nov 22, 2008 5:25 am


User avatar
tomgreever
Joomla! Intern
Joomla! Intern
Posts: 79
Joined: Wed Nov 14, 2007 5:52 pm

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by tomgreever » Tue Jan 06, 2009 8:32 pm

Can you explain how to use getVar? Here's what I've done.
I replaced this in both my model and my view.html.php file:

Code: Select all

$filter_state = $mainframe->getUserStateFromRequest($option.'filter_state', 'filter_state');
With this:

Code: Select all

$filter_state = JRequest::getVar('filter_state','','post');
In my view, I added filter_state to $lists[]

Then in my form I added:

Code: Select all

<input type="hidden" name="filter_state" value="<?php echo $this->lists['filter_state']; ?>" />
Now, my state filter only works the first time I filter. After that, the dropdown does nothing. What am I leaving out?

boxcarmib
Joomla! Apprentice
Joomla! Apprentice
Posts: 46
Joined: Sun Sep 02, 2007 4:08 pm

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by boxcarmib » Tue Feb 16, 2010 5:38 am

granted all the people involved in this post have probably all died or moved on, but in the interests of illuminating anyone who came here looking for an answer to the same problem, the problem appears to simply be syntactic.
the original poster stated in their code that:

Code: Select all

      ///////////////////////////////////////////////////////////////////////////////////////////
        //
        // CODE NOT WORKING PROPERLY:
   // $limit_start = $mainframe->getUserStateFromRequest("$option.limitstart", 'limitstart', 0, 'int');
        //
        //////////////////////////////////////////////////////////////////////////////////////////

the problem is simply the incorrect insertion of quote marks in the function call. The above line SHOULD be:

Code: Select all

$limit_start = $mainframe->getUserStateFromRequest($option.'limitstart', 'limitstart', 0, 'int');

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by ianmac » Tue Feb 16, 2010 2:56 pm

boxcarmib wrote:granted all the people involved in this post have probably all died or moved on, but in the interests of illuminating anyone who came here looking for an answer to the same problem, the problem appears to simply be syntactic.
the original poster stated in their code that:

Code: Select all

      ///////////////////////////////////////////////////////////////////////////////////////////
        //
        // CODE NOT WORKING PROPERLY:
   // $limit_start = $mainframe->getUserStateFromRequest("$option.limitstart", 'limitstart', 0, 'int');
        //
        //////////////////////////////////////////////////////////////////////////////////////////

the problem is simply the incorrect insertion of quote marks in the function call. The above line SHOULD be:

Code: Select all

$limit_start = $mainframe->getUserStateFromRequest($option.'limitstart', 'limitstart', 0, 'int');
Those two are more or less equivalent except your code won't put a period in the state variable name.

Ian

danielperaza
Joomla! Intern
Joomla! Intern
Posts: 54
Joined: Thu Apr 24, 2008 8:01 pm

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by danielperaza » Wed Feb 17, 2010 2:59 am

Wow! all this mess caused just by a simple period? seems unbelieveable, but that's the way programming is ...

Thank you very much.

NOTE FOR NEWCOMERS: I have noticed Packt book has lots of mistakes that haven't seemed to be fixed yet, and with 1.6 at sight, it seems they won't fix them ever anyway. So be careful when following their examples.

jtanmay
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Thu Feb 04, 2010 6:15 pm
Contact:

Re: JApplication::getUserStateFromRequest() doesn't seem to work

Post by jtanmay » Tue Mar 09, 2010 2:56 pm

Was looking for long for a solution around pagination.
I was facing the same problem.

Thanks guys

hookdump
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Tue Dec 22, 2009 11:28 pm

Re: JApplication::getUserStateFromRequest() doesn't seem to

Post by hookdump » Thu Nov 18, 2010 4:34 am

ianmac wrote:
boxcarmib wrote:granted all the people involved in this post have probably all died or moved on, but in the interests of illuminating anyone who came here looking for an answer to the same problem, the problem appears to simply be syntactic.
the original poster stated in their code that:

Code: Select all

      ///////////////////////////////////////////////////////////////////////////////////////////
        //
        // CODE NOT WORKING PROPERLY:
   // $limit_start = $mainframe->getUserStateFromRequest("$option.limitstart", 'limitstart', 0, 'int');
        //
        //////////////////////////////////////////////////////////////////////////////////////////

the problem is simply the incorrect insertion of quote marks in the function call. The above line SHOULD be:

Code: Select all

$limit_start = $mainframe->getUserStateFromRequest($option.'limitstart', 'limitstart', 0, 'int');
Those two are more or less equivalent except your code won't put a period in the state variable name.

Ian
Your version uses a dot as concatenation operator, so the key for a component called com_test would be: "com_testlimitstart". The other verison, would be something like "com_test.limitstart".
BOTH versions should work!

But actually, I've had some problems with this stuff, and using your solution fixed everything :D :D

I don't really understand why is that, but... THANK YOU very much!!! :)

ggerstein
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Fri Feb 10, 2012 7:27 pm

Re: JApplication::getUserStateFromRequest() doesn't seem to

Post by ggerstein » Wed Aug 22, 2012 3:25 pm

Hi,

I had a really hard time with this at first as well. This tutorial really helped me with this subject

http://docs.joomla.org/How_to_use_user_state_variables

On the issue of the syntax for the first parameter of the getUserStateFromRequest() method...
Remember that $option is a variable known by the application class. $option is the name of the component that is being called at the moment so the first parameter which identifies the session key is prefixed with this $option variable to INSURE that the session (or state) variable is identified as specific to your component and does not conflict with any session variable maintained by the Joomla application. That being said you could write it as
$option.'.state_variable'
or
"$option.state_variable"
These are all valid php statements in which $option is replaced by the name of that active component (probably the one you are writing).

Once I did this tutorial and made sense out of that syntax for the first parameter (which seems obvious now, but was confusing at first), I was on my way. I hope this helps some other folks that are trying to wrap their mind around this problem.


Locked

Return to “Joomla! 1.5 Coding”