Drupal onChange auto populate another CCK select list - forms

I have built a multistep form using CCK, however I have a couple of issues outstanding but, not sure if CCK can help.
In one step of the form I have 2 select boxes, the first is auto populated from the vocabulary table with the following code and all woks well.
$category_options = array();
$cat_res = db_query('select vid, name from vocabulary WHERE vid > 1 ORDER BY name ASC');
while ($cat_options = db_fetch_object($cat_res)) {
$category_options[$cat_options->vid] = $cat_options->name;
}
return $category_options;
What I would like to do is, when a user selects one item from the vocablulary list it auto populates another select box with terms from the term_data table. I have 2 issues;
1) I have added the following code to the second select list, just to make sure it works (IT DOESN'T). There a multiple terms associated with each vocabulary, but the second sql statemant only returns one result when it should return several, (SO SOMETHING WRONG HERE). For example in the term_date table there are 6 terms with the vid of 3, but I only get one added to select list.
$term_options = array();
$term_res = db_query('select vid, name from term_data WHERE vid = 3 ORDER BY name ASC'); while ($options = db_fetch_object($term_res)) {
$term_options[$options->vid] = $options->name;
}
return $term_options;
2) Can I add an onChange to the first select list to call a function to auto populate second list using CCK, or do I have to lean towards doing my entire form using the FORM API.
Any help or thoughts would be very much appreciated.

It seems to be a mistake in the query that gets terms. I tried to correct:
$term_res = db_query('select tid, name from term_data WHERE vid = 3 ORDER BY name ASC');
while ($options = db_fetch_object($term_res)) {
$term_options[$options->tid] = $options->name;
}
In your code you selected vid that is actually equal for all terms. Then you added terms names to $term_options array under the same key => so you got only 1 element.
Considering the second question: I would send the whole data structure (all vocabularies and their terms) as json to the client (insert a js script to the page from your drupal code) and implement the desired functionality with jquery.

Related

Is there a way to work with records that you marked in table on X++?

I want to work with table records that is selected (Marked), Example - I have 10 records in table and i mark 5 of them. Expected - when i work with selected records, it should look only on that 5 records, not whole 10.
So far i have this code that selects all records:
while select Table
where table.JournalId == table.JournalId
Is there a way to make it select only marked records, not everything?
That select is writen in class. I need to get those marked records into that class where that select is writen...
You need to pass the formdatasource object into the class that is selecting the records, and then use the MultiSelectionHelper to loop through the selected records on the formdatasource.
In the example below, the object salesTableFormDataSource needs to be passed in from the form to the class you are using. Obviously replace that with whatever your datasource/table needs are.
MultiSelectionHelper selection = MultiSelectionHelper::construct();
selection.parmDatasource(salesTableFormDataSource);
SalesTable salesTable = selection.getFirst();
while (salesTable)
{
//do something with your table buffer.
salesTable = selection.getNext();
}

Filter portal for most recently created record by group

I have a portal on my "Clients" table. The related table contains the results of surveys that are updated over time. For each combination of client and category (a field in the related table), I only want the portal to display the most recently collected row.
Here is a link to a trivial example that illustrates the issue I'm trying to address. I have two tables in this example (Related on ClientID):
Clients
Table 1 Get Summary Method
The Table 1 Get Summary Method table looks like this:
Where:
MaxDate is a summary field = Maximum of Date
MaxDateGroup is a calculated field = GetSummary ( MaxDate ;
ClientIDCategory )
ShowInPortal = If ( Date = MaxDateGroup ; 1 ; 0 )
The table is sorted on ClientIDCategory
Issue 1 that I'm stumped on: .
ShowInPortal should equal 1 in row 3 (PKTable01 = 5), row 4 (PKTable01 = 6), and row 6 (PKTable01 = 4) in the table above. I'm not sure why FM is interpreting 1Red and 1Blue as the same category, or perhaps I'm just misunderstanding what the GetSummary function does.
The Clients table looks like this:
Where:
The portal records are sorted on ClientIDCategory
Issue 2 that I'm stumped on:
I only want rows with a ShowInPortal value equal to 1 should appear in the portal. I tried creating a portal filter with the following formula: Table 1 Get Summary Method::ShowInPortal = 1. However, using that filter removes all row from the portal.
Any help is greatly appreciated.
One solution is to use ExecuteSQL to grab the Max Date. This removes the need for Summary functions and sorts, and works as expected. Propose to return it as number to avoid any issues with date formats.
GetAsTimestamp (
ExecuteSQL (
"SELECT DISTINCT COALESCE(MaxDate,'')
FROM Survey
WHERE ClientIDCategory = ? "
; "" ; "";ClientIDCategory )
)
Also, you need to change the ShowInPortal field to an unstored calc field with:
If ( GetAsNumber(Date) = MaxDateGroupSQL ; 1 ; 0 )
Then filter the portal on this field.
I can send you the sample file if you want.

Universe basic convert a select list to an array?

I have one major select list in a program
EXECUTE 'SELECT PRODUCTS WITH DEL.DATE <= "':EOM.DATE;'"' CAPTURING OUTPUT
I then want to covert the select list to an array, is there a Universe basic function to do this or do I need to write a function?
Use READLIST to read the contents of your active select list into a field delimited (#FM) dynamic array. This is a paradigm I employ when working with multiple select lists and the payloads aren't too large. You can also select into a different list other than the default of 0 but that gets a bit messy intellectually.
EXECUTE 'SELECT PRODUCTS WITH DEL.DATE <= "':EOM.DATE;'"' CAPTURING OUTPUT
READLIST PRODUCTS.LIST ELSE PRODUCTS.LIST = ''
PRODUCTS.COUNT = DCOUNT(PRODUCTS.LIST,#FM)
FOR X=1 TO PRODUCTS.COUNT
ID.PRODUCTS = PRODUCTS.LIST<X>
;* Your per ID magic goes here
NEXT X
Good luck!

get most actual row with zend's fetchRow

I'm very new to zend (1.12)..so please excuse my very basic question:
I want to fetch only one row from a database. Thatfore I want to use the fetchRow(..) function like this
$row = $db->fetchRow($db->select()->where("col1 = '".val1."' AND col2='".val2."'"));
The problem is, that there may be many rows that fit to the where-clause and I only want to get the one with the highest id. How can I do this?
The fetchRow() method returs only one row. If you want to choose row with the highest ID meeting other conditions, invoke it like this:
$select = $db->select()
->where('col1 = ?', $val1)
->where('col2 = ?', $val2)
->order('id DESC');
$row = $db->fetchRow($select);
Also, remember to pass values to SQL query in the way as in above code (to avoid SQL injection attack risk).

Best way to do an Inner Join using the Zend Framework?

It seems like there's a few different ways to join two tables using the Zend Framework, but I've never done it before so I don't know which is the best way to do it.
This is what I'm trying to do...
I have 3 tables in my database:
users
( id , name )
groups
( id , name )
group_members
( id , group_id , user_id )
I'm trying to look up the groups that a user belongs to and display that to the user. This SQL statement pretty much does the job (though there may be a better way to write it). It only returns the columns I'm concerned with which are the group's id and title.
SELECT groups.id, groups.title
FROM group_members
INNER JOIN groups
ON groups.id = group_members.group_id
WHERE user_id = $userId
How can I do this with the Zend Framework?
Finally figured out how to do it. If you've got a better way, please let me know.
$db = Zend_Db_Table::getDefaultAdapter(); //set in my config file
$select = new Zend_Db_Select($db);
$select->from('groups', array('id', 'title')) //the array specifies which columns I want returned in my result set
->joinInner(
'group_members',
'groups.id = group_members.group_id',
array()) //by specifying an empty array, I am saying that I don't care about the columns from this table
->where('user_id = ?', $userId);
$resultSet = $db->fetchAll($select);
This will return a table with only the id and title columns. The empty array() was the key to removing the columns I didn't care about. I could then do something with the result set.
foreach ($resultSet as $row) {
//do something with $row->id or $row->title
}
No need to using Join,we can use Zend_Db_Table instead for the reason about the MVC pattern. I got this idea form here,#10 by Filip.(maybe they call this "Table Data Gateway"?)