close ad
Databridge V2 with MySQLi support IS Now Available!
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Calculate average from form fields

Thread began 3/22/2019 8:31 pm by Jonas | Last modified 3/25/2019 12:03 pm by Ray Borduin | 28 views | 2 replies |

Jonas

Calculate average from form fields

I have a form that consists of rating in 8 different categories. Everything is in the same table, so no relationship. The fields are 'Kat1Rating', 'Kat2Rating' up to 8. Now I want to auto-generate an average rating from these categories, which should be displayed on the detail- and results-page.

On the Internet I found a solution that was to create a new query on the detail-page. This turned out to work perfectly, on the detail page. But since it's in a separate query, it doesn't work on the results-page. Even if I re-create the same query there, I get the same average on all records.

Unfortunately, it is a rather advanced query, and I do not know how to incorporate it in the original query created by DA. Unfortunately, Query builder does not seem to be able to help me here.

Here is the queries...

Average query on detail-page (this is working on the detail-page):

<?php
$rsAverage = new WA_MySQLi_RS("rsAverage",$matematikSverige,1000);
$rsAverage->setQuery("SELECT (CAST(AVG(`kat1Betyg`+`kat2Betyg`+`kat3Betyg`+`kat4Betyg`+`kat5Betyg`+`kat6Betyg`+`kat7Betyg`+`kat8Betyg`) /8 AS DECIMAL(10,1))) AS average FROM laromedel");
$rsAverage->execute();
?>

---
Query on results-page generated by DA:

<?php
$WADAlaromedel = new WA_MySQLi_RS("WADAlaromedel",$matematikSverige,1000);
$WADAlaromedel->setQuery("SELECT id, bild, laromedelsnamn, arskurser FROM laromedel");
$WADAlaromedel->execute();
?>

---

Query on detail-page generated by DA:

<?php
$WADAlaromedel_details = new WA_MySQLi_RS("WADAlaromedel_details",$matematikSverige,1);
$WADAlaromedel_details->setQuery("SELECT laromedel.id, laromedel.laromedelsnamn, laromedel.bild, laromedel.arskurser, laromedel.utvecklare, laromedel.pris, laromedel.adaptivt, laromedel.heltackande, laromedel.testad, laromedel.kat1Betyg, laromedel.kat1Kommentar, laromedel.kat2Betyg, laromedel.kat2Kommentar, laromedel.kat3Betyg, laromedel.kat3Kommentar, laromedel.kat4Betyg, laromedel.kat4Kommentar, laromedel.kat5Betyg, laromedel.kat5Kommentar, laromedel.kat6Betyg, laromedel.kat6Kommentar, laromedel.kat7Betyg, laromedel.kat7Kommentar, laromedel.kat8Betyg, laromedel.kat8Kommentar, former.`text` AS former_text, avsettfor.`text` AS avsettfor_text FROM laromedel LEFT OUTER JOIN former ON laromedel.form_id = former.id LEFT OUTER JOIN avsettfor ON laromedel.avsettfor_id = avsettfor.id WHERE laromedel.id = ?");
$WADAlaromedel_details->bindParam("i", "".($_GET['id']) ."", "-1"); //WAQB_Param1
$WADAlaromedel_details->execute();
?>

Maybe I'm overworking. Is there any easier way? Grateful for help.

Sign in to reply to this post

Ray BorduinWebAssist

You can just add it to the results page query:



$WADAlaromedel_details->setQuery("SELECT (CAST(AVG(`kat1Betyg`+`kat2Betyg`+`kat3Betyg`+`kat4Betyg`+`kat5Betyg`+`kat6Betyg`+`kat7Betyg`+`kat8Betyg`) /8 AS DECIMAL(10,1))) AS average, laromedel.id, laromedel.laromedelsnamn, laromedel.bild, laromedel.arskurser, laromedel.utvecklare, laromedel.pris, laromedel.adaptivt, laromedel.heltackande, laromedel.testad, laromedel.kat1Betyg, laromedel.kat1Kommentar, laromedel.kat2Betyg, laromedel.kat2Kommentar, laromedel.kat3Betyg, laromedel.kat3Kommentar, laromedel.kat4Betyg, laromedel.kat4Kommentar, laromedel.kat5Betyg, laromedel.kat5Kommentar, laromedel.kat6Betyg, laromedel.kat6Kommentar, laromedel.kat7Betyg, laromedel.kat7Kommentar, laromedel.kat8Betyg, laromedel.kat8Kommentar, former.`text` AS former_text, avsettfor.`text` AS avsettfor_text FROM laromedel LEFT OUTER JOIN former ON laromedel.form_id = former.id LEFT OUTER JOIN avsettfor ON laromedel.avsettfor_id = avsettfor.id WHERE laromedel.id = ?");

Sign in to reply to this post

Jonas

Thank you Ray for your reply, but unfortunately your proposal does not work. I get an SQL error.

But now it seems that I found the solution on the results page where the problem was:

SELECT (CAST(AVG(`kat1Betyg`+`kat2Betyg`+`kat3Betyg`+`kat4Betyg`+`kat5Betyg`+`kat6Betyg`+`kat7Betyg`+`kat8Betyg`) /8 AS DECIMAL(10,1))) AS average, `id`,`bild`,`laromedelsnamn`,`arskurser` FROM laromedel GROUP BY `id`

...and on the details page, I managed to group the two querys to one:

SELECT(CAST(AVG(laromedel.kat1Betyg + laromedel.kat2Betyg + laromedel.kat3Betyg + laromedel.kat4Betyg + laromedel.kat5Betyg + laromedel.kat6Betyg + laromedel.kat7Betyg + laromedel.kat8Betyg) /8 AS DECIMAL(10,1))) AS average, laromedel.id, laromedel.laromedelsnamn, laromedel.bild, laromedel.arskurser, laromedel.utvecklare, laromedel.pris, laromedel.adaptivt, laromedel.heltackande, laromedel.testad, laromedel.kat1Betyg, laromedel.kat1Kommentar, laromedel.kat2Betyg, laromedel.kat2Kommentar, laromedel.kat3Betyg, laromedel.kat3Kommentar, laromedel.kat4Betyg, laromedel.kat4Kommentar, laromedel.kat5Betyg, laromedel.kat5Kommentar, laromedel.kat6Betyg, laromedel.kat6Kommentar, laromedel.kat7Betyg, laromedel.kat7Kommentar, laromedel.kat8Betyg, laromedel.kat8Kommentar, former.`text` AS former_text, avsettfor.`text` AS avsettfor_text FROM laromedel LEFT OUTER JOIN former ON laromedel.form_id = former.id LEFT OUTER JOIN avsettfor ON laromedel.avsettfor_id = avsettfor.id WHERE laromedel.id = ?

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...