0

I have a contest running, and to look at the results I've made this short code:

    date_default_timezone_set('America/New_York');
    echo "Results tallied at ". date("g:i:s A")."<br /><br />";

    $names = array('contestant1','contestant2','contestant3'...); //10 entries
    $plots = array();

    foreach ($names as $name) {
            $query = $this->db->getwhere('friday', array('votedon'=>$name));//CodeIgniter Active Record
            $count = $query->num_rows(); //again, CodeIgniter
            echo "Votes for " . ucfirst($name).": $count <br />";
            $plots[]= $count;
        }
        $total = $this->db->count_all('friday'); //CodeIgniter
        echo "<br />Total votes: $total <br />";
        $highest = 5+max($plots); //make the chart look better
        $url = "http://chart.apis.google.com/chart?cht=bvg&chs=900x300&chbh=40,100,40";
        $par1 = "&chds=0,$highest";
        $par2 = "&chl=". implode("|",$names);
        $par3 = '&chd=t:'. implode(",",$plots);

        $imgsrc = $url.$par1.$par2.$par3;
        echo "<img src='$imgsrc' />";

So this generates a bar graph showing the current polling results. What I'm wondering was if there is an elegant way to avoid having to enter each unique entry that people could have voted for. (Line #4 $names = ....) For this contest there are ten possible entry names in the "votedon" column, but what if it were an user-input instead, and I wanted to show/tally all possible results.

What would code like that look like? I'm newer to programming, and idioms like this are sometimes hard for me to come up with on my own. Thanks!

1 Answer 1

1

The query is one that is pretty simple:

SELECT votedon, count(*) 
FROM VOTING_TABLE
GROUP BY votedon

Now, I don't know how that fits into codeigniter, but a mysql connection from php is not that hard, and if this is really sort of an ad hoc thing, this will do.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.