|
From: mdw c. <myd...@li...> - 2002-02-19 00:33:46
|
MyDynaWeb CVS committal Author : sukria Project : mydynaweb Module : htdocs Dir : mydynaweb/htdocs Modified Files: comment.php add_comment.php save_coment.php Log Message: OK, now the comments are like a true forum : you can comment a comment ! have a look here for an example : http://www.sukria.net/comment.php?news=100 =================================================================== RCS file: /cvsroot/mydynaweb/mydynaweb/htdocs/comment.php,v retrieving revision 1.13 retrieving revision 1.14 diff -u -3 -r1.13 -r1.14 --- comment.php 15 Jan 2002 11:16:46 -0000 1.13 +++ comment.php 19 Feb 2002 00:33:15 -0000 1.14 @@ -6,6 +6,8 @@ $DBH=new db(); $DBH->open($dbName, $dbServer, $dbUser, $dbPass); +define("REACT", translate('REACT', $DBH)); + $ca = new cache($DBH); if ($file_name=$ca->is_cached("COMMENT.$news", USER_LANG, ACTIVE_THEME)) { @@ -28,9 +30,10 @@ $info = "<center>" . translate('POSTED_BY', $DBH). " " . $row['user_name'].", ".$row['date'] . "</center>"; $sql = "select count(*) from comments where _num_news = $row[0]"; - $rqt2 = new query($DBH); - $rqt2->query($DBH, $sql); - $row2 = $rqt2->getrow(); +$rqt2 = new query($DBH); +$rqt2->query($DBH, $sql); +$row2 = $rqt2->getrow(); +$num_comments = $row2[0]; // here we can increase the hit number $row[nbhit]++; @@ -44,32 +47,112 @@ $PAGE .= get_piece_of_news($row[1], '<P>' . $row['texte'] . '<br><br>' . $row['long_text'], $row[4], translate($row[5],$DBH), "news.php?q=$row[6]", $row[nbhit], $row[0], $row2[0], strlen($row['long_text']) * 8, $DBH); -// we get the comments -$sql = "select title, content, user_name, user_mail, date_ins - from comments - where _num_news = $news - order by num_cmt desc"; -$rqt = new query($DBH); -$rqt->query($DBH, $sql); -$num_cmt = 0; +// The react button +$PAGE .= "<center><b><form action='add_comment.php'> +<input type='hidden' name='num_news' value='$news'> +<input type='submit' value='".translate("COMMENT_NEWS",$DBH)."'></form> +</center> + +<p>"; + + +//////////////////////////////////////////////////////////////////////// +// the comments related to that piece of news + +// Step 1 : init stuff +$msg_infos = read_messages($DBH, $news); +$get_message = $msg_infos[0]; +$get_parents = $msg_infos[1]; +$get_children = $msg_infos[2]; -while ($row = $rqt->getrow()) { - $num_cmt++; +if (sizeof($get_parents)) { + $PAGE .= "<br><b>$num_comments " . translate("COMMENTS", $DBH). " : </b><br><br>"; +} - if (! strlen($row['user_name'])) { - $row['user_name'] = translate("anonymous",$DBH); - } - - $PAGE .= get_template(THEMES_PATH."/comment.html", $row); - +// Step 2 : print out the threads +foreach ($get_parents as $thread) { + $PAGE .= print_message($thread, 0, $get_message, $get_children); } +//////////////////////////////////////////////////////////////////////// -$PAGE .= "<P><center><b><a href='add_comment.php?num_news=$news'>".translate("COMMENT_NEWS",$DBH)."</a></b></P></center>"; $CONTENT = get_page("<h2>$news_titre</h2>", $info, $PAGE, $DBH); print_template(THEMES_PATH."/main.html", $CONTENT, $row[1], $DBH,"COMMENT.$news", $REMOTE_ADDR, $HTTP_REFERER, $MSG, 10, 50); } + + + + +/* + Great news : comments are recursive ! + Now people can answer to comment like + in every forum ... + So we need a recursive function which + can draw messages and their children... +*/ + +function print_message ($num_cmt, $level, $get_message, $get_children) +{ + $HTML = ""; + + $get_message[$num_cmt]['REACT'] = REACT; + $get_message[$num_cmt]['SIZE'] = 100; + for ($i=0; $i<$level; $i++) { + $get_message[$num_cmt]['SIZE'] -= 5; + } + + $HTML .= get_template(THEMES_PATH."/comment.html", $get_message[$num_cmt]); + + if (sizeof($get_children[$num_cmt])) { + foreach ($get_children[$num_cmt] as $child) { + $HTML .= print_message($child, $level+1, $get_message, $get_children); + } + } + + return $HTML; +} + +/* + To increase speed, we do only one query to the DB : + get every comments posted to that piece of news and + put it into a hashtable $message[num] => [FIELD] => content + for instance + $message[12][TITLE] send me the title of the #12 comment +*/ +function read_messages($dbh, $num_news) +{ + // the hashtable with the info of the messages + $get_message = Array(); + + // return an array of children + $get_children = Array(); + + $sql = "select num_cmt, title, content, user_name, user_mail, date_ins, pere, _num_news + from comments + where _num_news = $num_news + order by num_cmt desc"; + $rqt = new query($dbh); + $rqt->query($dbh, $sql) or send_sql_error($PHP_SELF, $sql, $rqt->error()); + + // fetching the message and filling the hash with infos... + while ($row = $rqt->getrow()) { + $get_message[$row[0]] = $row; + + // if this message has a parent, say it ! + if ($row['pere']) { + // push the numcmt to the table of children of the parent + $get_children[$row['pere']][sizeof($get_children[$row['pere']])] = $row['num_cmt']; + } + else { + // this message begins a new thread, lets say it ! + $get_parents[sizeof($get_parents)] = $row['num_cmt']; + } + } + + return Array ($get_message, $get_parents, $get_children); +} + ?> =================================================================== RCS file: /cvsroot/mydynaweb/mydynaweb/htdocs/add_comment.php,v retrieving revision 1.6 retrieving revision 1.7 diff -u -3 -r1.6 -r1.7 --- add_comment.php 8 Nov 2001 03:11:12 -0000 1.6 +++ add_comment.php 19 Feb 2002 00:33:16 -0000 1.7 @@ -24,7 +24,7 @@ } // we get every comment posted for that news (for creating the reply listbox) -$sql = "SELECT title +$sql = "SELECT title, num_comment FROM comments WHERE _num_news = $num_news ORDER BY date_ins DESC"; @@ -32,10 +32,12 @@ $rqt->query($DBH, $sql); $REPLY_LIST = "<select name='reply' onchange=\"update_title();return(false);\">\n"; while ($row = $rqt->getrow()) { - $REPLY_LIST .= "\t<option>$row[0]</option>\n"; + $REPLY_LIST .= "\t<option value=''>$row[0]</option>\n"; } $REPLY_LIST .= "</SELECT>\n"; +if (!isset($pere)) $pere = 0; + $formulaire = " @@ -57,6 +59,7 @@ <FORM name=\"form_cmt\" action=\"save_coment.php\" METHOD='POST'> <input type='hidden' name='num_news' value='$num_news'> +<input type='hidden' name='pere' value='$pere'> <table align=\"center\" cellpadding=0 cellspacing=5 border=0> =================================================================== RCS file: /cvsroot/mydynaweb/mydynaweb/htdocs/save_coment.php,v retrieving revision 1.10 retrieving revision 1.11 diff -u -3 -r1.10 -r1.11 --- save_coment.php 24 Jan 2002 10:07:44 -0000 1.10 +++ save_coment.php 19 Feb 2002 00:33:16 -0000 1.11 @@ -45,13 +45,14 @@ $content = ereg_replace("((ftp://)|(http://))(([[:alnum:]]|[[:punct:]])*)", "<a href=\"\\0\">\\0</a>",$content); - + // we insert the comment in the database + if (!isset($pere)) $pere = 0; - $sql = "insert into comments (title, _num_news, date_ins, content, user_name, user_mail) - values ('$titre', $num_news, '$aujourdhui', '$content', '$username', '$usermail')"; + $sql = "insert into comments (title, _num_news, date_ins, content, user_name, user_mail, pere) + values ('$titre', $num_news, '$aujourdhui', '$content', '$username', '$usermail', $pere)"; $rqt = new query($DBH); $rqt->query($DBH, $sql) or die($sql . $rqt->error()); |