Menu
News
All News
Dungeons & Dragons
Level Up: Advanced 5th Edition
Pathfinder
Starfinder
Warhammer
2d20 System
Year Zero Engine
Industry News
Reviews
Dragon Reflections
White Dwarf Reflections
Columns
Weekly Digests
Weekly News Digest
Freebies, Sales & Bundles
RPG Print News
RPG Crowdfunding News
Game Content
ENterplanetary DimENsions
Mythological Figures
Opinion
Worlds of Design
Peregrine's Nest
RPG Evolution
Other Columns
From the Freelancing Frontline
Monster ENcyclopedia
WotC/TSR Alumni Look Back
4 Hours w/RSD (Ryan Dancey)
The Road to 3E (Jonathan Tweet)
Greenwood's Realms (Ed Greenwood)
Drawmij's TSR (Jim Ward)
Community
Forums & Topics
Forum List
Latest Posts
Forum list
*Dungeons & Dragons
Level Up: Advanced 5th Edition
D&D Older Editions, OSR, & D&D Variants
*TTRPGs General
*Pathfinder & Starfinder
EN Publishing
*Geek Talk & Media
Search forums
Chat/Discord
Resources
Wiki
Pages
Latest activity
Media
New media
New comments
Search media
Downloads
Latest reviews
Search resources
EN Publishing
Store
EN5ider
Adventures in ZEITGEIST
Awfully Cheerful Engine
What's OLD is NEW
Judge Dredd & The Worlds Of 2000AD
War of the Burning Sky
Level Up: Advanced 5E
Events & Releases
Upcoming Events
Private Events
Featured Events
Socials!
EN Publishing
Twitter
BlueSky
Facebook
Instagram
EN World
BlueSky
YouTube
Facebook
Twitter
Twitch
Podcast
Features
Top 5 RPGs Compiled Charts 2004-Present
Adventure Game Industry Market Research Summary (RPGs) V1.0
Ryan Dancey: Acquiring TSR
Q&A With Gary Gygax
D&D Rules FAQs
TSR, WotC, & Paizo: A Comparative History
D&D Pronunciation Guide
Million Dollar TTRPG Kickstarters
Tabletop RPG Podcast Hall of Fame
Eric Noah's Unofficial D&D 3rd Edition News
D&D in the Mainstream
D&D & RPG History
About Morrus
Log in
Register
What's new
Search
Search
Search titles only
By:
Forums & Topics
Forum List
Latest Posts
Forum list
*Dungeons & Dragons
Level Up: Advanced 5th Edition
D&D Older Editions, OSR, & D&D Variants
*TTRPGs General
*Pathfinder & Starfinder
EN Publishing
*Geek Talk & Media
Search forums
Chat/Discord
Menu
Log in
Register
Install the app
Install
Upgrade your account to a Community Supporter account and remove most of the site ads.
Enchanted Trinkets Complete--a hardcover book containing over 500 magic items for your D&D games!
Community
General Tabletop Discussion
*TTRPGs General
Monte on Origins awards and ENnies
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Michael Morris" data-source="post: 2408191" data-attributes="member: 87"><p>For those capacle of reading it - here's the (tenatively) final code that does the counting.</p><p></p><p>[php]function calculate_winner($category)</p><p>{</p><p> global $DB_site;</p><p></p><p> // Initialize internal arrays</p><p> $votes = array();</p><p> $drop_list = array(0 => 0);</p><p></p><p> // Get the votes </p><p> $getvotes = $DB_site->query("SELECT $category FROM ennies");</p><p></p><p> // Build the votes array</p><p> while ($getvote = $DB_site->fetch_array($getvotes))</p><p> {</p><p> $votes[] = $getvote[$category];</p><p> }</p><p></p><p></p><p> for ($x = 1; $x < 5; $x++)</p><p> {</p><p> // Initialize $voted_for array</p><p> $voted_for = array(</p><p> 1 => 0,</p><p> 2 => 0,</p><p> 3 => 0,</p><p> 4 => 0,</p><p> 5 => 0</p><p> );</p><p> </p><p> // Now unset members of $voted_for on the drop list </p><p> foreach ($drop_list as $drop)</p><p> {</p><p> unset($voted_for[$drop]);</p><p> }</p><p> </p><p> // Iterate over the ballots to count votes</p><p> foreach ($votes as $ballot)</p><p> {</p><p> // Break the tally string up into an array.</p><p> $tally = split_str($ballot);</p><p></p><p> // Reverse the array because the ballots are stored backwards.</p><p> $tally = array_reverse($tally);</p><p> </p><p> // Flip the array to align the keys with the drop values. This also destroys unwanted duplicate entries.</p><p> $tally = array_flip($tally);</p><p></p><p> // Drop out votes for candidates that have been elminated in prior voting rounds. </p><p> foreach ($drop_list as $drop)</p><p> {</p><p> unset($tally[$drop]);</p><p> }</p><p></p><p> // Flip the array back to put the values back where they belong.</p><p> $tally = array_flip($tally);</p><p></p><p> // Now count the ballot on the first element IF any elements still exist. </p><p> if (count($tally))</p><p> {</p><p> $voted_for[reset($tally)]++; // Increment the element of $voted for corresponding</p><p> } // to the first element of tally </p><p> }</p><p></p><p> </p><p> //Sort by value, retaining keys, arrange in reverse order.</p><p> arsort($voted_for);</p><p> </p><p> if (count($voted_for) == 2)</p><p> { </p><p> $finalists = array_flip($voted_for);</p><p> </p><p> if (count($finalists) == 2) // If it drops to 1 there is a tie.</p><p> { </p><p> $winners[0] = reset($finalists);</p><p> $winners[1] = end($finalists);</p><p> }</p><p> else</p><p> {</p><p> $winners[0] = reset($voted_for);</p><p> $winners[1] = end($voted_for);</p><p> $winners[2] = "tie";</p><p> }</p><p></p><p> // Return the result for this category back to the main ennies program. Note that this function gets called multiple times - once for each category. </p><p> return $winners;</p><p> }</p><p> else</p><p> {</p><p> //Find all the keys of the array.</p><p> $voted_keys = array_keys($voted_for);</p><p></p><p> // Pop the last element into the drop list </p><p> $drop_list[] = array_pop($voted_keys);</p><p></p><p> }</p><p> }</p><p>}[/php]</p><p></p><p>As you can see, once the system has narrowed things down to 2 finalists it sorts them out and returns them. If they are tied it is set to return two gold winners but not return a silver - we're still debating what to do in that situation, one that is in any event unlikely to occur anyway.</p><p></p><p>EDIT: Added additional comment text (in orange) to clarify the flow for laymen.</p></blockquote><p></p>
[QUOTE="Michael Morris, post: 2408191, member: 87"] For those capacle of reading it - here's the (tenatively) final code that does the counting. [php]function calculate_winner($category) { global $DB_site; // Initialize internal arrays $votes = array(); $drop_list = array(0 => 0); // Get the votes $getvotes = $DB_site->query("SELECT $category FROM ennies"); // Build the votes array while ($getvote = $DB_site->fetch_array($getvotes)) { $votes[] = $getvote[$category]; } for ($x = 1; $x < 5; $x++) { // Initialize $voted_for array $voted_for = array( 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0 ); // Now unset members of $voted_for on the drop list foreach ($drop_list as $drop) { unset($voted_for[$drop]); } // Iterate over the ballots to count votes foreach ($votes as $ballot) { // Break the tally string up into an array. $tally = split_str($ballot); // Reverse the array because the ballots are stored backwards. $tally = array_reverse($tally); // Flip the array to align the keys with the drop values. This also destroys unwanted duplicate entries. $tally = array_flip($tally); // Drop out votes for candidates that have been elminated in prior voting rounds. foreach ($drop_list as $drop) { unset($tally[$drop]); } // Flip the array back to put the values back where they belong. $tally = array_flip($tally); // Now count the ballot on the first element IF any elements still exist. if (count($tally)) { $voted_for[reset($tally)]++; // Increment the element of $voted for corresponding } // to the first element of tally } //Sort by value, retaining keys, arrange in reverse order. arsort($voted_for); if (count($voted_for) == 2) { $finalists = array_flip($voted_for); if (count($finalists) == 2) // If it drops to 1 there is a tie. { $winners[0] = reset($finalists); $winners[1] = end($finalists); } else { $winners[0] = reset($voted_for); $winners[1] = end($voted_for); $winners[2] = "tie"; } // Return the result for this category back to the main ennies program. Note that this function gets called multiple times - once for each category. return $winners; } else { //Find all the keys of the array. $voted_keys = array_keys($voted_for); // Pop the last element into the drop list $drop_list[] = array_pop($voted_keys); } } }[/php] As you can see, once the system has narrowed things down to 2 finalists it sorts them out and returns them. If they are tied it is set to return two gold winners but not return a silver - we're still debating what to do in that situation, one that is in any event unlikely to occur anyway. EDIT: Added additional comment text (in orange) to clarify the flow for laymen. [/QUOTE]
Insert quotes…
Verification
Post reply
Community
General Tabletop Discussion
*TTRPGs General
Monte on Origins awards and ENnies
Top