[SOLVED] math calculation of fields not working in category view

More
8 years 6 months ago - 8 years 6 months ago #57370 by bobthebob01
Hi,

I have a content type with many fields to build a table to add value for each day of the month in order to build a table report.
The fields are text fields to enter numbers of product done for each day and called: Stage1Day1, Stage1Day2, Stage1Day3 ....
I also have one text field where a total is entered and called: TotalQuantity

Since it's a production report. I need to have a balance and a percentage of completion.

balance = total - (sum of all fields)
completion = ((sum of all fields) / TotalQuantity) * 100
And i display the completion using round($completion) to have round number used to create a progress bar.

I'm able to "echo" all these fields in my template and also make the calculation in item view. Everything is working: the balance is correct and my progress bar as well.

But I have errors when in category view.
I placed the fields in renderonly. And in my template I use
Code:
<?php echo $item->fields['Stage1Day1']->display; ?>
to display the fields. All good up to now.

But when I do my caculation to get the balance and progress i get "Undefined variable:" and "Trying to get property of non-object". And no calculation is done.

This is what i use to make the calculation. Which is the same concept that I use in item view.
Code:
$mytotal = $item->fields['Stage1Day1']->display + $item->fields['Stage1Day2']->display + $item->fields['Stage1Day3']->display + $item->fields['Stage1Day4']->display + $item->fields['Stage1Day5']->display + $item->fields['Stage1Day6']->display + $item->fields['Stage1Day7']->display + $item->fields['Stage1Day8']->display + $item->fields['Stage1Day9']->display + $item->fields['Stage1Day10']->display + $item->fields['Stage1Day11']->display + $item->fields['Stage1Day12']->display + $item->fields['Stage1Day13']->display + $item->fields['Stage1Day14']->display + $item->fields['Stage1Day15']->display + $item->fields['Stage1Day16']->display + $item->fields['Stage1Day17']->display + $item->fields['Stage1Day18']->display + $item->fields['Stage1Day19']->display + $item->fields['Stage1Day20']->display + $item->fields['Stage1Day21']->display + $item->fields['Stage1Day22']->display + $item->fields['Stage1Day23']->display + $item->fields['Stage1Day24']->display + $item->fields['Stage1Day25']->display + $item->fields['Stage1Day26']->display + $item->fields['Stage1Day27']->display + $item->fields['Stage1Day28']->display + $item->fields['Stage1Day29']->display + $item->fields['Stage1Day30']->display + $item->fields['Stage1Day31']->display;


I even tried to first set them as variable like so:
Code:
$Stage1Day1 = $item->fields['Stage1Day1']->display; $Stage1Day2 = $item->fields['Stage1Day2']->display; ....

and then add the variable like to:
Code:
$mytotal = $Stage1Day1 + $Stage1Day2 + $Stage1Day3 ....


Does anyone have an idea why it's not working.

I wonder if it has nothing to do with the one of the fix showing on github for FC v3.0.9-dev2: "Fixed fieldgroup field mixing field values in category view when custom HTML parameter is used".

I should add that I have other sets of similar fields for different stages. But of course, they have a different field name.

thank you in advance.
Last edit: 8 years 6 months ago by ggppdk.

Please Log in or Create an account to join the conversation.

More
8 years 6 months ago #57372 by micker
i think $Stage1Day1 is a string not a number ... maybe (int) $Stage1Day1 but not sure

FLEXIcontent is Free but involves a very big effort on our part.
Like the our support? (for a bug-free FC, despite being huge extension) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing reviews. Thanks![/size]

Please Log in or Create an account to join the conversation.

More
8 years 6 months ago - 8 years 6 months ago #57375 by ggppdk
Hello

Please avoid using ->display for calculation !!
- this may contain HTML !
- it exists only if field is in a template positions

instead use raw values, which contain the real value and exist always:
Code:
$field_id = 19; $fvals = $item->fieldvalues[$field_id]

See more here:
Using the raw value of a field inside a template file or inside a new custom field type


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...
Last edit: 8 years 6 months ago by ggppdk.

Please Log in or Create an account to join the conversation.

More
8 years 6 months ago #57385 by bobthebob01
Thank you micker and ggppdk for your help. It's always extremely appreciated.

I think my issue was coming from the fact that I had put my php code outside the foreach statement in category_items_html5.php. It was actually at the very top.
Although I set a mask to these fields to be integer and cleanup all HTML, so no html can be added by user; I will still try ggppdk' suggestion using to see.
It's always good to use the best practice, especially when recommended by the developer(s).
It's now working.

Thanks again a lot. This CCK and its team are number uno !!!.

Cheers

PS: It's really incredible the different type of site one can do with FC. It really opens doors to a lot of application, Next step, json output to use FC with a mobile app. It's gonna be even more amazing.

Please Log in or Create an account to join the conversation.

More
8 years 6 months ago #57394 by micker
the json output already exist not by flexicontent team but you can use it
github.com/Lyquix/flexicontent_templates

FLEXIcontent is Free but involves a very big effort on our part.
Like the our support? (for a bug-free FC, despite being huge extension) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing reviews. Thanks![/size]

Please Log in or Create an account to join the conversation.

More
8 years 6 months ago #57410 by ggppdk
Hello

- you can create add to the URL
&format=json

and to use a different template (=different fields):

&clayout=TEMPLATENAME

Note to use a different template via URL, the template must be allowed in "allowed templates" parameter of the component or of category


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

Moderators: vistamediajoomlacornerggppdk
Time to create page: 0.287 seconds
Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Display
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline