Ver código fonte

Count directory comments

k4be 1 ano atrás
pai
commit
1473470d7a
2 arquivos alterados com 25 adições e 3 exclusões
  1. 22 1
      include/comment.php
  2. 3 2
      index.php

+ 22 - 1
include/comment.php

@@ -14,7 +14,7 @@ function get_field_name($type) {
 function get_comments($id, $type) {
 	global $pdo;
 
-	$sql = 'SELECT * FROM comments WHERE ' . get_field_name($type) . ' = :id';
+	$sql = 'SELECT * FROM comments WHERE ' . get_field_name($type) . ' = :id ORDER BY date DESC';
 	$values = array(
 		':id' => $id,
 	);
@@ -32,6 +32,27 @@ function get_comments($id, $type) {
 	return $comments;
 }
 
+function count_comments($id, $type) {
+	global $pdo;
+
+	$sql = 'SELECT count(*) AS count FROM comments WHERE ' . get_field_name($type) . ' = :id';
+	$values = array(
+		':id' => $id,
+	);
+	try {
+		$res = $pdo->prepare($sql);
+		$res->execute($values);
+	} catch (PDOException $e) {
+		echo 'Query error: ' . $e->getMessage();
+		die();
+	}
+	$comments = [];
+	if($row = $res->fetch(PDO::FETCH_ASSOC)) {
+		return $row['count'];
+	}
+	return 0;
+}
+
 function db_store_comment($nick, $email, $type, $id, $content) {
 	global $pdo;
 

+ 3 - 2
index.php

@@ -1,13 +1,14 @@
 <?php
 require_once('include/config.php');
 require_once('include/db.php');
+require_once('include/comment.php');
 
 $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></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></tr>';
 }
 
 ?><!DOCTYPE html>
@@ -20,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></tr>
+<table id="dirlist"><tr><th>Nazwa</th><th>Obrazków</th><th>Utworzono</th><th>Komentarzy<br>do katalogu</th></tr>
 <?=$dirs_html?>
 </table>
 <a class="k4" href="http://k4be.pl/"></a>