I need to solve next problem:
I need the most optomized solution for count new time comments in blogposts.
My own solution is:
create one more tbl, where save blogpost_id, lastview_num_comment what updated with every review of post, and count_num_comment what == blog_post.num_comments
example:
<?php
function post($id){
if($id && $id!=0){
$sql = "SELECT `num_comments` FROM `blog_post` WHERE `id`=".quote_smart($id);
$res=mysql_query($sql);
$rw=mysql_fetch_array($res);
$sql = "UPDATE `new_comments` SET last_view_numcom=".$rw['num_comments'];
if(mysql_query($sql)){
return 1;
}else $this->error("database connect failed");
}else redirect("");
}
?>
but with everyone adding comment i need to use update query what aren’t the best solution
I don’t think there is better solution if you need real time number. Otherwise you could update this every x minutes by cron.
However you can improve this code by reducing it to one mysql_query run:
$sql = "UPDATEnew_commentsSET last_view_numcom = (SELECTnum_commentsFROMblog_postWHEREid=".quote_smart($id).")";