Field Maths 101
Posted: 11 October 2006 11:30 AM   [ Ignore ]  
Moderator
Avatar
RankRankRankRankRankRank
Total Posts:  5202
Joined  03-22-2004

Playing with extensions at the moment as the opportunity to create one arised. I have a few questions.

The extension is called Field Maths
It came around because I was using cutom fields for a simple ratings system. I had 1-6 custom fields with a dropdown list of 1-10 rating
I was then adding them all and dividing by their sum to get an overall rating using php in the template. This worked fine but ti was difficult to pass around that overall result to various other templates. So I thought why not create an extra custom field and let an extension fill in the field.

So the plan was to
- Enable our extension
- Goto your settings
- Fill in field ids to be used
- Fill in overall results field id
- Choose your sum. There could be a few like just multiplication or the rating sum mentioned above etc.
- Then on your publish page choose your values from the dropdown fields for each value
- Do a quick save and it would update your overall field
- Click save and now you have your overall value in your database.

I have created all the extension framework except for the settings and logic. Do you think this is a good idea/ ANy tips on getting values from custom fields, creating the objects etc.Any help would be great thanks.

 Signature 
Profile
 
 
Posted: 11 October 2006 01:38 PM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  759
Joined  08-16-2003

I don’t know that an extension is how I would choose to go about it. Not that there’s anything wrong with an extension, I just like plugins for no particular reason. Let’s call our hypothetical plugin AverageThis:

{exp:weblog:entries weblog="whatever"}
  {exp
:averagethis}
    {averagethis
:item}{custom_field_1}{/averagethis:item}
    {averagethis
:item}{custom_field_2}{/averagethis:item}
    {averagethis
:item}{custom_field_3}{/averagethis:item}
    {averagethis
:item}{custom_field_4}{/averagethis:item}
    {averagethis
:item}{custom_field_5}{/averagethis:item}
    {averagethis
:item}{custom_field_6}{/averagethis:item}
  {
/exp:averagethis}
{
/exp:weblog:entries}

--or--

{exp:weblog:entries weblog="whatever"}
  {exp
:averagethis values="{custom_field_1}|{custom_field_2}|{custom_field_3}|{custom_field_4}|{custom_field_5}|{custom_field_6}"}
{
/exp:weblog:entries}

Then just fetch the values, do a little math, and spit out the answer.

Profile
 
 
Posted: 11 October 2006 01:54 PM   [ Ignore ]   [ # 2 ]  
Moderator
Avatar
RankRankRankRankRankRank
Total Posts:  5202
Joined  03-22-2004

Yeah thought about the plugin as I already has it working like this in the template with php but it only gives a result on the fly. I wanted something addied to the database that I could pull out and use all over a site and relate directly to that story. Also makes it easier when embedding templates. I thought it would also be easier that you could pick the different maths sum you wish from the settings.

 Signature 
Profile
 
 
Posted: 11 October 2006 02:09 PM   [ Ignore ]   [ # 3 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  759
Joined  08-16-2003

Makes sense.

Another way to get the value wherever you need it is to use a query and let MySQL do the math for you. I’m not saying that’s a better solution, mind you, just another one. It all depends on your specific needs.

Profile
 
 
   
 
 
‹‹ Invisible CAPTCHA      Edit page hooks ››
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1743, on December 02, 2009 03:47 PM
Total Registered Members: 120379 Total Logged-in Users: 43
Total Topics: 126498 Total Anonymous Users: 21
Total Replies: 665223 Total Guests: 293
Total Posts: 791721    
Members ( View Memberlist )