here is the controller method, pretty small
the query passed to template (topicsres)
so far so good but in the template you will see the big php code block with 6 query's in. Only html should be in there. This is the biggest block in the templates - for most of the others i can use a join and be a little creative. Just wonder whats the best way before i start working through them all
the logic here should not be so different from your own based on the TT original forum and here is the viewforum page https://torrenttrader.uk/forums/viewforum&forumid=110
/**
* View Forum.
*/
public function viewforum()
{
$this->validForumUser();
$forumid = Input::get("forumid");
if (!$this->valid->validId($forumid)) {
Session::flash('info', Lang::T("FORUMS_DENIED"), URLROOT . "/forums");
}
// Get forum name
$res = DB::run("SELECT name, minclassread, guest_read FROM forum_forums WHERE id=?", [$forumid]);
$arr = $res->fetch(PDO::FETCH_ASSOC);
$forumname = $arr["name"];
if (!$forumname || $_SESSION['class'] < $arr["minclassread"] && $arr["guest_read"] == "no") {
Session::flash('info', Lang::T("FORUMS_NOT_PERMIT"), URLROOT . "/forums");
}
// Pagination
$count = get_row_count("forum_topics", "WHERE forumid=$forumid");
list($pagertop, $pagerbottom, $limit) = pager(25, $count, URLROOT . "/forums/viewforum&forumid=$forumid&");
// Get topics data and display category
$topicsres = DB::run("SELECT * FROM forum_topics WHERE forumid=$forumid ORDER BY sticky, lastpost DESC $limit")->fetchAll();
$data = [
'title' => 'Forums',
'topicsres' => $topicsres,
'forumname' => $forumname,
'forumid' => $forumid,
'pagerbottom' => $pagerbottom,
];
$this->view('forum/viewforum', $data, 'user');
}
the query passed to template (topicsres)
array(1) { [0]=> array(9) {
["id"]=> int(2)
["userid"]=> int(1)
["subject"]=> string(11) "subject name"
["locked"]=> string(2) "no"
["forumid"]=> int(3)
["lastpost"]=> int(8)
["moved"]=> string(2) "no"
["sticky"]=> string(2) "no"
["views"]=> int(65) } }
so far so good but in the template you will see the big php code block with 6 query's in. Only html should be in there. This is the biggest block in the templates - for most of the others i can use a join and be a little creative. Just wonder whats the best way before i start working through them all
<?php
forumheader($data['forumname']);
if ($_SESSION['loggedin'] == true) {
?>
<table class='table table-striped'><tr><td>
<div align='right'><a href='<?php echo URLROOT; ?>/forums/newtopic&forumid=<?php echo $data['forumid']; ?>'>
<button type='submit' class='btn btn-sm btn-warning'>New Post</button></a></div>
</td></tr></table>
<?php
}
if ($data['topicsres'] > 0) {
?>
<div class="row">
<div class="col-lg-12">
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row card-header">
<div class="col-md-1">
Read
</div>
<div class="col-md-4">
Topic
</div>
<div class="col-md-1 d-none d-sm-block">
Replies
</div>
<div class="col-md-1 d-none d-sm-block">
Views
</div>
<div class="col-md-1 d-none d-sm-block">
Author
</div>
<div class="col-md-2 d-none d-sm-block">
Last Post
</div>
<?php
if ($_SESSION["edit_forum"] == "yes" || $_SESSION["delete_forum"] == "yes") {
?>
<div class="col-md-2 d-none d-sm-block">
Moderate
</div>
<?php
}
print("</div>");
foreach ($data['topicsres'] as $topicarr) {
//////// ALL THIS ////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
$topicid = $topicarr["id"];
$topic_userid = $topicarr["userid"];
$locked = $topicarr["locked"] == "yes";
$moved = $topicarr["moved"] == "yes";
$sticky = $topicarr["sticky"] == "yes";
//---- Get reply count
$res = DB::run("SELECT COUNT(*) FROM forum_posts WHERE topicid=$topicid");
$arr = $res->fetch(PDO::FETCH_LAZY);
$posts = $arr[0];
$replies = max(0, $posts - 1);
//---- Get userID and date of last post
$res = DB::run("SELECT * FROM forum_posts WHERE topicid=$topicid ORDER BY id DESC LIMIT 1");
$arr = $res->fetch(PDO::FETCH_ASSOC);
$lppostid = $arr["id"];
$lpuserid = (int) $arr["userid"];
$lpadded = TimeDate::utc_to_tz($arr["added"]);
//------ Get name of last poster
if ($lpuserid > 0) {
$res = DB::run("SELECT * FROM users WHERE id=$lpuserid");
if ($res->rowCount() == 1) {
$arr = $res->fetch(PDO::FETCH_ASSOC);
$lpusername = "<a href='" . URLROOT . "/profile?id=$lpuserid'>" . Users::coloredname($arr['username']) . "</a>";
} else {
$lpusername = "Deluser";
}
} else {
$lpusername = "Deluser";
}
//------ Get author
if ($topic_userid > 0) {
$res = DB::run("SELECT username FROM users WHERE id=$topic_userid");
if ($res->rowCount() == 1) {
$arr = $res->fetch(PDO::FETCH_ASSOC);
$lpauthor = "<a href='" . URLROOT . "/profile?id=$topic_userid'>" . Users::coloredname($arr['username']) . "</a>";
} else {
$lpauthor = "Deluser";
}
} else {
$lpauthor = "Deluser";
}
// Topic Views
$viewsq = DB::run("SELECT views FROM forum_topics WHERE id=$topicid");
$viewsa = $viewsq->fetch(PDO::FETCH_LAZY);
$views = $viewsa[0];
// End
//---- Print row
if ($_SESSION) {
$r = DB::run("SELECT lastpostread FROM forum_readposts WHERE userid=$_SESSION[id] AND topicid=$topicid");
$a = $r->fetch(PDO::FETCH_LAZY);
}
$new = !$a || $lppostid > $a[0];
$topicpic = ($locked ? ($new ? "folder_locked_new" : "folder_locked") : ($new ? "folder_new" : "folder"));
$subject = ($sticky ? "<b>" . Lang::T("FORUMS_STICKY") . ": </b>" : "") . "<a href='" . URLROOT . "/topic?topicid=$topicid'><b>" .
encodehtml(stripslashes($topicarr["subject"])) . "</b></a>$topicpages";
?>
<div class="row border border-primary">
<div class="col-md-1 d-none d-sm-block">
<img src='<?php echo URLROOT; ?>/assets/images/forum/<?php echo $topicpic ?>.png' alt='' />
</div>
<div class="col-md-4">
<?php echo $subject; ?>
</div>
<div class="col-md-1 d-none d-sm-block">
<?php echo $replies; ?>
</div>
<div class="col-md-1 d-none d-sm-block">
<?php echo $views; ?>
</div>
<div class="col-md-1 d-none d-sm-block">
<?php echo $lpauthor; ?>
</div>
<div class="col-md-2">
<span class='small'>by <?php echo $lpusername; ?><br /><span style='white-space: nowrap'><?php echo $lpadded; ?></span></span>
</div>
<?php
if ($_SESSION["edit_forum"] == "yes" || $_SESSION["delete_forum"] == "yes") {
print("<div class='col-md-2 d-none d-sm-block'>");
if ($locked) {
print("<a href='" . URLROOT . "/forums/unlocktopic&forumid=$data[forumid]&topicid=$topicid&page=$page' title='Unlock'><img src='" . URLROOT . "/assets/images/forum/topic_unlock.png' alt='UnLock Topic' /></a>\n");
} else {
print("<a href='" . URLROOT . "/forums/locktopic&forumid=$data[forumid]&topicid=$topicid&page=$page' title='Lock'><img src='" . URLROOT . "/assets/images/forum/topic_lock.png' alt='Lock Topic' /></a>\n");
}
print("<a href='" . URLROOT . "/forums/deletetopic&topicid=$topicid&sure=0' title='Delete'><img src='" . URLROOT . "/assets/images/forum/topic_delete.png' alt='Delete Topic' /></a>\n");
if ($sticky) {
print("<a href='" . URLROOT . "/forums/unsetsticky&forumid=$data[forumid]&topicid=$topicid&page=$page' title='UnStick'><img src='" . URLROOT . "/assets/images/forum/older_sticky_new.png' alt='Unstick Topic' /></a>\n");
} else {
print("<a href='" . URLROOT . "/forums/setsticky&forumid=$data[forumid]&topicid=$topicid&page=$page' title='Stick'><img src='" . URLROOT . "/assets/images/forum/folder_sticky.png' alt='Stick Topic' /></a>\n");
}
print("</div>");
}
print("</div>");
}
print("</div></div></div>");
print ($data['pagerbottom']);
} else {
print("<p align='center'>No topics found</p>\n");
}
the logic here should not be so different from your own based on the TT original forum and here is the viewforum page https://torrenttrader.uk/forums/viewforum&forumid=110
Last edited by M-jay
on 2021-06-10 13:53:43
on 2021-06-10 13:53:43










