Page 1 of 1

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

Posted: Sat Sep 20, 2008 10:03 pm
by danielperaza
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.

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

Posted: Wed Sep 24, 2008 8:56 am
by dam-man
I'm doing the same as you.

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

Posted: Wed Sep 24, 2008 6:01 pm
by danielperaza
What do you mean with that?, is it working for you?, why doesn't work for me too?

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

Posted: Wed Nov 19, 2008 6:00 pm
by copesc
This isnt't working for me, too. Do we have to register that 'limitstart' var somewhere?

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

Posted: Thu Nov 20, 2008 4:56 am
by ianmac
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

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

Posted: Thu Nov 20, 2008 5:37 pm
by danielperaza
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.

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

Posted: Thu Nov 20, 2008 6:40 pm
by ianmac
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

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

Posted: Fri Nov 21, 2008 6:45 pm
by danielperaza
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.

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

Posted: Fri Nov 21, 2008 8:04 pm
by ianmac
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

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

Posted: Sat Nov 22, 2008 12:52 am
by danielperaza
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.

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

Posted: Sat Nov 22, 2008 5:25 am
by ianmac

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

Posted: Tue Jan 06, 2009 8:32 pm
by tomgreever
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?

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

Posted: Tue Feb 16, 2010 5:38 am
by boxcarmib
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');

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

Posted: Tue Feb 16, 2010 2:56 pm
by ianmac
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

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

Posted: Wed Feb 17, 2010 2:59 am
by danielperaza
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.

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

Posted: Tue Mar 09, 2010 2:56 pm
by jtanmay
Was looking for long for a solution around pagination.
I was facing the same problem.

Thanks guys

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

Posted: Thu Nov 18, 2010 4:34 am
by hookdump
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!!! :)

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

Posted: Wed Aug 22, 2012 3:25 pm
by ggerstein
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.