problem with forum index - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

problem with forum index

johnlcoxjohnlcox Posts: 92Member
My forum index is supposed to display the forums under each category, but for some reason it is displaying one forum 3 times then another forum 1 time on 2 of the categories and the 3rd category is correct. A link to this is http://www.youngpeoplevote.com/forum.php The code for that page is:

[code]
<?PHP
define('IN_YPV', true);
$ypv_root_path = './';

include($ypv_root_path . 'includes/template.inc');
include($ypv_root_path . 'includes/config.php');

session_start();
header("Cache-control: private"); // IE 6 Fix.

// Begin output
include($ypv_root_path . 'header.php');

$template->set_file("PAGE_BODY", "forum_body.tpl");

$forum_index_url = "forum.php";
$forum_index_text = "Forum Index";

// connect to host and select database
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
or die("Could not connect : " . mysql_error());
mysql_select_db($mysql_database)
or die("Could not select database");

// set query and select categories from database
$query = "SELECT * FROM ypv_forum_categories
ORDER BY cat_order ASC";
$catresult = mysql_query($query) or die("Query failed : " . mysql_error());

// category block
$template->set_block("PAGE_BODY", "categoryrow", "CAT_BLOCK");

while($catrow = mysql_fetch_array($catresult, MYSQL_ASSOC))
{
$template->set_var("CATEGORY_NAME", $catrow['cat_title']);

// select all forums in this category
$query = "SELECT * FROM ypv_forums
WHERE cat_id = '" . $catrow['cat_id'] . "'
ORDER BY forum_order ASC";
$forumresult = mysql_query($query) or die("Query failed : " . mysql_error());

$template->set_block("CAT_BLOCK", "forumrow", "FORUM_BLOCK");

while($forumrow = mysql_fetch_array($forumresult, MYSQL_ASSOC))
{
$forumlink = "viewforum.php?forum=" . $forumrow['forum_id'];

$query = "SELECT poster_id, post_username, post_time
FROM ypv_forum_posts
WHERE post_id = '" . $forumrow['forum_last_post_id'] . "'";
$postresult = mysql_query($query) or die("Query failed : " . mysql_error());
$lastpostrow = mysql_fetch_array($postresult, MYSQL_ASSOC);
if($lastpostrow['post_time'] > 0)
{
$lastposttime = gmdate("D d M Y g:i a", $lastpostrow['post_time'] + $timezone);

$lastpost_profile = 'profile.php?id=' . $lastpostrow['poster_id'];
$lastposter_link = "" . $lastpostrow['post_username'] . "";
}

$template->set_var(array("FORUM_LINK" => $forumlink,
"FORUM_TITLE" => $forumrow['forum_name'],
"FORUM_DESCRIPTION" => $forumrow['forum_desc'],
"FORUM_TOPICS" => $forumrow['forum_topics'],
"FORUM_POSTS" => $forumrow['forum_posts'],
"LAST_POST_TIME" => $lastposttime,
"LAST_POST_AUTHOR" => $lastposter_link));

$template->parse("FORUM_BLOCK", "forumrow", true);
}
$template->parse("CAT_BLOCK", "categoryrow", true);
}
mysql_close();

$template->set_var(array("U_INDEX" => $forum_index_url,
"L_INDEX" => $forum_index_text,
"FORUMS" => "Forum",
"TOPICS" => "Topics",
"POSTS" => "Posts",
"LAST_POST" => "Last Post"));

// output body
$template->pparse("output", "PAGE_BODY");

include($ypv_root_path . 'footer.php');

?>
[/code]

Comments

  • DarQDarQ Posts: 1,625Member
    [red]
    ah, a long piece of code. please try to use a little code as possible. it takes a while to read and understand > 30 lines of code..

    anyway, you have a nasty problem. i suggest that you try and narrow down the problem to the source. use plenty of debug echo's to check if the script does what you expect. that will help you out, at least, it always works for me ;-)
    [/red]

    : My forum index is supposed to display the forums under each category, but for some reason it is displaying one forum 3 times then another forum 1 time on 2 of the categories and the 3rd category is correct. A link to this is http://www.youngpeoplevote.com/forum.php The code for that page is:
    :
    : [code]
    : <?PHP
    : define('IN_YPV', true);
    : $ypv_root_path = './';
    :
    : include($ypv_root_path . 'includes/template.inc');
    : include($ypv_root_path . 'includes/config.php');
    :
    : session_start();
    : header("Cache-control: private"); // IE 6 Fix.
    :
    : // Begin output
    : include($ypv_root_path . 'header.php');
    :
    : $template->set_file("PAGE_BODY", "forum_body.tpl");
    :
    : $forum_index_url = "forum.php";
    : $forum_index_text = "Forum Index";
    :
    : // connect to host and select database
    : $link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
    : or die("Could not connect : " . mysql_error());
    : mysql_select_db($mysql_database)
    : or die("Could not select database");
    :
    : // set query and select categories from database
    : $query = "SELECT * FROM ypv_forum_categories
    : ORDER BY cat_order ASC";
    : $catresult = mysql_query($query) or die("Query failed : " . mysql_error());
    :
    : // category block
    : $template->set_block("PAGE_BODY", "categoryrow", "CAT_BLOCK");
    :
    : while($catrow = mysql_fetch_array($catresult, MYSQL_ASSOC))
    : {
    : $template->set_var("CATEGORY_NAME", $catrow['cat_title']);
    :
    : // select all forums in this category
    : $query = "SELECT * FROM ypv_forums
    : WHERE cat_id = '" . $catrow['cat_id'] . "'
    : ORDER BY forum_order ASC";
    : $forumresult = mysql_query($query) or die("Query failed : " . mysql_error());
    :
    : $template->set_block("CAT_BLOCK", "forumrow", "FORUM_BLOCK");
    :
    : while($forumrow = mysql_fetch_array($forumresult, MYSQL_ASSOC))
    : {
    : $forumlink = "viewforum.php?forum=" . $forumrow['forum_id'];
    :
    : $query = "SELECT poster_id, post_username, post_time
    : FROM ypv_forum_posts
    : WHERE post_id = '" . $forumrow['forum_last_post_id'] . "'";
    : $postresult = mysql_query($query) or die("Query failed : " . mysql_error());
    : $lastpostrow = mysql_fetch_array($postresult, MYSQL_ASSOC);
    : if($lastpostrow['post_time'] > 0)
    : {
    : $lastposttime = gmdate("D d M Y g:i a", $lastpostrow['post_time'] + $timezone);
    :
    : $lastpost_profile = 'profile.php?id=' . $lastpostrow['poster_id'];
    : $lastposter_link = "" . $lastpostrow['post_username'] . "";
    : }
    :
    : $template->set_var(array("FORUM_LINK" => $forumlink,
    : "FORUM_TITLE" => $forumrow['forum_name'],
    : "FORUM_DESCRIPTION" => $forumrow['forum_desc'],
    : "FORUM_TOPICS" => $forumrow['forum_topics'],
    : "FORUM_POSTS" => $forumrow['forum_posts'],
    : "LAST_POST_TIME" => $lastposttime,
    : "LAST_POST_AUTHOR" => $lastposter_link));
    :
    : $template->parse("FORUM_BLOCK", "forumrow", true);
    : }
    : $template->parse("CAT_BLOCK", "categoryrow", true);
    : }
    : mysql_close();
    :
    : $template->set_var(array("U_INDEX" => $forum_index_url,
    : "L_INDEX" => $forum_index_text,
    : "FORUMS" => "Forum",
    : "TOPICS" => "Topics",
    : "POSTS" => "Posts",
    : "LAST_POST" => "Last Post"));
    :
    : // output body
    : $template->pparse("output", "PAGE_BODY");
    :
    : include($ypv_root_path . 'footer.php');
    :
    : ?>
    : [/code]
    :

    [size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]
    url--> http://mark.space.servehttp.com

  • johnlcoxjohnlcox Posts: 92Member
    I've solved the problem. It turns out that it had to do with the nesting of template blocks. You have to start from the inner most block and work your way out to the outermost. Also if you loop through code of a block that has an inner block you must clear the contents of the inner block. If anyone would like a better explanation just ask, I can go into more detail.
Sign In or Register to comment.