dongshan2680 2015-06-29 19:30
浏览 24

从单个数据库获取线程信息

I am trying to learn how to create a simple forum and i stuck in here;

My Database is something like this; (post table)

ThreadID |  PostID  | Author |  Title   |  Content |    Date    |    Time
--------------------------------------------------------------
   1     |     1    |  Jack  | Thread 1 |    ...   | 14/12/2015 |   20:21
   1     |     2    |  Arn   |          |    ...   | 15/12/2015 |   19:28
   1     |     3    |  Hank  |          |    ...   | 15/12/2015 |   20:24
   2     |     1    |  Tom   | Thread 2 |    ...   | 15/12/2015 |   22:41
   2     |     2    |  Frank |          |    ...   | 16/12/2015 |   13:06

Post table contains both replies and threads And i would like to get some information from database on category segment

and this is what i want;

Title      |   Author   |   Replies   | Views |               Last Poster
-------------------------------------------------------------------------
Thread 1   |    Jack    |      2      |   -   |   Hank - 20:24 15/12/2015
Thread 2   |    Tom     |      1      |   -   |  Frank - 20:24 15/12/2015

and finally my code is;

$get_threads = "SELECT threadid, title, author, postid, date, time FROM post WHERE categoryid = '" . $category . "'";   
$threads = mysqli_query($conn, $get_threads); if (mysqli_num_rows($threads) > 0) {while($row = mysqli_fetch_assoc($threads)) {
$get_reply = "SELECT (MAX(postid)-1) rn, (max(postid)) lp FROM post WHERE threadid = '".$row["threadid"]."'";
$reply = mysqli_query($conn, $get_reply); if (mysqli_num_rows($reply) > 0) {while($rod = mysqli_fetch_assoc($reply)) {
$get_lp = "SELECT op.author, op.date, op.time FROM post op INNER JOIN (SELECT author, date, time FROM post WHERE POSTID = '".$rod["lp"]."' GROUP BY threadid) lp ON op.author = lp.author AND op.date = lp.date AND op.time = lp.time WHERE threadid = '".$row['threadid']."'";
$lp = mysqli_query($conn, $get_lp); if (mysqli_num_rows($lp) > 0) {while($roc = mysqli_fetch_assoc($lp)) {

echo "<div class='thread'><div class='threadbox'>";
echo "<a href='forum.php?post=".$row["threadid"]."'class='ttitle'>".$row["title"]."</a>";
echo "<div class='tauthor'>".$row["author"]."</div>";
echo "<div class='treplies'>".$rod["rn"]."</div>";
echo "<div class='tviews'>".$row["date"]."</div>";
echo "<div class='tlastposter'>".$roc['author']." - ".$roc["time"]." ".$roc["date"]."</div>";
echo "</div></div><div class='threaddiv'></div>";

}}}}}} else {echo "<center>Content Not Found</center>";}

Well it may be the worst code ever written but its my first, i apologize for that anyway its duplicating some results i couldnt figure out.

  1. Please help me about how i can handle it?
  2. If you improove and shorten this code i would be glad ^^

Thanks in Advance

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?