ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Obtaining various values for in-template maths

July 25, 2012 12:22pm

Subscribe [3]
  • #1 / Jul 25, 2012 12:22pm

    Rick Lecoat

    128 posts

    Scenario: I’m setting up a feedback system. Customers will submit feedback on the performance of any one of half a dozen companies; in each case the company will have been providing one of 3 or 4 specific and pre-determined services. In addition to being able to write their own notes on the company’s performance, the customer will specify the feedback as being Positive, Neutral or Negative. So, an example might be: Feedback for Company A performing Service X: Positive.

    I am currently expecting to use Playa to handle the relationships between company, service, quality of feedback etc. This is not yet implemented (I’m still in the planning stages).

    Now: I would like to be able to perform some in-template maths calculations on the feedback results. Eg.:
    “What percentage of Company A’s total feedback for providing Service X is Positive?”
    To do this I would need my EE template to be able to add together the total number of feedbacks submitted for Company A performing Service X, obtain the number of positive feedback for Company A performing Service X, divide the latter by the former, and multiply by 100. This should give me the correct percentage which I could use to, for example, represent that data as a graph (with a bit of css that I haven’t quite worked out yet).

    My question: does EE (or EE + Playa) give me in-template access to the values that I need?

    Is this a really confusing post? I can’t tell any more.

  • #2 / Jul 25, 2012 1:09pm

    Boyink!

    5011 posts

    EE (and Playa as well that I know) simply store and spit back content.  What you are after is transformation/calculations that you’ll need to look to either a custom query, add-on, or PHP to do.

  • #3 / Jul 25, 2012 1:43pm

    Rick Lecoat

    128 posts

    Thanks for that Mike. I’ve not tried carrying out calculations via an EE template before and wasn’t even sure if it could be done. I guess the key thing is being able to extract the different values from the database. Provided that can be done then I suppose it’s over to PHP (which is a mystery to me, but I could no doubt find a PHP dev to help out) or perhaps Javascript. Hmm.

    The process that I sort of had in mind when I was thinking about this was to a) extract the various data and store them as variables, b) carry out the various calculations using those variables and store the result as a new EE variable, then c) use THAT variable to set the width of an html element. I figure I could use this to draw a simple bar chart showing the relative percentages of Positive, Neutral and Negative feedback. Maybe I’m way off base with my whole approach.

    Looks like I need to do some reading up on Custom queries.
    Cheers

  • #4 / Jul 25, 2012 1:44pm

    Boyink!

    5011 posts

    Or search Devot-ee?  😉

  • #5 / Jul 25, 2012 1:56pm

    Rick Lecoat

    128 posts

    Yes, good point. This project is quite a bit further into the EE deep end than anything I’ve previously attempted—lots of channels, numerous category groups, events, calendars, fairly complicated relationships between channels… as a result half the battle is just working out what online information I need to be searching for!  I appreciate your help, thanks.

  • #6 / Jul 26, 2012 3:20pm

    Robin Sowell

    13255 posts

    Interesting.  And a lot of different ways you could go at it.  I’d lean toward custom module for the whole thing, assuming the data collection happens frontend.  I don’t see the need to have it in playa, which kind of complicates it a bit.  You could riff off of Solspace’s Freeform- use that to collect the data and they have plenty of hooks if you need to do anything odd.  Which I’m not really seeing you do on entry- but if you wanted a separate table to hold calculated totals when entries are created, then yep, hooks for that.  Hrm… though not at all sure you can edit freeform entries.  I guess that would be a point in favor of using playa.  But playa/relationships are going to be more complicated to deal with overall.

    And then yep- I’d do a module/plug to handle the output.

    Not a small project, but definitely doable, and in a lot of different ways.

    I’m going to shift to ‘General Discussion’ because you may get some more brainstorming there.  Sounds like it should be a nice one when you’re done!

  • #7 / Jul 29, 2012 3:17pm

    Rick Lecoat

    128 posts

    Thanks for the suggestions Robin. You did lose me slightly with your involvement of Freeform; it made me wonder whether I was simply not understanding the logic of your process, or whether I had not described the scenario well enough and thus caused you to misunderstand it. Either way, it’s my bad.  But it sounded like you are thinking that I want data values for the bar chart to be entered by the visitor when they visit the page, hence your Freeform suggestion(?). That’s not exactly what I meant.

    The relevant values are in fact the ‘counts’ of a given Channel’s entries under various limiting factors. Eg.: All the entries in a channel named ‘Feedback’, all the entries in the channel ‘Feedback’ limited by the category ‘Positive’, all the entries in the channel ‘Feedback’ limited by the category ‘Negative’, etc.
    Looking through the Channel Entry tag’s variables, I am thinking that this could be accomplished by using {absolute_count} and/or {absolute_results}. Using one Channel Entries tag (with appropriate result-limiting conditions) per data value required would get the ‘count’ numbers out of the EE database; then it’s ‘just’ a matter of performing the calculation. And of passing the result of the calculation to the width parameter of an HTML element.

    Perhaps the Channel Entry tags can appear right at the end of the template (and hidden off screen by CSS), then a javascript could locate the required ‘count’ values from the output of those CE tags, do the maths, and then delete the count values from the page (cleaning up after itself) before passing the final value to the HTML of the bar chart.

    Am I talking out of my behind here? Or could this work?

    Your custom module idea sounds like a neater solution in the long run, but I have no idea how to go about that (I have zilch php skills) and I’m pretty sure that even my feeble javascript programming is up to the task outlined above. Of course it all assumes that I’m not making some rookie EE error in my proposed CE tag use. What do you think?

    BTW, one level of relationship complexity has recently been removed from the project by the client, so I’m currently looking to see if I can do it all just with channels and categories, taking Playa out of the mix.

    Thanks, as always, for any suggestions or advice – always gratefully and humbly received.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases