Page 1 of 1

Variables

PostPosted: Sun Aug 12, 2012 3:52 am
by Philipp Wenzel
Hi Steve,
i really like our work. It make my work so much easier. Thanks again.

Lately i have a little problem:
I try to make dynamic valuelist entries, like u explain in the Using Value-list tutorial. Further more i'd like to manipulate the query with a simple WHERE adding in the code. This is the Basic, like the one in the tutorial and it works fine.

Code: Select all
[gesetz]
__sql__ = "SELECT gesetz_id, gesetz_name FROM gesetz"


Now with the WHERE:

Code: Select all
[gesetz]
__sql__ = "SELECT gesetz_id, gesetz_name FROM gesetz WHERE nation_id = '$nation_id'"

as result i have an empty valuelist.

im not sure if the variable is right. how are the db filds are stored in a variable? Or is it possible to do a WHERE querry in the valuelist.ini?

Greetz,
Philipp

Re: Variables

PostPosted: Mon Aug 13, 2012 9:26 pm
by shannah
Valuelists don't accept variables, since they are meant to be loaded once per request. If you really need to change the valuelist depending on context, then you could implement the valuelist with a delegate class method (e.g. valuelist__xxx()) try to make it so that it only makes the db request once per http request for performance reasons.

Re: Variables

PostPosted: Tue Aug 14, 2012 1:23 am
by Philipp Wenzel
Alright! Thanks a lot.
I will try the delegate class thingy :)

Re: Variables

PostPosted: Tue Aug 14, 2012 10:51 am
by Philipp Wenzel
Hi,
i tried to use the valuelist__xxx() function an after a research in the forum this, what i came up with:

Code: Select all
   function valuelist__gesetz(){   
      $res = mysql_query("select gesetz_id, nation_id, gesetz_name from gesetz WHERE nation_id = '$record->val('nation_id')'");
      $out = array();
       while ( $row = mysql_fetch_assoc($res) ) $out[$row['gesetz_id']] = $row['gesetz_name'];
      return $out;
   }


this happens in a related table (mastertable is nation, from the master i need the selected nation_id information).
but it wont work. im guessing it is something about the variable '$record->val('nation_id')'. But i can't figure it out. My programming skills are really not that good. :/

How can i access the selected data from the mastertable to perform this query?
Greetings,
Philipp

Re: Variables

PostPosted: Mon Aug 20, 2012 10:56 am
by shannah
It sounds like you may be using a valuelist for something that it can't do very will. My guess is that you have a select list in an edit form and you want it to show different options depending on which record is being edited.

If this is the case, then you will need to do some Javascript to dynamically build the select list depending on the values in other fields. I have developed a depselect widget that requires Xataface 2.0 which handles this sort of thing automatically:
http://xataface.com/dox/modules/depselect/latest/

You can take a look at the source of this for some hints. There have also been a number of other discussions in the forum on how to build similar things.

-Steve