So I’ve been asked to create a voting system where members vote for either A, B or C.
I’ve got it all setup and working - but they have now asked if they can see the progress of who’s ‘winning’ (A,B or C).
So I was wondering if it’s possible to create a live graph from the form submissions to show the progess of the voting.
The submissions are being saved in the database so there must be a way to do this, but I’m not very good with PHp so not sure where to start.
So i’m using the hook ‘FormItSaveForm’ to save all the submissions.
To be honest, I’m not worried too much about the display - it could just show a simple number of votes for each option.
So as simple as:
A = 20 votes
B = 30 votes
C = 40 votes
A graph would be nice and fancy, but it’s not really necessary I guess!
Yes, this is inefficient. And you also have to run the snippet uncached as the data is always changing.
If you really have “few thousand submissions”, then it’s probably a better idea to save the data in a custom database table. Or adding another hook-snippet, that just keeps track of the total values, increments them and saves them somewhere.
In the hook snippet saveVote update the total value in the database.
<?php
$vote = $hook->getValue('vote'); //read value of the 'vote' form field
$stmt = $modx->prepare("UPDATE `modx_tm2000_vote` SET `total` = `total` + 1 WHERE `option` = ?");
if ($stmt->execute([$vote]) && $stmt->rowCount() == 1){
return true;
} else {
return false;
}
Show the totals with the getVotes snippet.
<?php
$stmt = $modx->prepare("SELECT * FROM `modx_tm2000_vote` ORDER BY `option` ASC");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$output = [];
foreach ($rows as $row) {
$output[] = $row['option'] . '=' . $row['total'] . " votes";
}
return implode('<br>', $output);
This is of course just one possible solution.
You could also insert a new row for every vote into the database table, and then use COUNT() and GROUP BY in the getVotes snippet.
Or create a proper model for the custom database table, so that you can use xPDO.
Or store the totals in a file instead of a database table, etc.
Crikey Harry - that’s amazing. I obvisouly haven’t gone through it yet, but just wanted to quickly thank you for taking the time out to do this. It’s very much appreciated.
I’ll have a go at implimenting this and let you know how it works.
OK hat’s off to Harry here. That worked perfectly.
I’ve moved the resutls to a private page, so that users can’t see the votes, and I’ve also added another custom hook to prevent people from voting over and over again - and it’s perfect - thanks Harry so much - I’ve learnt a lot on this one!
Just a suggestion, but you could show the vote progress to the users as well on (successful) submit if you wanted to, without having a private page, by adding the getVote snippet into the successMessage.
This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.