0

I have a template that outputs a csv, php is set to run on input to allow headers to be set.

For each row I need to calculate the result of 2 fields multiplied and output this as a field to the csv.

Are the field values available in PHP?

Or is there another way to mulitply 2 fields in an entries loop?

<?php
$csv_ids = $this->EE->input->post('csv_id');

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=stock_".date("d_m_Y_H_i").".csv");
header("Pragma: no-cache");
header("Expires: 0");
?>
CODE,field1, field2
{exp:channel:entries channel="stock" limit="10000" entry_id="<?=$csv_ids?>"}
{code},{field1},{field2},{field5}
{/exp:channel:entries}

I need 'field5' to be the result of multiplying 'field3' by 'field4'.

3

You can use MX Calculator for any calculation.

But possible you can use query module instead of channel:entries (if you have some simple fieldtype which keep they data regular way). In this case you can do calculation in query:

{exp:query sql="SELECT field_id_15 AS code, field_id_2 AS field2, field_id_1 AS field1, (field_id_3 * field_id_4) AS field5 FROM exp_channel_data WHERE channel_id = 1 AND entry_id IN (<?=$csv_ids?>) LIMIT 10000"}
    {code},{field1},{field2},{field5}
{/exp:query}
3
  • I had hoped that it could have been done without extra plugins, but this one is pretty minimal, thank you.
    – wot
    Jan 15 '15 at 10:42
  • EE have math operations for using in conditionals only so far.But if what you show is just some regular fields, you can use default {exp:query} module and do this calculation directly in sql query. It also will be better by performance then channel:entries if you have many entries. I add example into my answer.
    – Max Lazar
    Jan 15 '15 at 11:05
  • Yes, of course, I hadn't thought of doing it directly in SQL, thank you.
    – wot
    Jan 15 '15 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.