Programming a forum
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8211
- Loc: USA
- Status: Offline
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?
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?
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
April 1st, 2011, 10:25 am
- SpooF
- ٩๏̯͡๏۶


- Joined: May 22, 2004
- Posts: 3415
- Loc: Richland, WA
- Status: Offline
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.
Such as on this page: forum/
I don't see any reason not to use a nested loop.
#define NULL (::rand() % 2)
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8211
- Loc: USA
- Status: Offline
- Bogey
- Bogey


- Joined: Jul 14, 2005
- Posts: 8211
- Loc: USA
- Status: Offline
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...)
And the PHP is...
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
[EDIT] Alright, I managed to fix the problem... appearantly if there are no results to take then there is obviously nothing to loop through
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
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;
}
}
}
}
/*
* 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;
}
}
}
}
- <?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;
- }
- }
- }
- }
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
)
)
(
[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
)
)
- 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
- )
- )
[EDIT] Alright, I managed to fix the problem... appearantly if there are no results to take then there is obviously nothing to loop through
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;
}
}
}
}
/*
* 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;
}
}
}
}
- <?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;
- }
- }
- }
- }
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Bigwebmaster
- Site Admin


- Joined: Dec 20, 2002
- Posts: 8926
- Loc: Seattle, WA & Phoenix, AZ
- Status: Offline
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.
Ozzu Hosting - Want your website on a fast server like Ozzu?
- Rabid Dog
- Web Master


- Joined: May 21, 2004
- Posts: 3229
- Loc: South Africa
- Status: Offline
Quote:
Warning: Invalid argument supplied for foreach() in C:\wamp\www\CMS\includes\forum.php on line 51
pretty self explanitory
Watch me grow
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 6 posts
- Users browsing this forum: No registered users and 132 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
