· 7 years ago · Jan 16, 2019, 08:20 PM
1CREATE TABLE `search` (
2 `id` int(11) NOT NULL AUTO_INCREMENT,
3 `term` varchar(255) NOT NULL DEFAULT '',
4 `counter` int(11) NOT NULL DEFAULT '1',
5 `last_search` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
6 PRIMARY KEY (`id`),
7 KEY `term` (`term`)
8) ENGINE=InnoDB;
9
10<?php
11// connect to the database
12$conn = mysql_connect('localhost', 'root', '******');
13if (!$conn or !mysql_select_db('tags', $conn)) die('cannot connect to db');
14
15// handle new searches
16if (isset($_GET['action']) and $_GET['action'] == 'search')
17{
18 // get the current time
19 $now = date("Y-m-d H:i:s");
20
21 // get the submitted term and prepare it for the database query
22 $term = mysql_real_escape_string(strip_tags(trim($_GET['term'])));
23
24 // check if the term has been submitted before
25 if (mysql_result(mysql_query("SELECT COUNT(id) FROM search WHERE term = '$term'"), 0) > 0)
26 {
27 // the term exists - update the counter and the last search timestamp
28 mysql_query("UPDATE search SET counter = counter+1, last_search = '$now' WHERE term = '$term'");
29 } else {
30 // the term does not exist - insert a new record
31 mysql_query("INSERT INTO search (term, last_search) VALUES ('$term', '$now')");
32 }
33}
34
35// prepare the tag cloud array for display
36$terms = array(); // create empty array
37$maximum = 0; // $maximum is the highest counter for a search term
38
39$query = mysql_query("SELECT term, counter FROM search ORDER BY counter DESC LIMIT 30");
40
41while ($row = mysql_fetch_array($query))
42{
43 $term = $row['term'];
44 $counter = $row['counter'];
45
46 // update $maximum if this term is more popular than the previous terms
47 if ($counter > $maximum) $maximum = $counter;
48
49 $terms[] = array('term' => $term, 'counter' => $counter);
50
51}
52
53// shuffle terms unless you want to retain the order of highest to lowest
54shuffle($terms);
55?>
56<!DOCTYPE html>
57<html>
58 <head>
59 <title>Tag Cloud Example</title>
60 <style type="text/css">
61 #tagcloud {
62 width: 300px;
63 background:#CFE3FF;
64 color:#0066FF;
65 padding: 10px;
66 border: 1px solid #559DFF;
67 text-align:center;
68 -moz-border-radius: 4px;
69 -webkit-border-radius: 4px;
70 border-radius: 4px;
71 }
72 #tagcloud a:link, #tagcloud a:visited {
73 text-decoration:none;
74 color: #333;
75 }
76 #tagcloud a:hover {
77 text-decoration: underline;
78 }
79 #tagcloud span {
80 padding: 4px;
81 }
82 #tagcloud .smallest {
83 font-size: x-small;
84 }
85 #tagcloud .small {
86 font-size: small;
87 }
88 #tagcloud .medium {
89 font-size:medium;
90 }
91 #tagcloud .large {
92 font-size:large;
93 }
94 #tagcloud .largest {
95 font-size:larger;
96 }
97 </style>
98 </head>
99 <body>
100 <h1>Search</h1>
101 <form id="search" method="get" action="?action=search">
102 <input type="text" name="term" id="term" />
103 <input type="submit" name="submit" id="submit" value="Search" />
104 </form>
105
106 <h2>Popular Searches</h2>
107 <div id="tagcloud">
108 <?php
109 // start looping through the tags
110 foreach ($terms as $term):
111 // determine the popularity of this term as a percentage
112 $percent = floor(($term['counter'] / $maximum) * 100);
113 // determine the class for this term based on the percentage
114 if ($percent < 20):
115 $class = 'smallest';
116 elseif ($percent >= 20 and $percent < 40):
117 $class = 'small';
118 elseif ($percent >= 40 and $percent < 60):
119 $class = 'medium';
120 elseif ($percent >= 60 and $percent < 80):
121 $class = 'large';
122 else:
123 $class = 'largest';
124 endif;
125 ?>
126 <span class="<?php echo $class; ?>">
127 <a href="search.php?search=<?php echo urlencode($term['term']); ?>"><?php echo $term['term']; ?></a>
128 </span>
129 <?php endforeach; ?>
130 </div>
131 </body>
132</html>