Browse Source

Counting comments inside directory

k4be 1 year ago
parent
commit
bbae68f012
2 changed files with 22 additions and 3 deletions
  1. 20 1
      include/comment.php
  2. 2 2
      index.php

+ 20 - 1
include/comment.php

@@ -46,13 +46,32 @@ function count_comments($id, $type) {
 		echo 'Query error: ' . $e->getMessage();
 		die();
 	}
-	$comments = [];
 	if($row = $res->fetch(PDO::FETCH_ASSOC)) {
 		return $row['count'];
 	}
 	return 0;
 }
 
+function count_comments_all_dir_images($id) {
+	global $pdo;
+
+	$sql = 'SELECT count(*) AS count FROM comments WHERE image IN (SELECT id FROM images WHERE dir = :dir)';
+	$values = array(
+		':dir' => $id,
+	);
+        try {
+                $res = $pdo->prepare($sql);
+                $res->execute($values);
+        } catch (PDOException $e) {
+                echo 'Query error: ' . $e->getMessage();
+                die();
+        }
+        if($row = $res->fetch(PDO::FETCH_ASSOC)) {
+                return $row['count'];
+        }
+        return 0;
+}
+
 function db_store_comment($nick, $email, $type, $id, $content) {
 	global $pdo;
 

+ 2 - 2
index.php

@@ -8,7 +8,7 @@ $dirs = get_dir_list();
 $dirs_html = '';
 
 foreach ($dirs as $dir) {
-	$dirs_html .= '<tr><td><a href="' . $config['basepath'] . 'dir.php?dir=' . urlencode($dir['path']) . '">' . htmlspecialchars($dir['name']) . '</a></td><td>' . $dir['count'] . '</td><td>' . date('j.m.Y', $dir['date']) . '</td><td>' . count_comments($dir['id'], 'dir') . '</td></tr>';
+	$dirs_html .= '<tr><td><a href="' . $config['basepath'] . 'dir.php?dir=' . urlencode($dir['path']) . '">' . htmlspecialchars($dir['name']) . '</a></td><td>' . $dir['count'] . '</td><td>' . date('j.m.Y', $dir['date']) . '</td><td>' . count_comments($dir['id'], 'dir') . '</td><td>' . count_comments_all_dir_images($dir['id']) . '</td></tr>';
 }
 
 ?><!DOCTYPE html>
@@ -21,7 +21,7 @@ foreach ($dirs as $dir) {
 <body>
 <h1>Dostępne katalogi z obrazkami</h1>
 Uwaga, nowy skrypt. Zgłoś problemy administratorowi.
-<table id="dirlist"><tr><th>Nazwa</th><th>Obrazków</th><th>Utworzono</th><th>Komentarzy<br>do katalogu</th></tr>
+<table id="dirlist"><tr><th>Nazwa</th><th>Obrazków</th><th>Utworzono</th><th>Komentarzy<br>do katalogu</th><th>Komentarzy<br>do obrazków</th></tr>
 <?=$dirs_html?>
 </table>
 <a class="k4" href="http://k4be.pl/"></a>