How do I make a category for each unique DB entry? (MySQL/PHP)
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开发者_StackOverflow社区 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!
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.
精彩评论