Browse Source

Initial work on tags

pull/2/head
Chris Smith 13 years ago
parent
commit
1c947960d2
6 changed files with 152 additions and 0 deletions
  1. 37
    0
      inc/tags.php
  2. 2
    0
      latest.php
  3. 6
    0
      res/style.css
  4. BIN
      res/tag.png
  5. 4
    0
      submit.php
  6. 103
    0
      tag.php

+ 37
- 0
inc/tags.php View File

@@ -0,0 +1,37 @@
1
+<?PHP
2
+
3
+function showTags($quote) {
4
+
5
+ echo '<p class="tags">';
6
+
7
+ $sql = 'SELECT tag_text, COUNT(*) FROM tags WHERE quote_id = ' . $quote . ' GROUP BY tag_text';
8
+ $res = mysql_query($sql);
9
+
10
+ if (mysql_num_rows($res) == 0) {
11
+  echo 'This quote has not been tagged yet';
12
+ }
13
+
14
+ while ($row = mysql_fetch_assoc($res)) {
15
+  echo '  <a href="', BASE, 'tag?tag=', htmlentities($row['tag_text'], ENT_QUOTES, 'UTF-8'), '">';
16
+  echo htmlentities($row['tag_text'], ENT_QUOTES, 'UTF-8'), '</a>';
17
+ }
18
+
19
+ echo '</p>';
20
+
21
+}
22
+
23
+function addAutoTags($id) {
24
+
25
+ $sql = 'SELECT quote_quote FROM quotes WHERE quote_id = ' . $id;
26
+ $res = mysql_query($sql);
27
+ $text = mysql_result($res, 0);
28
+
29
+ preg_match_all('/^(?:\[.*?\]|.*?\\|)?\s*<(?:[@+])?([^\s]*?)>/m', $text, $matches);
30
+
31
+ foreach ($matches[1] as $user) {
32
+  mysql_query('INSERT INTO tags (quote_id, tag_text, user_id) VALUES (' . $id . ', \'@' . m($user) . '\', 0)');
33
+ }
34
+
35
+}
36
+
37
+?>

+ 2
- 0
latest.php View File

@@ -1,6 +1,7 @@
1 1
 <?PHP
2 2
 
3 3
  require_once('inc/database.php');
4
+ require_once('inc/tags.php');
4 5
 
5 6
  define('TITLE', 'Latest');
6 7
 
@@ -36,6 +37,7 @@
36 37
 
37 38
 ?>
38 39
   </p>
40
+<?PHP showTags($row['quote_id']); ?>
39 41
   <div class="quotebody">
40 42
    <?PHP echo nl2br(htmlentities($row['quote_quote'], ENT_QUOTES, 'UTF-8')); ?>
41 43
   </div>

+ 6
- 0
res/style.css View File

@@ -111,3 +111,9 @@ div.nav {
111 111
  padding-left: 10px;
112 112
  padding-bottom: 20px; 
113 113
 }
114
+
115
+p.tags {
116
+ padding-left: 20px;
117
+ font-size: small;
118
+ background: url('tag.png') no-repeat left center;
119
+}

BIN
res/tag.png View File


+ 4
- 0
submit.php View File

@@ -3,6 +3,7 @@
3 3
  require_once('inc/account.php');
4 4
  require_once('inc/database.php');
5 5
  require_once('inc/settings.php');
6
+ require_once('inc/tags.php');
6 7
 
7 8
  if (!isset($_SESSION['uid'])) {
8 9
   header('Location: '.BASE);
@@ -15,6 +16,9 @@
15 16
   }
16 17
   $sql = 'INSERT INTO quotes (quote_quote, quote_time, user_id) VALUES (\''.m($_POST['quote']).'\', '.time().', '.$_SESSION['uid'].')';
17 18
   mysql_query($sql);
19
+
20
+  doAutoTags(mysql_insert_id());
21
+
18 22
   header('Location: '.BASE.'latest');
19 23
   exit;
20 24
  }

+ 103
- 0
tag.php View File

@@ -0,0 +1,103 @@
1
+<?PHP
2
+
3
+ require_once('inc/database.php');
4
+
5
+ if (isset($_GET['tag'])) {
6
+  define('TAG', $_GET['tag']);
7
+ } else {
8
+  header('Location: ' . BASE . 'latest');
9
+  exit;
10
+ }
11
+
12
+ define('TITLE', 'Browse quotes tagged ' . htmlentities(TAG, ENT_QUOTES, 'UTF-8'));
13
+
14
+ require_once('inc/header.php');
15
+ require_once('inc/tags.php');
16
+
17
+ $offset = 0;
18
+ if (isset($_GET['o']) && ctype_digit($_GET['o'])) {
19
+  $offset = $_GET['o'];
20
+ }
21
+
22
+ $quotes = array();
23
+ $sql = 'SELECT DISTINCT(quote_id) FROM tags WHERE tag_text = \'' . m(TAG) . '\'';
24
+ $res = mysql_query($sql);
25
+ while ($row = mysql_fetch_assoc($res)) {
26
+  $quotes[] = $row['quote_id']; 
27
+ }
28
+
29
+ $where = 'WHERE quote_id IN (' . implode(', ', $quotes) . ')';
30
+
31
+ $sql = 'SELECT COUNT(*) FROM quotes ' . $where;
32
+ $res = mysql_query($sql);
33
+ $row = mysql_fetch_array($res);
34
+ define('QUOTES', $row[0]);
35
+
36
+ $sql = 'SELECT quote_id, quote_quote, quote_rating FROM quotes ' . $where . ' ORDER BY quote_id LIMIT '.$offset.',25';
37
+ $res = mysql_query($sql)
38
+
39
+?>
40
+<div>
41
+ <h2>Browse quotes tagged <?PHP echo htmlentities(TAG, ENT_QUOTES, 'UTF-8'); ?></h2>
42
+<?PHP
43
+
44
+ echo '<div class="nav">';
45
+ if ($offset > 0) {
46
+  echo '<a href="browse?o='.($offset-25).'">&lt;&lt; Previous</a> |';
47
+ }
48
+ if ($offset + 25 > QUOTES) { $max = QUOTES; } else { $max = $offset + 25; }
49
+ echo ' Viewing quotes '.(1+$offset).' to '.$max.' of '.QUOTES.'.';
50
+ if ($max < QUOTES) {
51
+  echo ' | <a href="browse?o='.$max.'">Next &gt;&gt;</a>';
52
+ }
53
+ echo '</div>';
54
+
55
+
56
+ $i = 0;
57
+ while ($row = mysql_fetch_array($res)) {
58
+  $i = 1 - $i;
59
+  if ($i == 1) { $e = 'even'; } else { $e = 'odd'; }
60
+?>
61
+ <div class="quote <?PHP echo $e; ?>">
62
+<?PHP
63
+ if (isset($_SESSION['uid'])) {
64
+  doRate($row['quote_id']);
65
+ }
66
+?>
67
+  <p>
68
+   Quote <a href="<?PHP echo BASE; ?>browse?q=<?PHP echo $row['quote_id']; ?>">#<?PHP echo $row['quote_id']; ?></a>. 
69
+   Rating <?PHP echo round($row['quote_rating'],2); ?>.
70
+<?PHP
71
+
72
+ if (!isset($_SESSION['uid'])) {
73
+  echo ' <a href="'.BASE.'login">Login to rate</a>.';
74
+ }
75
+
76
+?>
77
+  </p>
78
+<?PHP showTags($row['quote_id']); ?>
79
+  <div class="quotebody">
80
+   <?PHP echo nl2br(htmlentities($row['quote_quote'], ENT_QUOTES, 'UTF-8')); ?>
81
+  </div>
82
+ </div>
83
+<?PHP
84
+ }
85
+
86
+ echo '<div class="nav">';
87
+ if ($offset > 0) {
88
+  echo '<a href="browse?o='.($offset-25).'">&lt;&lt; Previous</a> |';
89
+ }
90
+ if ($offset + 25 > QUOTES) { $max = QUOTES; } else { $max = $offset + 25; }
91
+ echo ' Viewing quotes '.(1+$offset).' to '.$max.' of '.QUOTES.'.';
92
+ if ($max < QUOTES) {
93
+  echo ' | <a href="browse?o='.$max.'">Next &gt;&gt;</a>';
94
+ }
95
+ echo '</div>';
96
+
97
+?>
98
+</div>
99
+<?PHP
100
+
101
+ require_once('inc/footer.php');
102
+
103
+?>

Loading…
Cancel
Save