Hey all.

I'm at my wit's end with this web application/forum.

I'm attempting to accomplish something like vBulletin or phpbb where each category is shown and below it all of it's forums. I'm using CGI::Application with the DBH plugin, DBI and HTML::Template.

Simple enough, no?

Well obviously as you can tell, i'm having issues with this.

At first i was able to retrieve the categories and show the forums below it, however i wasn't able to restrict the forums to their respective categories.

Currently, I'm attempting to use a WHERE clause to provide some restriction/categorization, but to no avail.

Here is the main sub that retrieves the categories and their respective forums:

## display the forums in list/table form sub main { my $self = shift; my $q = $self->query; my $tmpl = $self->load_tmpl( "main.html", associate => $q ); # retrieve all forum categories my $categories = $self->dbh->selectall_arrayref( q{ SELECT DISTINCT(category) FROM mx_forums }, { Slice => {} } ); my $sth = $self->dbh->prepare(q[ SELECT IFNULL(COUNT(mx_threads.forum_id),0) AS thread_count, IFNULL(MAX(mx_threads.id), 0) AS last_post, IFNULL(mx_threads.is_new, 0) as is_new, mx_forums.id, mx_forums.forum_name, mx_forums.forum_description FROM mx_forums LEFT JOIN mx_threads ON mx_threads.forum_id = mx_forums.id WHERE mx_forums.category = ? GROUP BY mx_forums.id ORDER BY mx_forums.create_date DESC ]); for ( @{ $categories } ) { $sth->execute( $_ ) or die $self->dbh->errstr; } my $forums = $sth->fetchall_arrayref({}); ## Since we can't retrieve everything in ONE query, we have to bre +ak it up a bit. # format is: $forums[$loop_count]->{blah} #retrieve ids of all existing forums my $forum_ids = $self->dbh->selectall_arrayref( q{ SELECT id FROM mx_forums } ); # loop through forum ids and retrieve each forums thread count # NOT for IDs...for the array index my $loop_counter = 0; for my $i ( @{ $forum_ids } ) { # count the messages my $threads = $self->dbh->selectrow_array( q{ SELECT COUNT(*) FROM mx_threads WHERE forum_id=? }, { Slice => {} }, $i ) ; my $replies = $self->dbh->selectrow_array( q{ SELECT COUNT(*) FROM mx_replies WHERE forum_id=? }, { Slice => {} }, $i ); # COMMENTED OUT - fetchrow_arrayref not returning anything cur +rently # set the subject # @{$forums}[$loop_counter]->{subject} = $self->dbh->selectrow_arra +y( # q{ # SELECT subject FROM mx_threads # WHERE id=? AND forum_id=? # }, # { # Slice => {} # }, # @{$forums}[$loop_counter]->{last_post}, $i # ); # @{$forums}[$loop_counter]->{post_count} = $threads + $replies; # $loop_counter++; } my $loop_counter = 0; for ( @{ $categories } ) { @{ $categories }[$loop_counter]->{forums} = $forums; $loop_counter++; } $tmpl->param( ## get the thread count on the forums, as well as the forums and t +heir information categories => $categories ); return $frm->build_page( { title => "MX::FORUMS" , output => $tmpl +->output . Dumper($categories) });# . @{$forums}[0]->{last_post}} ); }

Table Structure for forums:

/* SQLyog - Free MySQL GUI v4.1 Host - 4.1.18 : Database - mx ********************************************************************* Server version : 4.1.18 */ /*Table structure for table `mx_forums` */ drop table if exists `mx_forums`; CREATE TABLE `mx_forums` ( `id` int(11) NOT NULL auto_increment, `forum_description` text NOT NULL, `forum_name` varchar(50) NOT NULL default '', `category` varchar(50) NOT NULL default '', `create_date` datetime NOT NULL default '0000-00-00 00:00:00', `is_locked` tinyint(1) NOT NULL default '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Any and all help is much-ly appreciated.

meh.

In reply to Categorizing using DBI by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.