Help with php in calc field

molcomp

New Member
Hello ,

I have the following code in my calc field:

--------------------------------------------------------------------------

// Get a db connection.

$myDb = \Joomla\CMS\Factory::getContainer()->get('DatabaseDriver');

// Create a new query object.

$myQuery = $myDb->getQuery(true);

$myQuery
->select(array('subactivity.subactivity_name'))
->from('subactivity')
->join('INNER', 'intemgroup1 ON intemgroup1.subactivity_name=subactivity.id')
->join('INNER', 'projects ON projects.id=intemgroup1.project_name')
->join('INNER', 'activities ON activities.id=intemgroup1.activity_name')
->where('projects.project_name = ' . $myDb->quote('{overlapping___project_1}') )
->where('activities.activity_name = ' . $myDb->quote('{overlapping___activity_1}'));

// Assign the query to the db

$myDb->setQuery($myQuery);

// Load the results as an array of objects.

$rows = $myDb->loadObjectList();
$list = array();

foreach ($rows as $row)
{ $list[] = "<option style=\"max-width=100px;\" value=\"{$row->id}\">" . $row->subactivity_name . "</option>";
}

return '<select style="width: 400px;height: 150px;word-wrap: break-word;word-break: break-all; white-space: normal;" name="subactivity">' . implode($list) . '</select>';

--------------------------------------------------------------------------

It all works more or less fine in the form. The problem is that after i click the submit button and the list shows up the formed data is saved and entered in the database with the whole code not just the value of the selected item. So at the list I get again the dropdown menu from the calc field rather then just the value selected in the form.

Any advise suggestion how to resolve it ? I have attached screenshot of the list which shows the calc field as dropdown rather then the value selected in the form dropdown .
 

Attachments

  • Screenshot from 2024-03-15 10-55-30.png
    Screenshot from 2024-03-15 10-55-30.png
    57.3 KB · Views: 20
Last edited:
A calc is no element for input. It's doing exactly what you say: returning the dropdown.

Use a dropdown element with eval options (or a dbjoin with Data-where) and custom CSS for styling.
 
Thank you @troester . Can you please point me to some example of dropdown element eval options and data where how they look like. I tried replicating the calc code in the dropdwon menu but I am having problems with the syntax how should I enter in eval and data where. Wiki says too basic for it.
Thank you again for continues help.

Alex
 
 
Just to add I need to use either databasejoin or cascading element since this field depends on two previous database join fields .
 
@troester I got what you pointed out and I got the dropdown working with eval option but I am having a problem with the sql query. Here is my query:
$myQuery
->select(array('subactivity.id', 'subactivity.subactivity_name'))
->from('subactivity')
->join('INNER', 'intemgroup1 ON intemgroup1.subactivity_name=subactivity.id')
->join('INNER', 'projects ON projects.id=intemgroup1.project_name')
->join('INNER', 'activities ON activities.id=intemgroup1.activity_name')
->where('projects.project_name = ' . $myDb->quote('{overlapping___project_1}') )
->where('activities.activity_name = ' . $myDb->quote('{overlapping___activity_1}'));

If I just use the basic table query
->select(array('subactivity.id', 'subactivity.subactivity_name'))
->from('subactivity')

I get the list of all the items, however when I use the full query I get empty field. In the calc field there was ajax on load option is that the reason why its not working or its something else ? Not sure which way to make the query work.
Thank you very much
Alex
 
Dropdown has no ajaxupdate, it's running only on form load.

In this case you need the dbjoin with ajaxupdate and Data-where (which is SQL, not php)
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top