Return a value to another element?

zebrafilm

Member
I have several calculations that could be more efficient if I could put part of the result in another element.
Is there a way to do this from a calc element?

Lots of my calculations are lookups and instead of repeating this in every element I would like to simplify this.
It might be that I need to 'grow up' and learn how to do this in an external PHP file? :rolleyes:

Currently I use ajax calc element to calculate value's straight after someone enters the numbers.
 
If this needs to happen real time then you'll need to use a JavaScript routine otherwise PHP if you just need it updated on form submit.

For the Javascript you can add it in in a couple of places, the javascript tab, form JS or within the Calc element itself which actually uses AJAX a combination of JavaScript and PHP.

I can't recommend what to use as it is not clear what you currently have and what your ultimate goal is. Maybe provide some more details and we can sort something out.

This is an old howto but using Javascript, (with mootools just to confuse things), to copy from one element to another is quite straight forward.

http://fabrikar.com/forums/index.ph...c-copy-value-from-one-field-to-another.12958/
 
It's pretty simple: client selects a product from a drop down and I am fetching productcode, product description, and product price from the DB with an ajax calc field.
Now I am using the same row/field lookup in every calc element. This seems a slow process. Ajax takes extra time and I love to do one (array) lookup and drop the results straight away in all elements. At the end I will need to present a report/overview showing selected product , code , price and totals. Since the lookups are not only simple DB lookups but also with a lot of conditions connected with other fields, it feels not good to copy paste 30 lines or more code into an element with hardly any other change than one field as the previous one.

Interesting link, will experiment with it but it is only a straight copy and my fields have different values.
 
Hmmm, I'm following you to a point but without fully understanding your setup it's hard to know what to suggest.

Do you think you could use something like Jing to show what you are doing now so we get a better understanding on what you are hoping to achieve?
 
OK let me entertain you :)

Simple example:

Screen Shot 2014-07-10 at 10.25.14.png

One dropdown, after selecting a choice it does a lookup for the code and price.

In the code calc is this:


PHP:
$model=(int) '{zuid_inv_aanvragen___dak_type_raw}';
$lengteID='{zuid_inv_aanvragen___Lengte_categorie}';
$type=(int)'{zuid_inv_aanvragen___Laadbak_type_raw}';
 
$db = JFactory::getDbo();
$query = $db->getQuery(true);
 
$query
    ->select('gilde_code')
    ->from('zuid_lst_dak')
    ->where('Naam = ' . $db->quote($model). ' AND lengteID = ' . $db->quote($lengteID). 'AND Wand_type = ' . $db->quote($type));
 
$db->setQuery($query);
 
try {
    $value = $db->loadResult();
} catch (Exception $e ) {
 
}
 
return $value;

This is only for getting the code (also related to some other input ($lengteID and $type)
Then in the price calc I use exactly the same code, only with selecting the price field instead of the gilde_code.

It feels I am needles abusing resources, with double lookups.
Why not do one array lookup and dump the values straight away in both elements.

Bastiaan
 
Ahh I think I know what you mean, yes you can do that.

What you need to do is run your query's on the PHP side and put the results into a json encoded array. Then on the JS side you decode the data and place all the various bits in their relevant places.

So as a bad coding example from and old server the user ajax PHP code could be something like:-

PHP:
function getsomething( )
    {
        $db        = JFactory::getDBO();
        $something_id = JRequest::getVar( "identify", "" );
        $query      = "SELECT thingsa FROM tablea WHERE something = '$something_id'";
        $db->setQuery( $query );
        $result = $db->loadResult();
       
        $query  = "SELECT id FROM tableb WHERE somethingelse = '$result'";
        $db->setQuery( $query );
       
        $listresult[ ] = $db->loadResult();
        $listresult[ ] = $result;
 
        //    echo '<pre>'; print_r($listresult); echo '</pre>';
        echo json_encode( $listresult );
    }


$listresult[] is an array and you can as many queries as you want and keep adding into this, the end results is a collection of all your results.

The results get json encoded and passed across to the JS side.

From the JS side you pick up the results with

HTML:
..
..
onComplete : function (r) {
                        var results = JSON.parse(r);

The variable results will then contain all your query data, you should then be able to access each result by using results[0], results[1] etc.

Let me know if that makes sense.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top