Jump to content

Help with arrays


spyke01

Recommended Posts

Hi guys, ive been dreading doing this for awhile, but finally got to it. I need to make a drop down box similar to IPB's "move topic to this forum". It will contain the category name using the optgroup tag, then all forums, and under each forum will be their subforums, if any. I figured to save processing time, i should do 1 database query, drop the results into an array, ksort it, and then print out the sectiosn.

[u][b]Result should look like this:[/b][/u]
[b]Category[/b]
-Forum
--Sub Forum
--Sub Forum
[b]Category[/b]
-Forum

[u][b]Variables:[/b][/u]
$categories[$cat_order] = array('catid' => '$cat_id', 'catname' => '$cat_name');
$categories[$forum_cat_id][$forum_order] = array('forumid' => '$forum_id', 'subforumid' => '$forum_subforum_id', 'forumname' => '$forum_name');

[u][b]Info:[/b][/u]
The value of $forum_subforum_id is the same as the $forum_id that it belongs to.

[u][b]Code so far:[/b][/u] (I have no idea if this will work, its a mock-up)
[code]
<?
$categories = array();
$forums = array();

// Pull forumss from DB
$sql = "SELECT * FROM forums";
$result = mysql_query($sql);

if (mysql_num_rows($result) == 0) { echo "no results"; }
else {
while ($row = mysql_fetch_array($result)) {
extract($row);
$forums[$forums_cat_id][$forums_order] = array('forumid' => '$forums_id', 'forumsubforumid' => '$forums_subforum_id', 'forumname' => '$forums_name');
}
}
mysql_free_result($result);

// Pull categories from DB
$sql = "SELECT * FROM categories";
$result = mysql_query($sql);

if (mysql_num_rows($result) == 0) { echo "no results"; }
else {
while ($row = mysql_fetch_array($result)) {
extract($row);
$categories[$cat_order] = $cat_name;
ksort($forums[$cat_id]); // Sort forums for this category by order
}

ksort($categories); // Sort categories by order
}
mysql_free_result($result);
?>
[/code]

Can someone help me create this? I'm really unsure as to how to get this to work right with assigning variables as arrays, i try not to use them at all
Link to comment
Share on other sites

Assuming you data structure is

[pre]
categories            forum
----------            -----------
cat_id    <---------  cat_id
cat_name        +-->  forum_id
                |    forum_name
                +---  forum_subforum_id[/pre]

Try
[code]
<?php
include '../test/db.php';
echo "<SELECT name='forum'>\n";
$res1 = mysql_query("SELECT category_id, name from categories ORDER BY name") or die(mysql_error());
while (list($cid, $cname) = mysql_fetch_row($res1)) {
echo "<OPTGROUP label='$cname'>\n";
echo forumOptions($cid);
echo "</OPTGROUP>\n";
}

function forumOptions($catid, $parent=0, $level=0) {
$sql = "SELECT forum_id, forum_name
FROM forums
WHERE forum_subforum_id = $parent
AND category_id = $catid";
$res = mysql_query($sql) or die(mysql_error());
$str = '';
while (list ($fid, $fname) = mysql_fetch_row($res)) {
$indent = str_repeat('- ', $level+1);
$str .= "<OPTION value='$fid'>$indent $fname</option>\n";
$str .= forumOptions($catid, $fid, $level+1);
}
return $str;
}
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.