Advertisement
Valleri

Most Frequent Tag

Aug 14th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <?php
  3.  
  4. class Map {
  5.     var $tagName;
  6.     var $count;
  7.  
  8.     function Map($tag, $count) {
  9.         $this->tagName = $tag;
  10.         $this->count = $count;
  11.     }
  12.  
  13.     function getTagName () {
  14.         return $this->tagName;
  15.     }
  16.     function getCount() {
  17.         return $this->count;
  18.     }
  19. }
  20. function isInArray($newMap, $map) {
  21.     foreach ($map as $item) {
  22.         $var1 = $newMap->tagName;
  23.         $var2 = $newMap->count;
  24.         if($item->tagName == $newMap->tagName) {
  25.             return true;
  26.         }
  27.     }
  28.     return false;
  29. }
  30. function FindAndIncrease($key, $map) {
  31.     foreach ($map as $item) {
  32.         if($item->tagName == $key) {
  33.             $item->count++;
  34.             break;
  35.         }
  36.     }
  37. }
  38. function sortByValue($a, $b) {
  39.     return $b->count - $a->count;
  40.  
  41. }
  42. if(isset($_GET['tags'])) {
  43.     $tagText = explode(",", $_GET['tags']);
  44.     //$tagText = array('Valeri', 'Bob', 'Chris', 'Bob', 'Chris', 'Valeri', 'Valeri');
  45.     $map = array();
  46.  
  47.     foreach ($tagText as $tag) {
  48.         $newTag = new Map($tag, 0);
  49.         if(!isInArray($newTag, $map)) {
  50.            $map[] = $newTag;
  51.         }
  52.         FindAndIncrease($tag, $map);
  53.  
  54.     }
  55.     usort($map, 'sortByValue');
  56.  
  57.     $content = '<div class="result">';
  58.     foreach ($map as $item) {
  59.         $content .= $item->tagName . ': ' . $item->count . '</br>';
  60.     }
  61.  
  62.     $content .= '</div>';
  63. }
  64. ?>
  65. <html>
  66. <head>
  67.     <title>Most Frequent Tag</title>
  68. </head>
  69. <body>
  70. <form action="MostFrequentTag.php" method="get">
  71.     <input type="text" name="tags" required="true"/><br/>
  72.     <input type="submit" name="submitBtn" value="Submit"/>
  73. </form>
  74. <?php
  75. if(isset($content))
  76.     echo $content
  77. ?>
  78. </body>
  79. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement