Jump to content

php country voting list


Ollifi

Recommended Posts

Hello,

 

I have countries listed in a file like this:

country name|votes
country name|votes
country name|votes

 

And example:

Finland|50
USA|500
Sweden|501

 

How could I display them as a list:

[*]Sweden ,501 votes

[*]USA ,500 votes

[*]Finland ,50 votes

 

Currently I have this code:

$file = file("suggested.txt");

$votes = array();

for($i=0;$i<count($file);$i++){
$exp = explode("|", $file[$i]);
$votecount = trim($exp[1]);
$maa = $exp[0];
$votes["$maa"] = $votecount;
}

sort($votes, SORT_NUMERIC);
$votes = array_reverse($votes);

for($i=0;$i<count($file);$i++){
print"".$country_list[$i]." ".$votes[$i]."<br>";
}

 

country_list has all world countries in an array.

 

thank you for helping!

 

 

P.S. what it should do: It should display countries from largest number to smallest number (biggest number->first, smallest number->last)

Link to comment
Share on other sites

try something like:

<?php
$file = file("suggested.txt");
$votes = array();

for($i=0;$i<count($file);$i++){
$exp = explode("|", $file[$i]);
$votecount = trim($exp[1]);
$votes[$exp[0]] = $votecount;
}

arsort($votes, SORT_NUMERIC);
foreach ($votes as $country=>$vote){
print $country." ".$vote."<br>";
}
?>

Link to comment
Share on other sites

and what method you would recommend to have country list doing like this:

 

Sweden ,501 votes

USA ,500 votes

Finland ,50 votes

 

So country with lot of votes would have bigger font size,and if small count of votes, it would have small font size.

Link to comment
Share on other sites

and,if I want to save vote to file, I need url something like vote.php?id=COUNTRY-ID

And COUNTRY-ID should be same than country is in the file. So it should not be same id in the table that has been ordered in number order. How could I get it?

Link to comment
Share on other sites

Hi

 

For the different sizes, using Webstyles code earlier you could use something like this:-

 

<?php
$file = file("suggested.txt");
$votes = array();

for($i=0;$i<count($file);$i++){
$exp = explode("|", $file[$i]);
$votecount = trim($exp[1]);
$votes[$exp[0]] = $votecount;
}

arsort($votes, SORT_NUMERIC);
$RecCnt = 0;
foreach ($votes as $country=>$vote)
{
print <span class='CountryList".(($RecCnt <= 3) ? $RecCnt : '')."' >$country." ".$vote."</span><br>";
$RecCnt++;
}
?> 

 

Then just have a few different styles to control it.

 

You could also possibly do it by restyling an unordered list (with list-style:none) and then styling subsequent  sub elements. Probably CSS wise the most flexible but probably the most difficult to understand by anyone except those who really understand CSS.

 

All the best

 

Keith

Link to comment
Share on other sites

 

@kickstart thank you for help!

 

Could you comment also this one:

and,if I want to save vote to file, I need url something like vote.php?id=COUNTRY-ID

And COUNTRY-ID should be same than country is in the file. So it should not be same id in the table that has been ordered in number order. How could I get it?

Link to comment
Share on other sites

Hi

 

For the different sizes, using Webstyles code earlier you could use something like this:-

 

<?php
$file = file("suggested.txt");
$votes = array();

for($i=0;$i<count($file);$i++){
$exp = explode("|", $file[$i]);
$votecount = trim($exp[1]);
$votes[$exp[0]] = $votecount;
}

arsort($votes, SORT_NUMERIC);
$RecCnt = 0;
foreach ($votes as $country=>$vote)
{
print <span class='CountryList".(($RecCnt <= 3) ? $RecCnt : '')."' >$country." ".$vote."</span><br>";
$RecCnt++;
}
?> 

 

Then just have a few different styles to control it.

 

You could also possibly do it by restyling an unordered list (with list-style:none) and then styling subsequent  sub elements. Probably CSS wise the most flexible but probably the most difficult to understand by anyone except those who really understand CSS.

 

All the best

 

Keith

now I tried it,

but it seems only change first 3 styles. But,I want it to be like this: http://coa.inducks.org/podium.php

Link to comment
Share on other sites

Hi

 

Not sure how you want to size them.

 

Easy enough to come up with various schemes, but need to know how you want to decide which get what sizing first.

 

I would agree with the comment to use standard names / abbreviations for countries. However you could use a lookup table (ie, array with one value as the key with the other as the value) to switch between them

 

Voting with a url like that seems like an invitation for someone to automate voting and make the votes meaningless.

 

All the best

 

Keith

Link to comment
Share on other sites

I want that size goes smaller between 10 (so 0-10 biggst, 10-20 smaller, 20-30 more smaller etc)

 

However you could use a lookup table (ie, array with one value as the key with the other as the value) to switch between them

How should I do this?

 

Voting with a url like that seems like an invitation for someone to automate voting and make the votes meaningless.

And,is there some other method to do this?

Link to comment
Share on other sites

Hi

 

Not sure still what you want to base the formatting on.

 

However a few rough ideas to show you.

 

First one, sum up all the votes and then use a style depending on how many votes a single country has got compared to the total votes cast. Using 0.9, 0.8, 0.65 and 0.4 for any that gets more than 90%, 80%, 65% or 40% of the votes cast (you would need to jiggle with these numbers to get useful ones).

 

<?php
$file = file("suggested.txt");
$votes = array();

for($i=0;$i<count($file);$i++){
$exp = explode("|", $file[$i]);
$votecount = trim($exp[1]);
$votes[$exp[0]] = $votecount;
}

arsort($votes, SORT_NUMERIC);
$TotCnt = array_sum($votes);
$Border = array();
$Border[0] = (int)$TotCnt * .9);
$Border[1] = (int)$TotCnt * .;
$Border[2] = (int)$TotCnt * .65);
$Border[3] = (int)$TotCnt * .4);
$Border[3] = 0;
foreach ($votes as $country=>$vote)
{
foreach($Border AS $key=>$value)
{
	if ($vote > $value)
	{
		print "<span class='CountryList$key' >$country $vote</span><br>";
		break;
	}
}
}
?>  

 

Or similar, but giving style sheets to the top 10%, then 20%, then 35% then 60% of the counties.

 

<?php
$file = file("suggested.txt");
$votes = array();

for($i=0;$i<count($file);$i++){
$exp = explode("|", $file[$i]);
$votecount = trim($exp[1]);
$votes[$exp[0]] = $votecount;
}

arsort($votes, SORT_NUMERIC);
$TotCnt = count($votes);
$Border = array();
$Border[0] = (int)$TotCnt * .9);
$Border[1] = (int)$TotCnt * .;
$Border[2] = (int)$TotCnt * .65);
$Border[3] = (int)$TotCnt * .4);
$Border[3] = 0;
foreach ($votes as $country=>$vote)
{
foreach($Border AS $key=>$value)
{
	if ($vote > $value)
	{
		print "<span class='CountryList$key' >$country $vote</span><br>";
		break;
	}
}
}
?>  

 

As to the lookup table, you have 2 lists of countries and need to match one to the other.

 

$Countries = array('gb'=>'Great Britain', 'us'=>'United States, etc);

 

The if you get the abbreviation you just look up $Countries['some abbreiation'] to get the other name

 

Do something to disguise the vote. The more you disguise it to harder it becomes to corrupt the results.

 

Ask for an email address, and send a confirmation email with some url with a random long string in it. Votes do not count until the url with that random string have been visited. And you block any email address used twice.

 

Encode something else in the url that can easily be checked. Maybe use a session, encrypt the session id into the URL and destroy the session after the vote has been cast. If the current session id encrypted doesn't equal the one on the URL then ignore the vote. You could also give the url parameter a random name stored in the session so if it doesn't match the parameter is ignored.

 

Put the time in a var in the URL and check it is within a certain time.

 

Nothing is totally proof, but as your current idea goes someone could just knock up something like the following:-

 

for($icnt=1;$icnt <=1000000;$icnt++)
{
file_get_contents('http://www.myurl.com/vote.php?id=COUNTRY-ID');
}

 

and vote a million times.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.