Ollifi Posted August 8, 2011 Share Posted August 8, 2011 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) Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 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>"; } ?> Quote Link to comment Share on other sites More sharing options...
kickstart Posted August 8, 2011 Share Posted August 8, 2011 Hi Agree with the above. Sort destroys the key / value association which asort keeps. arsort does a sort in reverse order to save you having to reverse the array afterwards. All the best Keith Quote Link to comment Share on other sites More sharing options...
Ollifi Posted August 8, 2011 Author Share Posted August 8, 2011 thank you both,it worked very well =) Quote Link to comment Share on other sites More sharing options...
Ollifi Posted August 8, 2011 Author Share Posted August 8, 2011 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. Quote Link to comment Share on other sites More sharing options...
Ollifi Posted August 8, 2011 Author Share Posted August 8, 2011 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? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 8, 2011 Share Posted August 8, 2011 There are standards for all country ids/codes, you should use that. There are also databases available with all country names and ids, that you can download. You'll find them on google. Quote Link to comment Share on other sites More sharing options...
Ollifi Posted August 8, 2011 Author Share Posted August 8, 2011 yes,but I´m currently using number based system. so I would like to continue using them. So, could someone help with it? Quote Link to comment Share on other sites More sharing options...
kickstart Posted August 8, 2011 Share Posted August 8, 2011 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 Quote Link to comment Share on other sites More sharing options...
Ollifi Posted August 8, 2011 Author Share Posted August 8, 2011 @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? Quote Link to comment Share on other sites More sharing options...
Ollifi Posted August 8, 2011 Author Share Posted August 8, 2011 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 Quote Link to comment Share on other sites More sharing options...
kickstart Posted August 8, 2011 Share Posted August 8, 2011 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 Quote Link to comment Share on other sites More sharing options...
Ollifi Posted August 8, 2011 Author Share Posted August 8, 2011 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? Quote Link to comment Share on other sites More sharing options...
kickstart Posted August 8, 2011 Share Posted August 8, 2011 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 Quote Link to comment Share on other sites More sharing options...
Ollifi Posted August 8, 2011 Author Share Posted August 8, 2011 Hello, Thank you for help, I will look up your ideas! Thank you very much!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.