Programming a forum

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

At the moment, I took up a project to program a forum as a learning experience in both, programming PHP and troubleshooting (since there's bound to be problems that I would run across)... and I ran into one that I need help with.

I'm it's not really a problem as far as how to create the categories and forums per categories list. The only way I can see doing that is by using 2 loops... one inside another. The first loop would be for the categories and the second one inside of it, would be for the forums inside the categories. Is there another, better way of doing this, or is this the best way?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

You mean for when you are displaying them?

Such as on this page: forum/

I don't see any reason not to use a nested loop.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

Yeah, like that. I just didn't know if there was a better way already out there. Thanks.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

I got another problem here... this one got me baffled though.

I got the SQL working, the array is generated just like I want it to, but I'm getting some warnings... I don't know what they are about.

The warnings I get (Well, technically its "Warning" but it throws the same one twice...)
Quote:
Warning: Invalid argument supplied for foreach() in C:\wamp\www\CMS\includes\forum.php on line 51

Warning: Invalid argument supplied for foreach() in C:\wamp\www\CMS\includes\forum.php on line 51

And the PHP is...
PHP Code: [ Select ]
<?php
 
/*
 * forum.php
 *
 * Initiates the functions needed for the forum.
 */
 
class forum {
 
    function forum()
    {
        global $db, $tpl;
       
        // Making sure that the SQL doesn't cache results here...
        $db->set_opts(array('cache_results' => false));
       
        // Checking if there are any categories available
        $sql = $db->build_key_query(array('SELECT'    => '*',
                                          'FROM'      => FORUM_CAT));
 
        if($db->num_rows($sql) == 0)
        {
            $tpl->error = true;
            $tpl->errorMsg = "There are no categories available yet.";
        }
        else
        {
            // Initiating the forums array
            $tpl->forums = array();
       
            // The SQL to retrieve the categories from the database
            $cat_sql = $db->build_key_query(array('SELECT'    => '*',
                                                  'FROM'      => FORUM_CAT));
           
            // Setting the required loop variables
            $inc = 0;
           
            // The loops to get the categories and forums
            foreach($db->fetch_rowset($cat_sql) as $ckey => $cvalue)
            {
                // The category information
                $tpl->cats[$ckey] = $cvalue;
               
                // The SQL to retrieve the forums for the categories from the database
                $forums_sql = $db->build_key_query(array('SELECT'    => '*',
                                                         'FROM'      => FORUM_FORUMS,
                                                         'WHERE'     => 'catID = ' . $cvalue['catID']));
           
                // Looping through the forums and retrieving them
                foreach($db->fetch_rowset($forums_sql) as $fkey => $fvalue)
                {
                    // Setting the forums array
                    $tpl->cats[$inc]['forum'][$fkey] = $fvalue;
                }
               
                // Increasing the loop increment variable
                ++$inc;
            }
        }
    }
}
  1. <?php
  2.  
  3. /*
  4.  * forum.php
  5.  *
  6.  * Initiates the functions needed for the forum.
  7.  */
  8.  
  9. class forum {
  10.  
  11.     function forum()
  12.     {
  13.         global $db, $tpl;
  14.        
  15.         // Making sure that the SQL doesn't cache results here...
  16.         $db->set_opts(array('cache_results' => false));
  17.        
  18.         // Checking if there are any categories available
  19.         $sql = $db->build_key_query(array('SELECT'    => '*',
  20.                                           'FROM'      => FORUM_CAT));
  21.  
  22.         if($db->num_rows($sql) == 0)
  23.         {
  24.             $tpl->error = true;
  25.             $tpl->errorMsg = "There are no categories available yet.";
  26.         }
  27.         else
  28.         {
  29.             // Initiating the forums array
  30.             $tpl->forums = array();
  31.        
  32.             // The SQL to retrieve the categories from the database
  33.             $cat_sql = $db->build_key_query(array('SELECT'    => '*',
  34.                                                   'FROM'      => FORUM_CAT));
  35.            
  36.             // Setting the required loop variables
  37.             $inc = 0;
  38.            
  39.             // The loops to get the categories and forums
  40.             foreach($db->fetch_rowset($cat_sql) as $ckey => $cvalue)
  41.             {
  42.                 // The category information
  43.                 $tpl->cats[$ckey] = $cvalue;
  44.                
  45.                 // The SQL to retrieve the forums for the categories from the database
  46.                 $forums_sql = $db->build_key_query(array('SELECT'    => '*',
  47.                                                          'FROM'      => FORUM_FORUMS,
  48.                                                          'WHERE'     => 'catID = ' . $cvalue['catID']));
  49.            
  50.                 // Looping through the forums and retrieving them
  51.                 foreach($db->fetch_rowset($forums_sql) as $fkey => $fvalue)
  52.                 {
  53.                     // Setting the forums array
  54.                     $tpl->cats[$inc]['forum'][$fkey] = $fvalue;
  55.                 }
  56.                
  57.                 // Increasing the loop increment variable
  58.                 ++$inc;
  59.             }
  60.         }
  61.     }
  62. }

I also have a feeling I could make that function a little shorter but still do the same thing, but I can't manage to figure out how...

That function gives me the following array
Code: [ Select ]
Array
(
  [0] => Array
    (
      [catID] => 2
      [catName] => News and Announcements
      [catDescription] => News and Announcements
      [forums] => 1
      [forum] => Array
        (
          [forumID] => 1
          [catID] => 2
          [forumName] => Policies and Procedures
          [forumDescription] => All policies relating to hosting provided, and abuse/support procedures are housed within
          [topics] => 0
          [posts] => 0
          [lastPoster] =>
        )

    )

  [1] => Array
    (
      [catID] => 3
      [catName] => Support Center
      [catDescription] => Support Center
      [forums] => 0
    )

  [2] => Array
    (
      [catID] => 4
      [catName] => On-Topic Discussion
      [catDescription] => On-Topic Discussion
      [forums] => 0
    )

)
  1. Array
  2. (
  3.   [0] => Array
  4.     (
  5.       [catID] => 2
  6.       [catName] => News and Announcements
  7.       [catDescription] => News and Announcements
  8.       [forums] => 1
  9.       [forum] => Array
  10.         (
  11.           [forumID] => 1
  12.           [catID] => 2
  13.           [forumName] => Policies and Procedures
  14.           [forumDescription] => All policies relating to hosting provided, and abuse/support procedures are housed within
  15.           [topics] => 0
  16.           [posts] => 0
  17.           [lastPoster] =>
  18.         )
  19.     )
  20.   [1] => Array
  21.     (
  22.       [catID] => 3
  23.       [catName] => Support Center
  24.       [catDescription] => Support Center
  25.       [forums] => 0
  26.     )
  27.   [2] => Array
  28.     (
  29.       [catID] => 4
  30.       [catName] => On-Topic Discussion
  31.       [catDescription] => On-Topic Discussion
  32.       [forums] => 0
  33.     )
  34. )


[EDIT] Alright, I managed to fix the problem... appearantly if there are no results to take then there is obviously nothing to loop through :lol:
PHP Code: [ Select ]
<?php
 
/*
 * forum.php
 *
 * Initiates the functions needed for the forum.
 */
 
class forum {
 
    function forum()
    {
        global $db, $tpl;
       
        // Making sure that the SQL doesn't cache results here...
        $db->set_opts(array('cache_results' => false));
       
        // Checking if there are any categories available
        $sql = $db->build_key_query(array('SELECT'    => '*',
                                          'FROM'      => FORUM_CAT));
 
        if($db->num_rows($sql) == 0)
        {
            $tpl->error = true;
            $tpl->errorMsg = "There are no categories available yet.";
        }
        else
        {
            // Initiating the forums array
            $tpl->forums = array();
       
            // The SQL to retrieve the categories from the database
            $cat_sql = $db->build_key_query(array('SELECT'    => '*',
                                                  'FROM'      => FORUM_CAT));
           
            // Setting the required loop variables
            $inc = 0;
           
            // The loops to get the categories and forums
            foreach($db->fetch_rowset($cat_sql) as $ckey => $cvalue)
            {
                // The category information
                $tpl->cats[$ckey] = $cvalue;
               
                // The SQL to retrieve the forums for the categories from the database
                $forums_sql = $db->build_key_query(array('SELECT'    => '*',
                                                         'FROM'      => FORUM_FORUMS,
                                                         'WHERE'     => 'catID = ' . $cvalue['catID']));
           
                // Checking if the following forum has any results
                if($tpl->cats[$ckey]['forums'] > 0)
                {
                    // Looping through the forums and retrieving them
                    foreach($db->fetch_rowset($forums_sql) as $fkey => $fvalue)
                    {
                        // Setting the forums array
                        $tpl->cats[$inc]['forum'][$fkey] = $fvalue;
                    }
                }
               
                // Increasing the loop increment variable
                ++$inc;
            }
        }
    }
}
  1. <?php
  2.  
  3. /*
  4.  * forum.php
  5.  *
  6.  * Initiates the functions needed for the forum.
  7.  */
  8.  
  9. class forum {
  10.  
  11.     function forum()
  12.     {
  13.         global $db, $tpl;
  14.        
  15.         // Making sure that the SQL doesn't cache results here...
  16.         $db->set_opts(array('cache_results' => false));
  17.        
  18.         // Checking if there are any categories available
  19.         $sql = $db->build_key_query(array('SELECT'    => '*',
  20.                                           'FROM'      => FORUM_CAT));
  21.  
  22.         if($db->num_rows($sql) == 0)
  23.         {
  24.             $tpl->error = true;
  25.             $tpl->errorMsg = "There are no categories available yet.";
  26.         }
  27.         else
  28.         {
  29.             // Initiating the forums array
  30.             $tpl->forums = array();
  31.        
  32.             // The SQL to retrieve the categories from the database
  33.             $cat_sql = $db->build_key_query(array('SELECT'    => '*',
  34.                                                   'FROM'      => FORUM_CAT));
  35.            
  36.             // Setting the required loop variables
  37.             $inc = 0;
  38.            
  39.             // The loops to get the categories and forums
  40.             foreach($db->fetch_rowset($cat_sql) as $ckey => $cvalue)
  41.             {
  42.                 // The category information
  43.                 $tpl->cats[$ckey] = $cvalue;
  44.                
  45.                 // The SQL to retrieve the forums for the categories from the database
  46.                 $forums_sql = $db->build_key_query(array('SELECT'    => '*',
  47.                                                          'FROM'      => FORUM_FORUMS,
  48.                                                          'WHERE'     => 'catID = ' . $cvalue['catID']));
  49.            
  50.                 // Checking if the following forum has any results
  51.                 if($tpl->cats[$ckey]['forums'] > 0)
  52.                 {
  53.                     // Looping through the forums and retrieving them
  54.                     foreach($db->fetch_rowset($forums_sql) as $fkey => $fvalue)
  55.                     {
  56.                         // Setting the forums array
  57.                         $tpl->cats[$inc]['forum'][$fkey] = $fvalue;
  58.                     }
  59.                 }
  60.                
  61.                 // Increasing the loop increment variable
  62.                 ++$inc;
  63.             }
  64.         }
  65.     }
  66. }
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Good catch, before I looked at your resolution I was looking more at syntactical errors, but looks like it ended up being a logical error.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Quote:
Warning: Invalid argument supplied for foreach() in C:\wamp\www\CMS\includes\forum.php on line 51


pretty self explanitory ;) i am pretty certain the foreach maps to a function that requires a value in order to process. This would have led to an invalid argument exception in Java and C#. Note that it is a warning and not an error. So it is not fatal just letting you know the arguments you passed to foreach are invalid.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 155 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.